Home > Uncategorized > Multi-Threading Basics – Deadlocks, Livelocks and Starvation

Multi-Threading Basics – Deadlocks, Livelocks and Starvation

Deadlock:

In a retail store products get moved from one location to another. Each location is called a stockpot. Examples of locations include, Display, Free Stock(Sale Floor), Soil and Damaged, Washing and Ironing Sold etc. The stock table stores the Qty of products in each location. So when a stock movement occurs you decrement the qty in the source stockpot row and increment the qty in the destination stockpot row. This stored procedure could be called from multiple threads and it executes the increment / decrement within a transaction.

 

Assume the scenario:

Thread 1

Thread 2

Thread 1 executes a Free Stock to Display movement:

 

Thread 2 executes a Display to Free Stock movement.

Acquires lock for Free Stockpot row and decrements the qty.

 

Suspended

Context-Switch

Context-Switch

Suspended

Acquires lock for Display Stockpot row and increments the qty.

Context-switch

Context-Switch

Tries to acquire lock for the Display stock row while holding on the lock for Free Stock row (Which it cannot get so this goes into wait)

Suspended

Context-Switch

Context-Switch

Suspended

Tries to acquire lock for the Free stock row while holding on the lock for Display Stock row (Which it cannot get so this goes into wait)

Suspended

Suspended

 

Both the threads are now waiting for each other to release a resource, this is called a deadlock.

 

A livelock is similar to a deadlock, except that the state of the two processes involved in the livelock constantly changes with regards to the other process. As a real world example, livelock occurs when two people meet in a narrow corridor, and each tries to be polite by moving aside to let the other pass, but they end up swaying from side to side without making any progress because they always both move the same way at the same time. A deadlock results in an infinite wait whereas a livelock results in wasting CPU cycles.

 

Starvation is a problem, where a process is perpetually denied necessary resources. Without those resources, the program can never finish its task. Starvation is related to deadlock. Deadlock occurs when two programs each hold resources the other needs to finish, and neither is willing to give them up. Starvation occurs when one program holds resources the other needs, but is unwilling to give them up.

Advertisements
Categories: Uncategorized
  1. sugan
    March 1, 2012 at 4:03 pm

    good…….

  2. Emmad
    February 10, 2013 at 10:24 pm

    explained in very easy and brilliant way 🙂

  3. May 10, 2013 at 7:16 pm

    What about condition synchronization and mutual exclusion?

  1. November 19, 2010 at 10:59 pm
  2. November 19, 2010 at 11:09 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: