-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdeadlockExample.c
62 lines (52 loc) · 1.34 KB
/
deadlockExample.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
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
pthread_mutex_t read_mutex;
pthread_mutex_t write_mutex;
void * writeTest(void *temp) {
char *ret;
FILE *file1;
char *str;
pthread_mutex_lock(&write_mutex);
sleep(5);
pthread_mutex_lock(&read_mutex);
printf("\nFile locked, please enter the message \n");
str=(char *)malloc(10*sizeof(char));
file1=fopen("temp","w");
scanf("%s",str);
fprintf(file1,"%s",str);
fclose(file1);
pthread_mutex_unlock(&read_mutex);
pthread_mutex_unlock(&write_mutex);
printf("\nUnlocked the file you can read it now \n");
return ret;
}
void * readTest(void *temp) {
char *ret;
FILE *file1;
char *str;
pthread_mutex_lock(&read_mutex);
sleep(5);
pthread_mutex_lock(&write_mutex);
printf("\n Opening file \n");
file1=fopen("temp","r");
str=(char *)malloc(10*sizeof(char));
fscanf(file1,"%s",str);
printf("\n Message from file is %s \n",str);
fclose(file1);
pthread_mutex_unlock(&write_mutex);
pthread_mutex_unlock(&read_mutex);
return ret;
}
int main() {
pthread_t thread_id,thread_id1;
pthread_attr_t attr;
int ret;
void *res;
ret=pthread_create(&thread_id,NULL,&writeTest,NULL);
ret=pthread_create(&thread_id1,NULL,&readTest,NULL);
printf("\n Created thread");
pthread_join(thread_id,&res);
pthread_join(thread_id1,&res);
}