-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpeterson.c
56 lines (49 loc) · 975 Bytes
/
peterson.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
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
int value, turn=0;
int flag[2];
void entry(int id)
{
flag[id] = 1;
turn = 1 - id;
while(flag[1-id] == 1 && turn == 1- id);
}
void exit1(int id)
{
flag[id] = 0;
}
void *pth(void *value)
{
int *val = (int*)value;
printf("Producer Thread:\n");
printf("%d\n", *val);
entry(0);
*val = *val - 100;
exit1(0);
printf("producer output %d\n", *val);
return NULL;
}
void *cth(void *value)
{
int *val = (int *) value;
printf("Consumer Thread:\n");
printf("%d\n", *val);
entry(1);
*val = *val + 300;
exit1(1);
printf("consumer output %d\n", *val);
return NULL;
}
int main()
{
value = 1000;
flag[1] = 0;
flag[0] = 0;
pthread_t producer, consumer;
pthread_create(&producer, NULL, pth,(void *)&value);
pthread_create(&consumer, NULL, cth, (void *)&value);
pthread_join(producer, NULL);
pthread_join(consumer, NULL);
exit(0);
}