|
| 1 | +/* Script to demonstrate deadlock. |
| 2 | + On running this, nothing will be printed as both the threads are waiting |
| 3 | + to get the lock acquired by the other process. Hence, never enter their |
| 4 | + critical sections. Changing both the executing functions to be the same, |
| 5 | + essentially makes it deadlock-free. |
| 6 | +*/ |
| 7 | + |
| 8 | +#include <pthread.h> |
| 9 | +#include <stdio.h> |
| 10 | + |
| 11 | +/* function that the first thread will execute in */ |
| 12 | +void *do_work_one(void *param); |
| 13 | +/* function that the second thread will execute in */ |
| 14 | +void *do_work_two(void *param); |
| 15 | + |
| 16 | +/* Create and initialize the mutex locks */ |
| 17 | +pthread_mutex_t first_mutex, second_mutex; |
| 18 | + |
| 19 | +int main(){ |
| 20 | + pthread_t tid_1, tid_2; /* the thread identifiers */ |
| 21 | + pthread_attr_t attr_1, attr_2; /* thread attributes */ |
| 22 | + |
| 23 | + /* initialize the mutex locks */ |
| 24 | + pthread_mutex_init(&first_mutex, NULL); |
| 25 | + pthread_mutex_init(&second_mutex, NULL); |
| 26 | + |
| 27 | + /* initialize the thread attributes */ |
| 28 | + pthread_attr_init(&attr_1); |
| 29 | + pthread_attr_init(&attr_2); |
| 30 | + |
| 31 | + /* Create thread one */ |
| 32 | + pthread_create(&tid_1, &attr_1, do_work_one, NULL); |
| 33 | + |
| 34 | + /* Create thread two */ |
| 35 | + pthread_create(&tid_2, &attr_2, do_work_two, NULL); |
| 36 | + |
| 37 | + /* Wait for the two threads to complete */ |
| 38 | + pthread_join(tid_1, NULL); |
| 39 | + pthread_join(tid_2, NULL); |
| 40 | +} |
| 41 | + |
| 42 | +/* function that the first thread will execute in */ |
| 43 | +void *do_work_one(void *param){ |
| 44 | + /* Get the first mutex lock if it's available */ |
| 45 | + pthread_mutex_lock(&first_mutex); |
| 46 | + /* Get the second mutex lock if it's available */ |
| 47 | + pthread_mutex_lock(&second_mutex); |
| 48 | + |
| 49 | + /* Critical section start */ |
| 50 | + printf("Inside Thread 1\n"); |
| 51 | + /* Critical section ends */ |
| 52 | + |
| 53 | + /* Release the locks */ |
| 54 | + pthread_mutex_unlock(&first_mutex); |
| 55 | + pthread_mutex_unlock(&second_mutex); |
| 56 | + |
| 57 | + /* Exit the thread */ |
| 58 | + pthread_exit(0); |
| 59 | +} |
| 60 | + |
| 61 | +/* function that the second thread will execute in */ |
| 62 | +void *do_work_two(void *param){ |
| 63 | + /* Get the second mutex lock. Note the order of waiting on the locks. |
| 64 | + The second thread gets the second mutex lock and waits for the first |
| 65 | + mutex lock, which is held by the first thread, which is waiting for the |
| 66 | + second mutex lock to be released. This leads to DEADLOCK. |
| 67 | + */ |
| 68 | + pthread_mutex_lock(&second_mutex); |
| 69 | + /* Get the first mutex lock */ |
| 70 | + pthread_mutex_lock(&first_mutex); |
| 71 | + |
| 72 | + /* Critical section starts */ |
| 73 | + printf("Inside thread 2\n"); |
| 74 | + /* Critical section ends */ |
| 75 | + |
| 76 | + /* Release the locks */ |
| 77 | + pthread_mutex_unlock(&second_mutex); |
| 78 | + pthread_mutex_unlock(&first_mutex); |
| 79 | + |
| 80 | + /* Exit the thread */ |
| 81 | + pthread_exit(0); |
| 82 | +} |
0 commit comments