A Program to Test Deadlock - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Community

demo-image

A Program to Test Deadlock

Share This

In the following program, we will try to test a deadlock scenario.

In this scenario, we are creating two threads, MY_THREAD_1 and MY_THREAD_2 . Moreover, we have two resources, resourceA and resourceB .

Source code of deadlock.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <pthread.h>
#include <stdio.h>
#include <sched.h>
#include <time.h>
#define NUM_OF_THREADS 2
#define MY_THREAD_1 1
#define MY_THREAD_2 2
pthread_t threads[NUM_OF_THREADS];
struct sched_param nrt_param;
pthread_mutex_t resourceA, resourceB;
volatile int resourceACount=0, resourceBCount=0, noWait=0;
void *grabresources(void *threadid)
{
if((int)threadid == MY_THREAD_1)
{
printf("Thread-1 is grabbing resources...\n");
pthread_mutex_lock(&resourceA);
resourceACount++;
if(!noWait)
sleep(1000000);
printf("Thread-1 has received resource-A, trying for resource-B...\n");
pthread_mutex_lock(&resourceB);
resourceBCount++;
printf("Thread-1 has received resource-A and resource-B...\n");
pthread_mutex_unlock(&resourceB);
pthread_mutex_unlock(&resourceA);
printf("Thread-1 is complete...\n");
}
else
{
printf("Thread-2 is grabbing resources...\n");
pthread_mutex_lock(&resourceB);
resourceBCount++;
if(!noWait)
usleep(1000000);
printf("Thread-2 has received resource-B, trying for resource-A...\n");
pthread_mutex_lock(&resourceA);
resourceACount++;
printf("Thread-2 has received resource-B and resource-A...\n");
pthread_mutex_unlock(&resourceA);
pthread_mutex_unlock(&resourceB);
printf("Thread-2 is complete...\n");
}
pthread_exit(NULL);
}
int main (int argc, char *argv[])
{
int status, safe=0;
resourceACount=0, resourceBCount=0, noWait=0;
if(argc < 2)
{
printf("Will set up unsafe deadlock scenario...\n");
}
else if(argc == 2)
{
if(strncmp("safe", argv[1], 4) == 0)
safe=1;
else if(strncmp("race", argv[1], 4) == 0)
noWait=1;
else
printf("Will set up unsafe deadlock scenario...\n");
}
else
{
printf("Usage: deadlock [safe|race|unsafe]\n");
}
pthread_mutex_init(&resourceA, NULL);
pthread_mutex_init(&resourceB, NULL);
printf("Creating thread %d\n", MY_THREAD_1);
status = pthread_create(&threads[0], NULL, grabresources, (void *)MY_THREAD_1);
if (rc)
{
printf("ERROR: pthread_create() status is %d\n", status);
perror(NULL);
exit(-1);
}
printf("Thread-1 spawned\n");
if(safe)
{
if(pthread_join(threads[0], NULL) == 0)
printf("Thread-1: %d done\n", threads[0]);
else
perror("Thread-1");
}
printf("Creating thread %d\n", MY_THREAD_2);
status = pthread_create(&threads[1], NULL, grabresources, (void *)MY_THREAD_2);
if (rc)
{
printf("ERROR: pthread_create() status is %d\n", status);
perror(NULL);
exit(-1);
}
printf("Thread-2 spawned\n");
printf("resourceACount=%d, resourceBCount=%d\n", resourceACount, resourceBCount);
printf("will try to join CS threads unless they deadlock\n");
if(!safe)
{
if(pthread_join(threads[0], NULL) == 0)
printf("Thread-1: %d done\n", threads[0]);
else
perror("Thread-1");
}
if(pthread_join(threads[1], NULL) == 0)
printf("Thread-2: %d done\n", threads[1]);
else
perror("Thread-2");
if(pthread_mutex_destroy(&resourceA) != 0)
perror("mutex A destroy");
if(pthread_mutex_destroy(&resourceB) != 0)
perror("mutex B destroy");
printf("Everyting is complete...\n");
exit(0);
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

To compile the Program use

$gcc deadlock.c -o deadlock -lpthread

To run

$./deadlock

Happy Exploring!

Comment Using!!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.