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