-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeadlockThreeThreadsContibutionTwo.c
More file actions
57 lines (51 loc) · 1.14 KB
/
Copy pathdeadlockThreeThreadsContibutionTwo.c
File metadata and controls
57 lines (51 loc) · 1.14 KB
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
#include<stdio.h>
#include<pthread.h>
#include<sys/types.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>
int shared = 1;
pthread_mutex_t l,l1;
void *fun1();
void *fun2();
void *fun3();
int main()
{
pthread_mutex_init(&l, NULL); /*initiliazing mutex*/
pthread_mutex_init(&l1, NULL); /*initiliazing mutex*/
pthread_t thread1, thread2,thread3;
pthread_create(&thread1, NULL, fun1, NULL);
pthread_create(&thread2, NULL, fun2, NULL);
pthread_create(&thread3,NULL,fun3,NULL);
pthread_join(thread3,NULL);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
printf("FInal vvalue of shared is %d \n", shared);
}
void *fun1()
{ printf("i am here");
int x;
pthread_mutex_lock(&l);
x = shared;
x++;
sleep(1);
pthread_mutex_lock(&l1);
shared = x;
pthread_mutex_lock(&l1);
pthread_mutex_unlock(&l);
}
void *fun2()
{ printf("yo");
int y;
pthread_mutex_lock(&l1);
y = shared;
y--;
sleep(3);
pthread_mutex_lock(&l);
shared = y;
pthread_mutex_unlock(&l);
pthread_mutex_unlock(&l1);
}
void *fun3(){
printf("done thread3");
sleep(1);}