-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.cpp
1198 lines (1198 loc) · 26.7 KB
/
test.cpp
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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*2017.12.25:实现读取文件、初始化工作、表格界面
2017.12.26:实现调度图、FCFS、PSA、PSA抢占式、HRRN、SJF、SRT
2017.12.27:实现时间片、多级反馈
2017.12.28:微调了用户界面、动态显示*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
#include"head.h"
//设置颜色
void setColor(unsigned short ForeColor=7,unsigned short BackGroundColor=0)
{
HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);//获取当前窗口句柄
SetConsoleTextAttribute(handle,ForeColor+BackGroundColor*0x10);//设置颜色
}
//设置XY的坐标
void setxy(int x, int y)
{
COORD coord = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
//返回光标x
int getX()
{
HANDLE gh_std_out;
gh_std_out = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO bInfo;
GetConsoleScreenBufferInfo(gh_std_out,&bInfo);
return bInfo.dwCursorPosition.X;
}
//返回光标y
int getY()
{
HANDLE gh_std_out;
gh_std_out = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO bInfo;
GetConsoleScreenBufferInfo(gh_std_out,&bInfo);
return bInfo.dwCursorPosition.Y;
}
//pcb复制,node复制到x
void copy_pcb(pcb * node,pcb * x)
{
x->arrivetime=node->arrivetime;
x->end=node->end;
x->name=node->name;
x->priority=node->priority;
x->pturn=node->pturn;
x->runtime=node->runtime;
x->start=node->start;
x->state=node->state;
x->turnaroundtime=node->turnaroundtime;
x->restime=node->restime;
x->remain=node->remain;
x->slice=node->slice;
}
//入队
void push(lqueuetp * lq,pcb * node)
{
pcb * x=(pcb *)malloc(sizeof(pcb));
copy_pcb(node,x);
x->next=NULL;
lq->rear->next=x;
lq->rear=x;
}
//初始化pcb函数
void init_pcb(pcb * p)
{
p->state='f';//未执行完状态
p->name=(char *)malloc(sizeof(char)*50);//为字符串申请空间
memset(p->name,'\0',sizeof(char)*50);
}
//手动输入函数
void manually_entered(int pro_num,pcb * head)
{
pcb * p=NULL,* tail;
int i;
tail=head;
for(i=0;i<pro_num;i++)
{
p=(pcb *)malloc(sizeof(pcb));
init_pcb(p);
printf("%d:输入进程名:",i);
scanf("%s",p->name);
//到达时间
printf("\t到达时间:");
scanf("%d",&p->arrivetime);
//运行时间
printf("\t运行时间:");
scanf("%d",&p->runtime);
//优先级
printf("\t优先级:");
scanf("%d",&p->priority);
p->remain=p->runtime;
p->start=-1;
tail->next=p;
p->next=NULL;
tail=p;
}
}
//读取文件
void read_file(int pro_num,pcb * head)
{
pcb * p=NULL,* tail;
tail=head;
int i;
char ch;
FILE * fp;
if((fp=fopen("process.txt","r"))==NULL)
{
printf("OPEN FAILED!\n");
return ;
}
for(i=0;i<pro_num;i++)
{
p=(pcb *)malloc(sizeof(pcb));
init_pcb(p);
//到达时间 运行时间 优先级
fscanf(fp,"%s %d %d %d",p->name,&p->arrivetime,&p->runtime,&p->priority);
//读入换行符
fscanf(fp,"%c",&ch);
p->remain=p->runtime;
p->start=-1;
tail->next=p;
p->next=NULL;
tail=p;
}
}
//释放申请的空间
void free_pcb(pcb * head)
{
pcb * tmp=NULL,* tmpnext=NULL;
tmp=head->next;
while(tmp)
{
tmpnext=tmp->next;
free(tmp);
tmp=tmpnext;
}
free(head);
}
//处理机调度算法菜单函数
int menu(pcb * head)
{
int n;
printf("\t\t\t\t\t\t\t****************处理机调度算法********************\n");
printf("\t\t\t\t\t\t\t\t\t1->先来先服务\n");
printf("\t\t\t\t\t\t\t\t\t2->短作业优先\n");
printf("\t\t\t\t\t\t\t\t\t3->最短剩余时间优先\n");
printf("\t\t\t\t\t\t\t\t\t4->时间片轮转\n");
printf("\t\t\t\t\t\t\t\t\t5->基于静态优先级的调度(非抢占式)\n");
printf("\t\t\t\t\t\t\t\t\t6->基于静态优先级的调度(抢占式)\n");
printf("\t\t\t\t\t\t\t\t\t7->基于高响应比优先的动态优先级调度\n");
printf("\t\t\t\t\t\t\t\t\t8->多级反馈队列调度等处理机调度算法的实现\n");
printf("\t\t\t\t\t\t\t\t\t0->退出\n");
printf("\t\t\t\t\t\t\t*************************************************\n");
simple_prin(head);
fflush(stdin);
printf("选择算法:");
scanf("%d",&n);
return n;
}
//简单的打印观察排序结果
void simple_prin(pcb * head)
{
pcb * tmp=NULL;
tmp=head->next;
printf("\t\t\t\t\t\t\t\t----------进程控制块-------------\n");
printf("\t\t\t\t\t\t\t\t进程名\t|到达时间|运行时间|优先级\n");
while(tmp!=NULL)
{
printf("\t\t\t\t\t\t\t\t---------------------------------\n");
printf("\t\t\t\t\t\t\t\t%4s\t|%4d\t |%4d\t |%4d\n",tmp->name,tmp->arrivetime,tmp->runtime,tmp->priority);
tmp=tmp->next;
}
printf("\t\t\t\t\t\t\t\t---------------------------------\n");
}
//交换pcb的值
void change_pcb(pcb * p1,pcb * p2)
{
double tmp_dou;
int tmp_int;
char * tmp_str,tmp_char;
tmp_str=(char *)malloc(sizeof(char)*50);
//交换到达时间
tmp_int=p1->arrivetime;
p1->arrivetime=p2->arrivetime;
p2->arrivetime=tmp_int;
//交换结束时间
tmp_int=p1->end;
p1->end=p2->end;
p2->end=tmp_int;
//交换优先级
tmp_int=p1->priority;
p1->priority=p2->priority;
p2->priority=tmp_int;
//交换运行时间
tmp_int=p1->runtime;
p1->runtime=p2->runtime;
p2->runtime=tmp_int;
//交换开始时间
tmp_int=p1->start;
p1->start=p2->start;
p2->start=tmp_int;
//交换周转时间
tmp_int=p1->turnaroundtime;
p1->turnaroundtime=p2->turnaroundtime;
p2->turnaroundtime=tmp_int;
//交换带权周转时间
tmp_dou=p1->pturn;
p1->pturn=p2->pturn;
p2->pturn=tmp_dou;
//交换状态
tmp_char=p1->state;
p1->state=p2->state;
p2->state=tmp_char;
//交换名字
tmp_str=p1->name;
p1->name=p2->name;
p2->name=tmp_str;
//交换响应时间
tmp_int=p1->restime;
p1->restime=p2->restime;
p2->restime=tmp_int;
//交换剩余时间
tmp_int=p1->remain;
p1->remain=p2->remain;
p2->remain=tmp_int;
//交换时间片
tmp_int=p1->slice;
p1->slice=p2->slice;
p2->slice=tmp_int;
}
//根据到达时间从小到大排序
void sort_by_arrivetime(pcb * head)
{
pcb * p,* q,* min;
p = head->next;
while(p!=NULL)
{
min=p;
q=p->next;
while(q!=NULL)
{
if(min->arrivetime>q->arrivetime)
min=q;
q=q->next;
}
// 交换值
if(min!=p)
{
change_pcb(min,p);
}
p=p->next;
}
}
//打印进程运行过程
void prin_head(pcb * head,int pro_num)
{
double turnall=0,pturnall=0;
pcb * tmp;
printf("\t\t\t\t\t\t---------------------------进程控制块-----------------------------\n");
printf("\t\t\t\t\t\t进程名\t|到达时间|开始时间|运行时间|结束时间|周转时间|带权周转时间\n");
tmp=head->next;
//队列不空
while(tmp!=NULL)
{
printf("\t\t\t\t\t\t------------------------------------------------------------------\n");
printf("\t\t\t\t\t\t%4s\t|%4d\t |%4d\t |%4d\t |%4d |%4d |%8.2lf\n",tmp->name,tmp->arrivetime,tmp->start,tmp->runtime,tmp->end,tmp->turnaroundtime,tmp->pturn);
turnall+=tmp->turnaroundtime;
pturnall+=tmp->pturn;
tmp=tmp->next;
}
printf("\t\t\t\t\t\t------------------------------------------------------------------\n");
//打印平均周转时间、平均带权周转时间
printf("\t\t\t\t\t\t\t\t平均周转时间为%.2lfs\n",turnall/pro_num);
printf("\t\t\t\t\t\t\t\t平均带权周转时间为%.2lfs\n",pturnall/pro_num);
printf("\t\t\t\t\t\t------------------------------------------------------------------\n");
}
//初始化矩阵,记录进程在各秒是否运行的情况
void init_matrix(int matrix[MAX][MAX],int pro_num)
{
int i,j;
for(i=0;i<pro_num;i++)
{
for(j=0;j<MAX;j++)
{
matrix[i][j]=0;
}
}
}
void prin_move(int matrix[][MAX],int pro_num,int endtime,pcb * head)
{
int i,j,p[MAX]={0},x,y,y0,flag;
pcb * tmp;
tmp=head->next;
for(i=0;i<endtime;i++)
{
printf("--------");
}
printf("\n ");
for(i=0;i<endtime;i++)
printf("| %3d\t",i+1);
printf("\n");
for(i=0;i<endtime;i++)
{
printf("--------");
}
printf("\n");
while(tmp)
{
printf("%s: ",tmp->name);
tmp=tmp->next;
printf("\n\n");
}
x=getX();
y=getY();
y0=y;
x=x+2;
setxy(x,y);
for(i=0;i<endtime;i++)
{
flag=0;
for(j=0;j<pro_num;j++)
{
if(matrix[j][i]==1)
{
x=getX();
y=y0-2*(pro_num-j);
setxy(x,y);
setColor(7,j+1);
printf("\t");
Sleep(100);
setColor(7,0);
flag=1;
break;
}
}
if(!flag)
printf("\t");
}
printf("\n\n\n");
for(i=0;i<endtime;i++)
{
printf("--------");
}
printf("\n");
}
//判断链表状态是否都是t
int all_t(pcb * head)
{
pcb * tmp;
tmp=head->next;
while(tmp!=NULL)
{
if(tmp->state!='t')
return 0;
tmp=tmp->next;
}
return 1;
}
//找到链表中等待态的最小作业时间进程
pcb * min_runtime_pcb(pcb * head)
{
int min=9999;
pcb * tmp=NULL,* min_tmp=NULL;
tmp=head->next;
while(tmp!=NULL)
{
if(tmp->runtime<min&&tmp->state=='w')
{
min=tmp->runtime;
min_tmp=tmp;
}
tmp=tmp->next;
}
return min_tmp;
}
//找到链表中等待态的最小作业时间进程
pcb * min_priority_pcb(pcb * head)
{
int min=9999;
pcb * tmp=NULL,* min_tmp=NULL;
tmp=head->next;
while(tmp!=NULL)
{
if(tmp->priority<min&&tmp->state=='w')
{
min=tmp->priority;
min_tmp=tmp;
}
tmp=tmp->next;
}
return min_tmp;
}
//找到链表中等待态的最短剩余时间的进程
pcb * min_remain_pcb(pcb * head)
{
int min=9999;
pcb * tmp=NULL,* min_tmp=NULL;
tmp=head->next;
while(tmp!=NULL)
{
if(tmp->remain<min&&tmp->state=='w')
{
min=tmp->remain;
min_tmp=tmp;
}
tmp=tmp->next;
}
return min_tmp;
}
//找到链表中等待态的最大响应比的进程
pcb * max_restime_pcb(pcb * head,int time)
{
int max=0;
pcb * tmp=NULL,* max_tmp=NULL;
tmp=head->next;
while(tmp!=NULL)
{
tmp->restime=(time-tmp->arrivetime+tmp->runtime)/tmp->runtime;
if(tmp->restime>max&&tmp->state=='w')
{
max=tmp->restime;
max_tmp=tmp;
}
tmp=tmp->next;
}
return max_tmp;
}
//最小时间片
int min_slice(pcb * head)
{
int min=9999;
pcb * tmp=NULL;
tmp=head->next;
while(tmp!=NULL)
{
if(tmp->slice<min&&tmp->state=='w')
{
min=tmp->slice;
}
tmp=tmp->next;
}
return min;
}
//找到链表中等待态的最小时间片中最小到达时间的进程
pcb * min_slice_arrivetime(pcb * head)
{
int min=9999,minslice;
pcb * tmp=NULL,* min_tmp=NULL;
tmp=head->next;
minslice=min_slice(head);
while(tmp!=NULL)
{
if(tmp->arrivetime<min&&tmp->state=='w'&&tmp->slice==minslice)
{
min=tmp->arrivetime;
min_tmp=tmp;
}
tmp=tmp->next;
}
return min_tmp;
}
//返回到达时间较小的进程
pcb * min_pcb(pcb * x1,pcb * x2)
{
if(x1==NULL)
return x2;
else if(x2==NULL)
return x1;
else if(x1->arrivetime<x2->arrivetime)
return x1;
else
return x2;
}
//名字转数字
int name_to_int(pcb * head,pcb * x)
{
int i=0;
pcb * tmp;
tmp=head->next;
while(tmp)
{
if(strcmp(tmp->name,x->name)==0)
return i;
tmp=tmp->next;
i++;
}
return 0;
}
//设置head都为f
void set_all_f(pcb * head)
{
pcb * tmp;
tmp=head->next;
while(tmp)
{
tmp->state='f';
tmp->start=-1;
tmp->remain=tmp->runtime;
tmp=tmp->next;
}
}
//先来先服务
void FCFS(pcb * head,int pro_num)
{
int time=0,i,j=0;
int proMatrix[MAX][MAX]={0};
pcb * tmp;
sort_by_arrivetime(head);
tmp=head->next;
while(tmp!=NULL)
{
if(tmp->arrivetime>time)
{
time++;
continue;
}
else
{
tmp->start=time;
tmp->end=tmp->start+tmp->runtime;
tmp->turnaroundtime=tmp->end-tmp->arrivetime;
tmp->pturn=(double)tmp->turnaroundtime/tmp->runtime;
tmp->state='t';
for(i=time;i<time+tmp->runtime;i++)
proMatrix[j][i]=1;
j++;
time+=tmp->runtime;
tmp=tmp->next;
}
}
printf("先来先服务\n");
prin_move(proMatrix,pro_num,time,head);
prin_head(head,pro_num);
}
//短作业优先
void SJF(pcb * head,int pro_num)
{
int time=0,i;
int proMatrix[MAX][MAX]={0};
pcb * tmp=NULL,* tmp2=NULL,* tmp3=NULL;
tmp=head->next;
tmp2=tmp;
//按到达时间升序排序
sort_by_arrivetime(head);
//所有的进程没有都执行完
while(!all_t(head))
{
//找到最小运行时间的进程
tmp3=min_runtime_pcb(head);
//如果当前时间未达到进程下一个进程的开始时间,则时间加一
if(min_pcb(tmp,tmp3)->arrivetime>time)
{
time++;
continue;
}
else if(tmp3!=NULL)
{
//运行等待态的进程
while(tmp3!=NULL)
{
tmp3->start=time;
tmp3->end=tmp3->start+tmp3->runtime;
tmp3->state='t';
tmp3->turnaroundtime=tmp3->end-tmp3->arrivetime;
tmp3->pturn=(double)tmp3->turnaroundtime/tmp3->runtime;
for(i=time;i<time+tmp3->runtime;i++)
proMatrix[name_to_int(head,tmp3)][i]=1;
time+=tmp3->runtime;
tmp2=tmp;
//运行过程中到达的进程入队
while(tmp2!=NULL && tmp2->arrivetime <=time && tmp2->state=='f')
{
tmp2->state='w';//记为等待态
tmp2=tmp2->next;
}
//找到最小运行时间的进程
tmp3=min_runtime_pcb(head);
}
tmp=tmp2;
}
else
{
tmp->start=time;
tmp->end=tmp->start+tmp->runtime;
tmp->state='t';
tmp->turnaroundtime=tmp->end-tmp->arrivetime;
tmp->pturn=(double)tmp->turnaroundtime/tmp->runtime;
for(i=time;i<time+tmp->runtime;i++)
proMatrix[name_to_int(head,tmp)][i]=1;
time+=tmp->runtime;
tmp2=tmp->next;
//运行过程中到达的进程入队
while(tmp2!=NULL && tmp2->arrivetime <=time && tmp2->state=='f')
{
tmp2->state='w';//记为等待态
tmp2=tmp2->next;
}
tmp=tmp2;
}
}
printf("短作业优先\n");
prin_move(proMatrix,pro_num,time,head);
prin_head(head,pro_num);
}
//最短剩余时间优先
void SRT(pcb * head,int pro_num)
{
int time=0;
int proMatrix[MAX][MAX]={0};
pcb * tmp=NULL,* tmp2=NULL,* tmp3=NULL;
tmp=head->next;
tmp2=tmp;
//按到达时间升序排序
sort_by_arrivetime(head);
//所有的进程没有都执行完
while(!all_t(head))
{
//找到最短剩余时间的进程
tmp3=min_remain_pcb(head);
//如果当前时间未达到进程下一个进程的开始时间,则时间加一
if(min_pcb(tmp,tmp3)->arrivetime>time)
{
time++;
continue;
}
else if(tmp3!=NULL)
{
//运行等待态的进程
while(tmp3!=NULL)
{
if(tmp3->state=='f' || tmp3->start==-1)
{
tmp3->start=time;
tmp3->state='w';
}
tmp3->remain-=1;
if(tmp3->remain==0)
{
tmp3->end=time+1;
tmp3->state='t';
tmp3->turnaroundtime=tmp3->end-tmp3->arrivetime;
tmp3->pturn=(double)tmp3->turnaroundtime/tmp3->runtime;
}
proMatrix[name_to_int(head,tmp3)][time]=1;
time++;
tmp2=tmp;
//运行过程中到达的进程入队
while(tmp2!=NULL)
{
if(tmp2->arrivetime <=time && tmp2->state=='f')
{
tmp2->state='w';//记为等待态
}
tmp2=tmp2->next;
}
//找到最短剩余时间的进程
tmp3=min_remain_pcb(head);
}
tmp=tmp2;
}
else
{
if(tmp->state=='f' || tmp->start==-1)
{
tmp->start=time;
tmp->state='w';
}
tmp->remain-=1;
if(tmp->remain==0)
{
tmp->end=time+1;
tmp->state='t';
tmp->turnaroundtime=tmp->end-tmp->arrivetime;
tmp->pturn=(double)tmp->turnaroundtime/tmp->runtime;
}
proMatrix[name_to_int(head,tmp)][time]=1;
time++;
tmp2=tmp->next;
//运行过程中到达的进程入队
while(tmp2!=NULL && tmp2->arrivetime <=time && tmp2->state=='f')
{
tmp2->state='w';//记为等待态
tmp2=tmp2->next;
}
tmp=tmp2;
}
}
printf("最短剩余时间优先\n");
prin_move(proMatrix,pro_num,time,head);
prin_head(head,pro_num);
}
//基于静态优先级的调度(抢占式)
void PSA_grab(pcb * head,int pro_num)
{
int time=0;
int proMatrix[MAX][MAX]={0};
pcb * tmp=NULL,* tmp2=NULL,* tmp3=NULL;
tmp=head->next;
tmp2=tmp;
//按到达时间升序排序
sort_by_arrivetime(head);
//所有的进程没有都执行完
while(!all_t(head))
{
//找到最大优先级的进程
tmp3=min_priority_pcb(head);
//如果当前时间未达到进程下一个进程的开始时间,则时间加一
if(min_pcb(tmp,tmp3)->arrivetime>time)
{
time++;
continue;
}
else if(tmp3!=NULL)
{
//运行等待态的进程
while(tmp3!=NULL)
{
if(tmp3->state=='f' || tmp3->start==-1)
{
tmp3->start=time;
tmp3->state='w';
}
tmp3->remain-=1;
if(tmp3->remain==0)
{
tmp3->end=time+1;
tmp3->state='t';
tmp3->turnaroundtime=tmp3->end-tmp3->arrivetime;
tmp3->pturn=(double)tmp3->turnaroundtime/tmp3->runtime;
}
proMatrix[name_to_int(head,tmp3)][time]=1;
time++;
tmp2=tmp;
//运行过程中到达的进程入队
while(tmp2!=NULL)
{
if(tmp2->arrivetime <=time && tmp2->state=='f')
{
tmp2->state='w';//记为等待态
}
tmp2=tmp2->next;
}
//找到最小运行时间的进程
tmp3=min_priority_pcb(head);
}
tmp=tmp2;
}
else
{
if(tmp->state=='f' || tmp->start==-1)
{
tmp->start=time;
tmp->state='w';
}
tmp->remain-=1;
if(tmp->remain==0)
{
tmp->end=time+1;
tmp->state='t';
tmp->turnaroundtime=tmp->end-tmp->arrivetime;
tmp->pturn=(double)tmp->turnaroundtime/tmp->runtime;
}
proMatrix[name_to_int(head,tmp)][time]=1;
time++;
tmp2=tmp->next;
//运行过程中到达的进程入队
while(tmp2!=NULL && tmp2->arrivetime <=time && tmp2->state=='f')
{
tmp2->state='w';//记为等待态
tmp2=tmp2->next;
}
tmp=tmp2;
}
}
printf("基于静态优先级的调度(抢占式)\n");
prin_move(proMatrix,pro_num,time,head);
prin_head(head,pro_num);
}
//基于静态优先级的调度(非抢占式)
void PSA(pcb * head,int pro_num)
{
int time=0,i;
int proMatrix[MAX][MAX]={0};
pcb * tmp=NULL,* tmp2=NULL,* tmp3=NULL;
tmp=head->next;
tmp2=tmp;
sort_by_arrivetime(head);
while(!all_t(head))
{
//找到最大优先级的进程
tmp3=min_priority_pcb(head);
if(min_pcb(tmp,tmp3)->arrivetime>time)
{
time++;
continue;
}
else if(tmp3!=NULL)
{
while(tmp3!=NULL)
{
tmp3->start=time;
tmp3->end=tmp3->start+tmp3->runtime;
tmp3->state='t';
tmp3->turnaroundtime=tmp3->end-tmp3->arrivetime;
tmp3->pturn=(double)tmp3->turnaroundtime/tmp3->runtime;
tmp3->remain=0;
for(i=time;i<time+tmp3->runtime;i++)
proMatrix[name_to_int(head,tmp3)][i]=1;
time+=tmp3->runtime;
tmp2=tmp;
while(tmp2!=NULL && tmp2->arrivetime <=time && tmp2->state=='f')
{
tmp2->state='w';//记为等待态
tmp2=tmp2->next;
}
//找到最大优先级的进程
tmp3=min_priority_pcb(head);
}
tmp=tmp2;
}
else
{
tmp->start=time;
tmp->end=tmp->start+tmp->runtime;
tmp->state='t';
tmp->turnaroundtime=tmp->end-tmp->arrivetime;
tmp->pturn=(double)tmp->turnaroundtime/tmp->runtime;
tmp->remain=0;
for(i=time;i<time+tmp->runtime;i++)
proMatrix[name_to_int(head,tmp)][i]=1;
time+=tmp->runtime;
tmp2=tmp->next;
while(tmp2!=NULL && tmp2->arrivetime <=time && tmp2->state=='f')
{
tmp2->state='w';//记为等待态
tmp2=tmp2->next;
}
tmp=tmp2;
}
}
printf("基于静态优先级的调度(非抢占式)\n");
prin_move(proMatrix,pro_num,time,head);
prin_head(head,pro_num);
}
//基于高响应比优先的动态优先级调度
void HRRN(pcb * head,int pro_num)
{
int time=0,i;
int proMatrix[MAX][MAX]={0};
pcb * tmp=NULL,* tmp2=NULL,* tmp3=NULL;
tmp=head->next;
tmp2=tmp;
sort_by_arrivetime(head);
while(!all_t(head))
{
//找到最高响应比的进程
tmp3=max_restime_pcb(head,time);
if(min_pcb(tmp,tmp3)->arrivetime>time)
{
time++;
continue;
}
else if(tmp3!=NULL)
{
while(tmp3!=NULL)
{
tmp3->start=time;
tmp3->end=tmp3->start+tmp3->runtime;
tmp3->state='t';
tmp3->turnaroundtime=tmp3->end-tmp3->arrivetime;
tmp3->pturn=(double)tmp3->turnaroundtime/tmp3->runtime;
tmp3->restime=1;
tmp3->remain=0;
for(i=time;i<time+tmp3->runtime;i++)
proMatrix[name_to_int(head,tmp3)][i]=1;
time+=tmp3->runtime;
tmp2=tmp;
while(tmp2!=NULL && tmp2->arrivetime <=time && tmp2->state=='f')
{
tmp2->state='w';//记为等待态
tmp2=tmp2->next;
}
//找到最高响应比的进程
tmp3=max_restime_pcb(head,time);
}
tmp=tmp2;
}
else
{
tmp->start=time;
tmp->end=tmp->start+tmp->runtime;
tmp->state='t';
tmp->turnaroundtime=tmp->end-tmp->arrivetime;
tmp->pturn=(double)tmp->turnaroundtime/tmp->runtime;
tmp->restime=1;
tmp->remain=0;
for(i=time;i<time+tmp->runtime;i++)
proMatrix[name_to_int(head,tmp)][i]=1;
time+=tmp->runtime;
tmp2=tmp->next;
while(tmp2!=NULL && tmp2->arrivetime <=time && tmp2->state=='f')
{
tmp2->state='w';//记为等待态
tmp2=tmp2->next;
}
tmp=tmp2;
}
}
printf("基于高响应比优先的动态优先级调度\n");
prin_move(proMatrix,pro_num,time,head);
prin_head(head,pro_num);
}
//时间片轮转
void TIMEROUND(pcb * head,int pro_num)
{
//定义变量
pcb * tmp,* tail,* pre,*sort_head,* q,* r,*final_head,* copy;
int time_slice,time,i;
int proMatrix[MAX][MAX]={0};
//初始化
sort_head=(pcb *)malloc(sizeof(pcb));
sort_head->next=head->next;
final_head=(pcb *)malloc(sizeof(pcb));
final_head->next=NULL;
copy=(pcb *)malloc(sizeof(pcb));
copy->next=NULL;
tail=sort_head;
//输入时间片长度
printf("时间片长度为:");
scanf("%d",&time_slice);
sort_by_arrivetime(head);
time=head->next->arrivetime;
tmp=head->next;
//备份head
while(tmp)
{
q=(pcb *)malloc(sizeof(pcb));
copy_pcb(tmp,q);
q->next=copy->next;
copy->next=q;
tmp=tmp->next;
}
sort_by_arrivetime(copy);
tmp=head->next;
//sort_head、head均不为空
while(sort_head->next!=NULL || head->next!=NULL)
{
tmp=head->next;
pre=head;
while(tmp!=NULL)
{
if(tmp->arrivetime>time)
{
tmp=tmp->next;
pre=pre->next;
}
else
{
q=tmp;
tmp=tmp->next;
pre->next=tmp;
tail->next=q;
q->next=NULL;
tail=q;
}
}
//提取sort_head的第一个元素,出队
r=sort_head->next;
sort_head->next=r->next;
r->next=NULL;
if(sort_head->next==NULL)
tail=sort_head;
//运行该元素
if(r->start==-1)
{
r->start=time;
r->state='w';
}
if(r->remain>time_slice)
{
r->remain-=time_slice;
for(i=time;i<time+time_slice;i++)
proMatrix[name_to_int(copy,r)][i]=1;
time+=time_slice;
}
else if(r->remain==time_slice)
{
r->remain=0;
for(i=time;i<time+time_slice;i++)
proMatrix[name_to_int(copy,r)][i]=1;
time+=time_slice;
r->end=time;
r->turnaroundtime=r->end-r->arrivetime;
r->pturn=(double)r->turnaroundtime/r->runtime;
r->state='t';
}
else
{