Deadlock – Getting Started

Well, I hope the readers have some idea about process synchronization. You may refer my friend’s blog http://priyachalakkal.wordpress.com/tag/semaphores/ to understand about mutual exclusion problem and use of semaphore much clearly.

All-rite! so let’s start with Deadlock. Consider a multiprogramming environment. Here several processes compete for finite number of resources. What if one process P1 waits for a resource R1 which is currently held by some other process P2? P1 enters into a waiting state. There are chances that P1 never enters the ready state since the resources it needs are always occupied by some other process. Such a situation is referred as deadlock.

Silberschatz book explains this with a beautiful example. Consider a system with 3 CD drives and 3 processes. Suppose each of the process holds one of these CD Drives, and they now request for CD Drive held by some other process. Here the 3 processes will be in a deadlock state. Let’s discuss with some more example.

Consider this pthread program.

void *one(void *arg)
{
    pthread_mutex_lock(&first_mutex);
    pthread_mutex_lock(&second_mutex);
  /* D0 $0me Task */

    pthread_mutex_unlock(&second_mutex);
    pthread_mutex_unlock(&first_mutex);

    pthread_exit(0);
}

void *two(void *arg)
{
    pthread_mutex_lock(&second_mutex);
    pthread_mutex_lock(&first_mutex);
    /* D0 $0me Task */

    pthread_mutex_unlock(&first_mutex);
    pthread_mutex_unlock(&second_mutex);

    pthread_exit(0)
}

Now assume two threads are created. Both of them have access to both the mutex_locks. Thread1 runs in function ‘one’, thread2 runs in function ‘two’. So, thread_one acquires lock in the order first_mutex and second mutex. Whereas thread_two acquires lock in the order second_mutex and first_mutex. This is an example for deadlock where both keeps waiting to acquire the lock. (thread_1 attempts to lock mutex_one whereas thread_2 attempts to lock mutex_two.)

Lets discussion the Deadlock characterization in the next post.

Advertisements

Tagged: ,

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