-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathframe.c
More file actions
177 lines (141 loc) · 3.47 KB
/
frame.c
File metadata and controls
177 lines (141 loc) · 3.47 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#define _POSIX_SOURCE 1
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <math.h>
int encode(unsigned short i, unsigned short j);
int validate(int c1, int c2, unsigned short ph, unsigned short phils);
void counter(pid_t producer);
unsigned short rounds;
int phils;
int main(int argc, char* argv[])
{
unsigned short i,j;
int c, milisec_delay;
char buf1[10], buf2[10],buf3[10];
pid_t producer;
if (argc != 4 || ((rounds= atoi(argv[2]))<0) || ((phils= atoi(argv[1]))<2) || ((milisec_delay= atoi(argv[3]))<0)) {
fprintf(stderr,"%s NB_OF_PHILS NB_OF_ROUNDS MILISECONDS_TURN_DELAY\n", argv[0]);
exit(1);
}
int pipes[2*phils], opipe[2];
pid_t children[phils];
fprintf(stderr,"philosophers: %d\n", phils);
fprintf(stderr,"rounds: %d\n", rounds);
if (pipe(opipe)){
fprintf(stderr,"Output pipe creation failed.\n");
exit(1);
}
producer= fork();
if (producer == -1) {
fprintf(stderr,"Producer process fork failed.\n");
exit(1);
}
if (producer) {
dup2(opipe[0],0);
close(opipe[1]);
close(opipe[0]);
counter(producer);
// never returns
}
/* PRODUCER STARTS HERE */
for (i=0; i<phils; i++) {
if (pipe(&pipes[2*i])){
fprintf(stderr,"Pipe creation failed.\n");
exit(1);
};
}
close(opipe[0]);
sprintf(buf1, "%d", phils);
sigset_t usrmask, oldmask;
sigemptyset(&usrmask);
sigaddset(&usrmask, SIGUSR1);
sigprocmask(SIG_BLOCK, &usrmask, &oldmask);
for (i=0; i< phils; i++){
children[i]= fork();
if (children[i]==-1){
fprintf(stderr,"Fork failed.\n");
exit(1);
}
if (children[i]==0){
close(0);
dup2(opipe[1], 1);
close(opipe[1]);
dup2(pipes[(2*i+2)%(2*phils)],4);
dup2(pipes[2*i],3);
for (j=0; j< 2*phils; j++) close(pipes[j]);
sprintf(buf2, "%d",i);
if (!execl("./phil.x","phil.x", buf1, buf2, NULL)){
fprintf(stderr,"Exec failed!\n");
exit(1);
}
}
}
sigprocmask(SIG_BLOCK, &oldmask, NULL);
close(opipe[1]);
for (i=0; i<phils; i++){
close(pipes[2*i]);
write(pipes[2*i+1], &(children[(i+1)%phils]), sizeof(pid_t));
}
for (j=0; j< rounds; j++){
usleep(milisec_delay*1000);
for (i=0; i<phils; i++){
c=encode(j,i);
write(pipes[2*i+1],&j,sizeof(short)); // time stamp
write(pipes[2*i+1],&c,sizeof(int)); // value
}
}
}
void counter(pid_t producer){
int valid[phils], invalid[phils];
int i,n, c1, c2, minv, maxv, total;
char buf[10];
unsigned short ph;
for (i=0; i<phils; i++)
valid[i]=invalid[i]=0;
fprintf(stderr,"Counter STARTED\n");
while (n=read(0,buf,10)){
if (n==-1)
if (errno==EINTR) continue;
else{
fprintf(stderr,"Read failed.\n");
exit(0);
}
if (n!=10) {
fprintf(stderr,"Boundary error: %d\n",n);
exit(0);
}
ph=*((unsigned short *) buf);
c1=*((int*) (buf+2));
c2=*((int*) (buf+6));
if (ph!=ph%phils) {
fprintf(stderr,"Invalid ID: %d\n",ph);
exit(0);
}
else if (!validate(c1,c2,ph,phils)){
fprintf(stderr,"Invalid pair: [%x,%x] from %d\n",c1,c2,ph);
invalid[ph]++;
} else {
fprintf(stderr,"Valid pair: [%x,%x] from %d\n",c1,c2,ph);
valid[ph]++;
}
}
minv= rounds;
maxv=0;
total=0;
for (i=0; i<phils; i++) {
fprintf(stderr,"[%d] valid:%d invalid:%d\n", i,valid[i],invalid[i]);
if (invalid[i]) exit(0); // Fail
if (minv > valid[i]) minv=valid[i];
if (maxv < valid[i]) maxv=valid[i];
total+=valid[i];
}
if (maxv>minv+1) exit(0); // Fail
if (total != rounds*(phils/2)) exit(0); // Fail
printf("OK");
exit(0);
}