-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab10-1.c
85 lines (78 loc) · 1.46 KB
/
lab10-1.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
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <malloc.h>
#include <semaphore.h>
sem_t chop[5];
delay()
{
int i,j;
for(i=0;i<1000000;++i)
{
for(j=0;j<1000;j++)
{
}
}
}
void * thr(void * p)
{
int q=(int)p;
do
{
if(q%2==0)
{
printf("philosopher%d is willing to enter\n",q);
delay();
sem_wait(&chop[(q+1)%5]);
sem_wait(&chop[q]);
////////////////////////////////
printf("philosopher%d is in critical section\n",q);
delay();
//////////////////////////////////
sem_post(&chop[q]);
sem_post(&chop[(q+1)%5]);
printf("philosopher%d is outside section\n",q);
delay();
}
else
{
printf("philosopher%d is willing to enter\n",q);
delay();
sem_wait(&chop[q]);
sem_wait(&chop[(q+1)%5]);
//critical section
printf("philosopher%d is in critical section\n",q);
delay();
//////////////////////
sem_post(&chop[(q+1)%5]);
sem_post(&chop[q]);
printf("philosopher%d is outside section\n",q);
delay();
}
}while(1);
}
main()
{
int i;
for(i=0;i<5;++i)
{
sem_init(&chop[i],0,1);
}
pthread_t thread[5];
int t,re;
t=0;
for(t=0;t<5;++t)
{
re=pthread_create(&thread[t],NULL,thr,(void *)t);
printf("thread%d created.............\n",t);
if(re)
{
printf("error");
exit(-1);
}
}
for(t=0;t<5;++t)
{
pthread_join(thread[t],NULL);
}
}