-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiningPhil.c
executable file
·132 lines (118 loc) · 2.17 KB
/
diningPhil.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <gtthread.h>
#include "mutex.h"
#define N 5
#define THINKING 0
#define HUNGRY 1
#define EATING 2
#define LEFT (i+N-1)%N
#define RIGHT (i+1)%N
int state[N];
gtthread_mutex_t mutex;
int semaphore[N];
int philosopher[N] ={0,1,2,3,4};
int chopsticks[N];
int i,k;
void init();
void getSticks(int );
void releaseSticks(int);
void test(int);
void init(){
gtthread_init(1000L);
gtthread_mutex_init(&mutex);
for(i=0;i<N;i++)
{
chopsticks[i]=0;
semaphore[i]=0;
}
return;
}
void think(int i){
for(k=0;k<1000000000;k++); // wait
printf("\n\tPhilosopher %d is THINKING.",i+1);
return;
}
void up(int *sem){
volatile int temp = *sem;
if(temp <= 0)
{
(*sem)++;
}
else
{
while(1){ printf("\n Spinning in UP\n"); if(temp<=0){ break;}}
}
return;
}
void down(int *sem){
volatile int temp = *sem;
if(temp>0)
{
(*sem)--;
}
else
{
while(1){ printf("\n Spinning in Down");if(temp >0){break;}}
}
return;
}
void getSticks(int i)
{ printf("\n\tPhilosopher %d is getting sticks ",i+1);
gtthread_mutex_lock(&mutex);
state[i]=HUNGRY;
printf("\n\tPhilosopher %d is HUNGRY ",i+1);
test(i);
gtthread_mutex_unlock(&mutex);
down(&semaphore[i]);
return;
}
void releaseSticks(int i)
{
printf("\n\tPhilosopher %d is releasing Sticks ",i+1);
gtthread_mutex_lock(&mutex);
state[i]= THINKING;
test(LEFT);
test(RIGHT);
gtthread_mutex_unlock(&mutex);
return;
}
void test(int i)
{ //printf("\nPhilosospher %d ndState[i] =%d , state[LEFT] = %d, state[RIGHT]= %d",i+1,state[i],state[LEFT],state[RIGHT]);
if(state[i]==HUNGRY && state[LEFT]!= EATING && state[RIGHT]!=EATING)
{
state[i]=EATING;
printf("\n\nPhilosopher %d is EATING ",i+1);
up(&semaphore[i]);
}
return;
}
/*
void eat(int i)
{
printf("\n\t Philosopher %d is EATING : ",i+1);
return;
}
*/
void *worker(void *phil){
int* pointer = phil;
int temp = *pointer;
while(1)
{
think(temp);
getSticks(temp);
//eat(temp);
releaseSticks(temp);
}
}
int main()
{
int i;
init();
gtthread_t philosophers_threads[N];
for(i=0;i<N;i++)
{
printf("\n\tPhilosopher number %d is thinking\n",i+1);
gtthread_create(&philosophers_threads[i],worker,&philosopher[i]);
}
//gtthread_exit(NULL);
for(;;);
}