-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
143 lines (131 loc) · 3.52 KB
/
main.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
133
134
135
136
137
138
139
140
141
142
143
#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>
#include "network.h"
# include <semaphore.h>
sem_t mutex;
#define MAX 5
int ports[] = {8001, 8002, 8003, 8004, 8005};
int pid[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int compute_left_fork(int id){
return id;
}
int compute_right_fork(int id){
return (id+1)%MAX;
}
void test(int id, int * state, int left, int right){
if(*state != HUNGRY) return;
int res_left = acquire_fork(id,left,ports[left]);
if(res_left != GRANTED) return;
int res_right = acquire_fork(id,right, ports[right]);
if(res_right != GRANTED){
release_fork(id,left,ports[left]);
return;
}
*state = EATING;
display(id,*state);
sleep(rand()%3+2);
}
void take_fork(int id, int * state, int left, int right){
if(*state != HUNGRY){
*state = HUNGRY;
display(id,*state);
}
sem_wait(&mutex);
test(id,state,left,right);
sem_post(&mutex);
sleep(rand()%3+2);
}
void put_fork(int id, int * state, int left, int right){
sem_wait(&mutex);
if(*state != THINKING){
*state=THINKING;
display(id,*state);
}
release_fork(id,left,ports[left]);
release_fork(id,right,ports[right]);
sleep(rand()%3+2);
sem_post(&mutex);
}
void philosopher_start(int id){
int state = THINKING;
display(id,state);
int left = compute_left_fork(id);
int right = compute_right_fork(id);
while(TRUE){
take_fork(id, &state, left, right);
put_fork(id, &state, left, right);
}
}
void fork_start(int id){
printf("Fork %d\n",id);
int fork_id = fork_network_call(id, ports[id]);
int state = AVAILABLE;
int command, philosopher;
while (TRUE){
// int clientfd = accept(fork_id, (struct sockaddr*)&address, (socklen_t*)&addrlen);
int clientfd = accept(fork_id, NULL, NULL);
if (clientfd < 0) {
perror("Accept Failed");
exit(EXIT_FAILURE);
}
read(clientfd, &command, sizeof(command));
read(clientfd, &philosopher, sizeof(philosopher));
if(command == REQUEST){
if(state == AVAILABLE){
state = OCCUPIED;
command = GRANTED;
write(clientfd, &command, sizeof(command));
}else{
command = REJECTED;
write(clientfd, &command, sizeof(command));
}
}else if(command == RELEASE){
state = AVAILABLE;
command = GRANTED;
write(clientfd, &command, sizeof(command));
}
}
}
int main() {
sem_init(&mutex, 1, 1);
if(pid[0]=fork()==0){
fork_start(0);
exit(EXIT_SUCCESS);
}else if(pid[1]=fork()==0){
fork_start(1);
exit(EXIT_SUCCESS);
}else if(pid[2]=fork()==0){
fork_start(2);
exit(EXIT_SUCCESS);
}else if(pid[3]=fork()==0){
fork_start(3);
exit(EXIT_SUCCESS);
}else if(pid[4]=fork()==0){
fork_start(4);
exit(EXIT_SUCCESS);
}
sleep(2);
if(pid[5]=fork()==0){
philosopher_start(0);
exit(EXIT_SUCCESS);
}else if(pid[6]=fork()==0){
philosopher_start(1);
exit(EXIT_SUCCESS);
}else if(pid[7]=fork()==0){
philosopher_start(2);
exit(EXIT_SUCCESS);
}else if(pid[8]=fork()==0){
philosopher_start(3);
exit(EXIT_SUCCESS);
}else if(pid[9]=fork()==0){
philosopher_start(4);
exit(EXIT_SUCCESS);
}
sleep(60);
for(int i = 0; i< 10;i++){
kill(pid[i],SIGQUIT);
}
wait(NULL);
return 0;
}