Threads & Concurrency

 Process Execution State

  • Program Counter determines the next instruction to be executed.

  • Results in a stream of instructions which are often referred to as a thread of execution.

Threads

The sequential flow of control within a process is called as a thread.

Each thread has

  • A program counter

  • A register set

  • A stack space

Multi-Threaded Process

  • A process can have multiple threads.

  • Each thread can run on a separate processor, thus utilizing multiple cores of the CPU

Single-Thread Process

Process vs Threads

Threads are lightweight.

  • Less time to create and terminate.

  • Faster to context switch between threads.

  • Quicker communication between threads.

Example

Google Chrome

  • Core control part of the browser - browser process
  • Rendering the web page and interaction - render process
  • Plugins
  • Efficient running - GPU process

In Chrome, each and every tab you open gets its own content process. Each of those processes has its own memory. Maximizes performance Ten tabs, 10 processes. One hundred tabs, 100 processes.

Mozilla Firefox

  • Four different processes. Multiple tabs run as threads within these processes.

  • Relatively less memory utilization compared to Chrome.

Advantages of Multi-Thread

  • Responsiveness

  • Resource Sharing

  • Economy

  • Utilization of multiprocessor architectures

Thread Concurrency

  • Concurrency is the execution of the multiple instruction sequences at the same time.

  • Concurrency occurs when several process threads running in parallel.

Advantages

  • Responsive applications

  • Better resource utilization & performance.

Disadvantages

  • Multi-threaded programs are harder to develop and can have bugs that are difficult to debug.

Example:

Let

  • user1 balance = 2500
  • user2 balance = 500

Thread A

def transfer_amount(u1, u2, amt):
b1 = get_balance(u1)
b2 = get_balance(u2)
b1 -= amt
b2 += amt
update_balance(u1, b1)
update_balance(u2, b2)
transfer_amount(u1, u2, 1000)
PYTHON

If the above operation is executing in the Thread A, and after the execution of four line of code Thread B comes with priority. So context switching happens in middle of the bank operation.

Thread B

def transfer_amount(u1, u2, amt):
b1 = get_balance(u1)
b2 = get_balance(u2)
b1 -= amt
b2 += amt
update_balance(u1, b1)
update_balance(u2, b2)
transfer_amount(u1, u2, 1600)
PYTHON

After the execution of Thread B

  • user1 balance = 900
  • user2 balance = 2100

Thread A completes its remaining operation, after execution of Thread A

  • user1 balance = 1500
  • user2 balance = 1500

Which is not at all possible.

Solution

  • The critical section is a code segment where the shared data is operated.

  • Ensuring that only one process/thread can be in its critical section at a time eliminates concurrency issues.

  • Use Locks to ensure this

Locks allow us to limit number of threads running the critical section at a given time. Ensure all threads have to

  • Acquire a lock before entering critical section

  • Release after leaving critical section

Deadlock

A situation where a set of processes are blocked because each process is holding a resource and waiting for another resource acquired by some other process.

Deadlock Conditions

Conditions for Deadlock:

  • Mutual exclusion: The resource can be held by one process at a time

  • Hold and wait: A process can hold multiple resources and still request more resources from other processes which are holding them.

  • No preemption: A resource cannot be acquired from a process by force. A process can only release a resource voluntarily.

  • Circular wait: If every process is waiting for each other
    to release the resource and no one is releasing their own resource. This is called a circular wait.

Dealing with Deadlock

  • Avoid deadlocks by ensuring at least one of the above conditions is not met.

  • Let deadlocks happen. Detect and break the deadlocks

Post a Comment

Please Select Embedded Mode To Show The Comment System.*

Previous Post Next Post

Contact Form