-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path实验一.c
604 lines (555 loc) · 14.9 KB
/
实验一.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
//#include <stdio.h>
//#include <stdlib.h>
//#define f 5
//struct PCB{
// int id;
// int priority;
// int cputime;
// int alltime;
// char state;
// int next;
//}pcb[f-1];
//
//int procnum;
//int rand();
//int algo;
//int run,head,tail,j;
//
//void print(){ //print the running process ,waiting queue and pcb sequence list
// int k,p;
// for(k = 1;k<=40;++k){
// printf("=");
// printf("\nrunning proc\n");
// printf("waiting queue");
// printf("\n %d ",pcb[run].id);
// p = head;
// while(p!=0){
// printf("%5d",p);
// p = pcb[p].next;
// }
// printf("\n");
// for(k = 1;k<=40;++k){
// printf("=");
// printf("\n");
// printf(" id ");
// for(k = 1;k<f+1;++k){
// printf("%5d",pcb[k].id);
// printf("\n");
// printf(" priority ");
// for(k = 1;k<f+1;++k){
// printf("%5d",pcb[k].priority);
// printf("\n");
// printf("alltime ");
// for(k = 1;k<f+1;++k){
// printf("%5d",pcb[k].alltime);
// printf("\n");
// printf("state ");
// for(k = 1;k<f+1;++k){
// printf("%5c",pcb[k].state);
// printf("\n");
// printf("next ");
// for(k=1;k<f+1;++k){
// printf("%5d",pcb[k].next);
// printf("\n");
// }
// }
// }
// }
//
// }
// }
//}
//}
//
//void insert(int q){ //insert a process
// int p,s;
// p = head;
// s = pcb[head].next;
// while((pcb[q].priority<pcb[s].priority)&&(s!=0)){
// p = s;
// s = pcb[s].next;
//}
// pcb[p].next = q;
// pcb[q].next = s;
//}
//void insert2(){ //put a process onto the tail of the queue
// pcb[tail].next = run;
// tail = run;
// pcb[run].next = 0;
//}
//
//void init(){ //create a waiting queue
// int i;
// head = 0;
// if(algo==2){
// for(i = 1;i<f+1;++i){
// pcb[i].id = i;
// pcb[i].priority = (rand()+11)%41;//0-40;
// pcb[i].cputime = 0;
// pcb[i].alltime = (rand()+1)%7; //0-7
// pcb[i].state = 'W';
// pcb[i].next = 0;
// if((pcb[i].priority<pcb[head].priority)&&(head!=0)){
// insert(pcb[i].id);
// }else{
// pcb[i].next = head;
// head = pcb[i].id;
// }
// }
// }else{
// for(i=1;i<f+1;++i){
// pcb[i].id = i;
// pcb[i].priority = (rand()+1)%3+1; //1-4
// pcb[i].cputime = 0;
// pcb[i].alltime = (rand()+1)%7;
// pcb[i].state = 'W';
// pcb[i].next = (i+1)%(f+1);
// }
// head = 1;
// tail = f;
// pcb[f].next = 0;
// }
// run = head;
// pcb[run].state = 'R';
// head = pcb[head].next;
// pcb[run].next = 0;
// print();
//}
//
// //Priority algorithm
//void prisch(){ //the process with prio algorithm
// while(run!=0){
// pcb[run].cputime += 1;
// pcb[run].priority -= 3;
// pcb[run].alltime -= 1;
// if(pcb[run].alltime==0){
// pcb[run].state = 'F';
// pcb[run].next = 0;
// if(head!=0){
// run = head;
// pcb[f].state = 'R';
// head = pcb[head].next;
// }else{
// pcb[0].id = pcb[run].id;
// run = 0;
// }
// }else if((pcb[run].priority < pcb[head].priority)&&(head!=0)){
// pcb[run].state = 'W';
// insert(run);
// run = head;
// pcb[run].state = 'R';
// head = pcb[head].next;
// }
// print();
// }
//}
//
//void timesch(){ //the process with RR algorithm
// while(run!=0){
// pcb[run].alltime -= 1;
// pcb[run].cputime += 1;
// if(pcb[run].alltime==0){
// pcb[run].state = 'F';
// pcb[run].next = 0;
// if(head!=0){
// run = head;
// pcb[run].state = 'R';
// head = pcb[head].next;
// }else{
// pcb[0].id = pcb[run].id;
// run = 0;
// }
// }else if((pcb[run].cputime==pcb[run].priority)&&(head!=0)){
// pcb[run].state = 'W';
// pcb[run].cputime = 0;
// insert2();
// run = head;
// pcb[run].state = 'R';
// head = pcb[head].next;
// }
// print();
// }
//}
//
//int main()
//{
// agan:printf("Type the algorithm is (1:时间片轮转算法 2:优先级调度算法):");
// scanf("%d",&algo);
// if(algo==2){
// printf("Output of priority.\n");
// init();
// prisch();
// }
// else if(algo==1){
//
// printf("Output of round robin.\n");
// init();
// timesch();
//
// }
// else{
// printf("Try again,please.\n");
// goto agan;
// }
// for(j=1;j<=40;++j){
// printf("=");
// }
// printf("\n\n");
// for(j=1;j<=40;++j){
// printf("=");
// }
// printf("\n\n");
// printf("system finished\n");
// getch();
// return 0;
//}
//
#include <stdio.h>
#include <stdlib.h>
struct node
{
int pid;//进程id号
int prio;//进程优先级
int runtime;//进程运行的时间
int alltime;//进程还需运行的时间
char state;//进程的状态 运行r,就绪j,阻塞b。
struct node *next;
};
typedef struct node Node;
typedef Node * Link;
void init(Link *head);
void create(Link head);
void menu(Link head);
void RR(Link head);
void FCFS(Link head);
Link gethighprio(Link head);
void PRIORITY(Link head);
void error();
void quit();
void save();
int num = 0;//进程的id号
void create(Link head)
{
Link p = head;
Link newnode;
//char ch;
newnode = (Link)malloc(sizeof(Node));
newnode->runtime = 0;
newnode->state = 'j';
newnode->pid = ++num;
newnode->prio = 2;
newnode->alltime = 2;
p = head;
while(p->next != NULL)
{
p = p->next;
}
newnode->next = p->next;
p->next = newnode;
newnode = (Link)malloc(sizeof(Node));
newnode->runtime = 0;
newnode->state = 'j';
newnode->pid = ++num;
newnode->prio = 3;
newnode->alltime = 3;
p = head;
while(p->next != NULL)
{
p = p->next;
}
newnode->next = p->next;
p->next = newnode;
newnode = (Link)malloc(sizeof(Node));
newnode->runtime = 0;
newnode->state = 'j';
newnode->pid = ++num;
newnode->prio = 4;
newnode->alltime = 4;
p = head;
while(p->next != NULL)
{
p = p->next;
}
newnode->next = p->next;
p->next = newnode;
newnode = (Link)malloc(sizeof(Node));
newnode->runtime = 0;
newnode->state = 'j';
newnode->pid = ++num;
newnode->prio = 5;
newnode->alltime = 5;
p = head;
while(p->next != NULL)
{
p = p->next;
}
newnode->next = p->next;
p->next = newnode;
//printf("请输入进程的优先级:");
// scanf("%d",&(newnode->prio));
// printf("请输入进程的总共需要运行的时间:") ;
// scanf("%d",&(newnode->alltime));
// getchar();
// printf("\n");
/*
printf("是否要继续创建进程:");
scanf("%c",&ch);
if(ch == 'y')
{
create(head);
}
else if(ch == 'n')
{
menu(head);
}
else
{
printf("您的输入有误!\n");
}*/
}
/*尾插入的形式调度算法*/
//void create(Link head)
//{
// Link p = head;
// Link newnode;
// char ch;
// newnode = (Link)malloc(sizeof(Node));
//
// printf("请输入进程的优先级:");
// scanf("%d",&(newnode->prio));
// printf("请输入进程的总共需要运行的时间:") ;
// scanf("%d",&(newnode->alltime));
//
// newnode->runtime = 0;
// newnode->state = 'j';
// newnode->pid = ++num;
//
// while(p->next != NULL)
// {
// p = p->next;
// }
// newnode->next = p->next;
// p->next = newnode;
// getchar();
// printf("\n");
// printf("是否要继续创建进程:");
// scanf("%c",&ch);
// if(ch == 'y')
// {
// create(head);
// }
// else if(ch == 'n')
// {
// menu(head);
// }
// else
// {
// printf("您的输入有误!\n");
// }
//}
//
//void save()
//{
//
//}
/*先来先服务调度算法*/
void FCFS(Link head)
{
Link old;
Link p = head->next;
if(p == NULL)
{
printf("队列里面没有进程,返回啦!\n");
menu(head);
}
p->state = 'r';
while(p != NULL && ((p->alltime) > 0))
{
p->runtime++;
p->alltime--;
printf("运行进程id号: %d 优先级:%d 进程运行时间: %d 进程还需运行时间 %d 进程状态:%c\n",p->pid,p->prio,p->runtime,p->alltime,p->state);
}
old = p;
head->next = p->next;
p = p->next;
free(old);
}
/*查找获得链表中优先级最高的进程,返回指向该进程的指针*/
Link gethighprio(Link head)
{
Link p = head->next;
Link q = p->next;
while(q != NULL)
{
if(p->prio < q->prio)
{
p = q;
}
q=q->next;
}
return p;
}
/*动态优先级调度算法*/
void PRIORITY(Link head)
{
Link q = head;
Link front = head;
Link old;
if(q->next == NULL)
{
printf("队列里面没有进程,返回啦!\n");
menu(head);
}
q = gethighprio(head);
q->state = 'r';
if(q->alltime > 0)
{
q->runtime++;
q->alltime--;
q->prio = (q->prio) - 1;
printf("运行的进程pid:%d 优先级:%d 进程运行时间: %d 进程还需运行时间 %d 进程状态:%c\n",q->pid,q->prio,q->runtime,q->alltime,q->state);
if(q->alltime == 0)
{
old = q;
if(q->next != NULL)
{
while(front->next != q)
{
front = front->next;
}
front->next = q->next;
q = NULL;
free(old);
}
else /*一开始程序问题出在这里,删除结点末尾没有=NULL,以后要注意*/
{
while(front->next != q)
{
front = front->next;
}
front->next = NULL;
q = NULL;
free(old);
}
}
PRIORITY(head);
}
}
/*时间片轮转调度算法*/
void RR(Link head)
{
Link p = head->next;
Link front = head;
Link old;
if(p == NULL)
{
printf("队列里面没有进程,返回啦!\n");
menu(head);
}
while(head->next != NULL)
{
front = head;
p->runtime++;
p->alltime--;
p->state = 'r';
printf("运行的进程pid:%d 优先级:%d 进程运行时间: %d 进程还需运行时间 %d 进程状态:%c\n",p->pid,p->prio,p->runtime,p->alltime,p->state);
if(p->alltime <= 0)
{
old = p;
if(p->next != NULL)
{
while(front->next != p)
{
front = front->next;
}
front->next = p->next;
p = p->next;
free(old);
}
else{
while(front->next != p)
{
front = front->next;
}
front->next = NULL;
p = NULL;
free(old);
old = NULL;
}
}
if((p != NULL) && (p->next != NULL))
{
p = p->next;
}
else{
p = head->next;
}
}
}
/*输入有误*/
void error()
{
printf("您的输入有误,退出啦!\n");
exit(1);
}
/*退出*/
void quit()
{
printf("Thank you for your using!\n");
printf(" 白白 \n");
exit(1);
}
void menu(Link head)
{
int choice;
while(1)
{
printf("\n");
printf("********************************************\n");
printf("***************进程调度*********************\n");
printf("* 1.创建进程 2.先来先服务算法 *\n");
printf("* 3.时间片轮转算法 4.优先级调度算法 *\n");
printf("* 5.退出 *\n");
printf("请输入您的选项(1~6):");
scanf("%d",&choice);
switch(choice)
{
case 1:{
create(head);
break;
}
case 2:{
FCFS(head);
break;
}
case 3:{
RR(head);
break;
}
case 4:{
PRIORITY(head);
break;
}
case 5:{
quit();
exit(1);
}
default:{
error();
break;
}
}
}
}
void init(Link *head)
{
*head = (Link)malloc(sizeof(Node));
(*head)->next = NULL;
}
int main()
{
Link head;
//Link newnode;
init(&head);
menu(head);
}