-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest1.c
362 lines (340 loc) · 8.09 KB
/
test1.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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*************************************************************************
> File Name: test1.c
> Author:KID_XiaoYuan
> Mail:[email protected]
> Created Time: 2017年05月19日 星期五 22时41分03秒
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include<conio.h>
#include<
#define MAXPRI 100
#define NIL -1
#define True 1
#define False 0
static int s1=0,s2=0;
typedef struct messagetp *message;
struct messagetp{
int num;
int pro;
message next;
};
struct procb{
int id;
int nextwr;
int priority;
int inum;
char status;
char addr;
message mess;
};
struct semaphorel{
int value;
int firstwr;
};
struct procb pcb[3];
struct semaphorel sem[2];
char addr;
int i,seed,exe;
/*int style;*/
void send(int sender,int receiver,int snum)
{ message q,p;
p=(message)malloc(sizeof(struct messagetp));
p->num=snum;
p->next=NULL;
p->pro=sender;
printf("send message to process %d\n",receiver);
printf("process %d already run %d times\n",sender,snum);
q=pcb[receiver].mess;
if(q==NULL) pcb[receiver].mess=p;
else {
while(q->next!=NULL)
q=q->next;
q->next=p;
}
}
void receive(int receiver)
{ message p;
p=pcb[receiver].mess;
if (p!=NULL)
{
while (p!=NULL){
printf("receive message from process %d\n",p->pro);
printf("process %d is already run %d time\n",p->pro,p->num);
printf("\n");
p=p->next;
}
pcb[receiver].mess=NULL;
}
}
void init()
{ int j;
for(j=0;j<=2;j++){ /*initiate pcb and sem,current process is set to -1*/
pcb[j].id=j;
pcb[j].status='r';
pcb[j].nextwr=NIL;
printf("process %d priority?",j);
scanf("%d",&i);
pcb[j].priority=i;
pcb[j].mess=NULL;
pcb[j].inum=0;
pcb[j].addr='0';
}
sem[0].value=1;
sem[0].firstwr=NIL;
sem[1].value=1;
sem[1].firstwr=NIL;
exe=NIL;
}
float random()
{ int m;
if(seed<0) m=-seed;
else m=seed;
seed=(25173*seed+13849)%65536;
return (m/32767.0);
}
int find()
{
int j,pd,w;
pd=NIL;
w=MAXPRI;
for(j=0;j<=2;j++)
if (pcb[j].status=='r')
if (pcb[j].priority<w){
w=pcb[j].priority;
pd=j;
}
if (pd==NIL)
for(j=0;j<=2;j++)
if (pcb[j].status=='t')
if(pcb[j].priority<w){
w=pcb[j].priority;
pd=j;
}
return pd;
}
int scheduler()
{ int pd;
pd=find();
if ((pd==NIL)&&(exe==NIL)) return NIL;
else{
if (pd!=NIL){
if(exe==NIL) { /*no current is executing*/
pcb[pd].status='e';
exe=pd;
printf("process %d is executing\n",exe);
}
else if (pcb[pd].priority<pcb[exe].priority){
pcb[exe].status='r';
printf("process %d enter into ready\n",exe);
pcb[pd].status='e';
exe=pd;
printf("proecess %d is executing\n",exe);
}
} /*if*/
i=pcb[exe].inum;
addr=pcb[exe].addr;
return exe;
}
}
void block(int se)
{
int w;
printf("process %d is blocked\n",exe);
pcb[exe].status='w';
pcb[exe].nextwr=NIL;
w=sem[se].firstwr;
if (w==NIL) sem[se].firstwr=exe;
else {
while (pcb[w].nextwr!=NIL)
w=pcb[w].nextwr;
pcb[w].nextwr=exe;
}
}
int p(int se,char ad)
{
sem[se].value=sem[se].value-1;
if(sem[se].value>=0) return False;
else
{
block(se);
pcb[exe].inum=i;
pcb[exe].addr=ad;
exe=NIL;
return True;
}
}
void wakeup(int se)
{ int w;
w=sem[se].firstwr;
if (w!=NIL)
{
sem[se].firstwr=pcb[w].nextwr;
pcb[w].status='r';
printf("process %d is wakeup\n",w);
}
}
int v(int se,char ad)
{
sem[se].value=sem[se].value+1;
if (sem[se].value>0) return False;
else{
wakeup(se);
pcb[exe].inum=i;
pcb[exe].addr=ad;
return True;
}
}
int timeint(char ad)
{ float x;
x=random();
if((x<0.33)&&(exe==0)) return False;
else if((x<0.66)&&(exe==1)) return False;
else if((x<1.00)&&(exe==2)) return False;
else {
pcb[exe].inum=i;
pcb[exe].addr=ad;
pcb[exe].status='t';
printf("time slice interrupt.\n");
printf("process %d enter into ready\n",exe);
exe=NIL;
return True;
}
}
void eexit(int n)
{
pcb[n].status='c';
printf("process %d is completed\n",n);
exe=NIL;
}
void process0()
{
if(addr=='a') goto a0;
if(addr=='b') goto b0;
if(addr=='c') goto c0;
if(addr=='d') goto d0;
if(addr=='e') goto e0;
if(addr=='f') goto f0;
while(i<5) {
receive(0);
printf("process 0 calls P on semaphore0.\n");
/*do p operation to sem[0],if no critical resoure,process block at address 'a'*/
if(p(0,'a')) goto stop0;
a0: printf("process 0 is executing on its cretical section0.\n");
if (timeint('b')) goto stop0;
b0: printf("seed=%d\n",seed);
s1=s1+1;
printf("s1=%d",s1);
printf("process 0 calls V on semaphore 0 and quit critical section 0.\n");
if(v(0,'c')) goto stop0;
c0: printf("process 0 calls P on semaphore1.\n");
if(p(1,'d')) goto stop0;
d0: printf("process 0 is excecuting section1.\n");
if(timeint('e')) goto stop0;
e0: printf("seed=%d\n",seed);
s2=s2+1;
printf("s2=%d",s2);
printf("pcocess 0 calls V on semaphore1 and quit cretical section1.\n");
if(v(1,'f')) goto stop0;
f0: printf("process 0 cyclen count=%d\n",i+1);
i=i+1;
send(0,1,i); send(0,2,i);
printf("\n");
}
stop0:
if(i<5) goto end0;
eexit(0);
end0:;
}
void process1()
{
if(addr=='a') goto a1;
if(addr=='b') goto b1;
if(addr=='c') goto c1;
if(addr=='d') goto d1;
if(addr=='e') goto e1;
if(addr=='f') goto f1;
while(i<5) {
receive(1);
printf("process1 calls P on semaphore1.\n");
if(p(1,'a')) goto stop1;
a1: printf("process1 is executing on its cretical section1.\n");
if (timeint('b')) goto stop1;
b1: s2=s2+1;
printf("s2=%d",s2);
printf("process 1 calls V on semaphore1 and quit cretical section1.\n");
if(v(1,'c')) goto stop1;
c1: printf("process 1 calls P on semaphore0.\n");
if(p(0,'d')) goto stop1;
d1: printf("process 1 is excecuting section0.\n");
if(timeint('e')) goto stop1;
e1: s1=s1+1;
printf("s1=%d",s1);
printf("pcocess 1 calls V on semaphore0 and quit section0.\n");
if(v(0,'f')) goto stop1;
f1: printf("process 1 cyclen count=%d\n",i+1);
i=i+1;
send(1,0,i); send(1,2,i);
printf("\n");
}
stop1:
if(i<5) goto end1;
eexit(1);
end1:;
}
void process2()
{
if(addr=='a') goto a2;
if(addr=='b') goto b2;
if(addr=='c') goto c2;
while(i<5) {
receive(2);
printf("process 2 calls P on semaphore 1.\n");
if(p(2,'a')) goto stop2;
a2: printf("process 2 is executing on its cretical section 1.\n");
if (timeint('b')) goto stop2;
13b2: s2=s2+1;
printf("s2=%d",s2);
printf("process 2 calls V on semaphore2 and quit cretical section 0.\n");
if(v(2,'c')) goto stop2;
c2: printf("process 2 cyclen count=%d\n",i+1);
i=i+1;
send(2,0,i); send(2,1,i);
printf("\n");
}
stop2:
if(i<5) goto end2;
eexit(2);
end2:;
}
void main()
{
int k;
seed=0;
printf("C.O.S Example one");
printf("\n");
init();
printf("s1=%d,s2=%d\n",s1,s2);
printf("process1,process2,and process3 are all ready!\n");
lable:
k=scheduler();
if(k!=NIL)
switch (k)
{
case 0: process0();
goto lable;
case 1: printf("s1=%d,s2=%d\n",s1,s2);
process1();
goto lable;
case 2: printf("s1=%d,s2=%d\n",s1,s2);
process2();
goto lable;
default: printf("process identifer error.\n");
}
printf("\n");
printf("\n");
printf("s1=%d,s2=%d",s1,s2);
printf("\n");
printf("COMPLETED!\n");
getch();
}