├── README.md ├── head.h ├── main.cpp ├── process.txt └── test.cpp /README.md: -------------------------------------------------------------------------------- 1 | # OS_course_project 2 | 操作系统课程设计-处理机的调度模拟实现 3 | + 2017.12.25:实现读取文件、初始化工作、表格界面 4 | + 2017.12.26:实现调度图、FCFS、PSA、PSA抢占式、HRRN、SJF、SRT 5 | + 2017.12.27:实现时间片、多级反馈 6 | + 2017.12.28:微调了用户界面、动态显示 7 | -------------------------------------------------------------------------------- /head.h: -------------------------------------------------------------------------------- 1 | #define MAX 100 2 | typedef struct pcb 3 | { 4 | int runtime;//运行时间 5 | char * name;//进程名 6 | int arrivetime;//到达时间 7 | int priority;//优先级 8 | int turnaroundtime;//周转时间 9 | double pturn;//带权周转时间 10 | int start;//开始时间 11 | int end;//结束时间 12 | char state;//状态 13 | int restime;//响应时间 14 | int remain;//剩余时间 15 | int slice;//时间片 16 | int remain_slice;//剩余时间片 17 | struct pcb * next;//下一个结点 18 | }pcb; 19 | //队列结构体 20 | typedef struct 21 | { 22 | pcb * front; 23 | pcb * rear; 24 | }lqueuetp; 25 | void init_pcb(pcb * p);//初始化pcb函数 26 | void run();//最终运行函数 27 | void manually_entered(int pro_num,pcb * head);//手动输入函数 28 | void read_file(int pro_num,pcb * head);//读取文件 29 | void free_pcb(pcb * head);//释放申请的空间 30 | int menu(pcb * head);//处理机调度算法菜单函数 31 | void FCFS(pcb * head);//先来先服务 32 | void change_pcb(pcb * p1,pcb * p2);//交换pcb的值 33 | void sort_by_arrivetime(pcb * head);//根据到达时间从小大到排序 34 | void prin_head(pcb * head,int pro_num);//打印进程运行过程 35 | void simple_prin(pcb * head);//简单的打印观察排序结果 36 | void SJF(pcb * head,int pro_num);//短作业优先 37 | void copy_pcb(pcb * node,pcb * x);//pcb复制,node复制到x 38 | void set_all_f(pcb * head);//设置head都为f 39 | void HRRN(pcb * head,int pro_num);//基于高响应比优先的动态优先级调度 40 | void PSA(pcb * head,int pro_num);//基于静态优先级的调度(非抢占式) 41 | void PSA_grab(pcb * head,int pro_num);//基于静态优先级的调度(抢占式) 42 | void SRT(pcb * head,int pro_num);//最短剩余时间优先 43 | int name_to_int(pcb * head,pcb * x);//名字转数字 44 | pcb * min_pcb(pcb * x1,pcb * x2);//返回到达时间较小的进程 45 | pcb * max_restime_pcb(pcb * head,int time);//找到链表中等待态的最大响应比的进程 46 | pcb * min_remain_pcb(pcb * head);//找到链表中等待态的最短剩余时间的进程 47 | pcb * min_priority_pcb(pcb * head);//找到链表中等待态的最小作业时间进程 48 | pcb * min_runtime_pcb(pcb * head);//找到链表中等待态的最小作业时间进程 49 | pcb * min_slice_arrivetime(pcb * head);//找到链表中等待态的最小时间片中最小到达时间的进程 50 | int min_slice(pcb * head);//最小时间片 51 | int all_t(pcb * head);//判断链表状态是否都是t 52 | void init_matrix(int matrix[MAX][MAX],int pro_num);//初始化矩阵,记录进程在各秒是否运行的情况 53 | void prin_matrix(int matrix[][MAX],int pro_num,int endtime,pcb * head);//打印矩阵 54 | void TIMEROUND(pcb * head,int pro_num);//时间片轮转 55 | void FEEDBACK(pcb * head,int pro_num);//多级反馈队列 56 | 57 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include"head.h" 4 | //主函数 5 | int main(void) 6 | { 7 | run(); 8 | return 0; 9 | } 10 | // -------------------------------------------------------------------------------- /process.txt: -------------------------------------------------------------------------------- 1 | A 0 3 3 2 | B 2 6 1 3 | C 4 4 2 4 | D 6 5 4 5 | E 8 2 6 6 | F 10 6 5 7 | G 12 2 8 8 | H 14 3 7 9 | I 16 3 10 10 | J 18 4 9 -------------------------------------------------------------------------------- /test.cpp: -------------------------------------------------------------------------------- 1 | /*2017.12.25:实现读取文件、初始化工作、表格界面 2 | 2017.12.26:实现调度图、FCFS、PSA、PSA抢占式、HRRN、SJF、SRT 3 | 2017.12.27:实现时间片、多级反馈 4 | 2017.12.28:微调了用户界面、动态显示*/ 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include"head.h" 10 | //设置颜色 11 | void setColor(unsigned short ForeColor=7,unsigned short BackGroundColor=0) 12 | { 13 | HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);//获取当前窗口句柄 14 | SetConsoleTextAttribute(handle,ForeColor+BackGroundColor*0x10);//设置颜色 15 | } 16 | //设置XY的坐标 17 | void setxy(int x, int y) 18 | { 19 | COORD coord = {x, y}; 20 | SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); 21 | } 22 | //返回光标x 23 | int getX() 24 | { 25 | HANDLE gh_std_out; 26 | gh_std_out = GetStdHandle(STD_OUTPUT_HANDLE); 27 | CONSOLE_SCREEN_BUFFER_INFO bInfo; 28 | GetConsoleScreenBufferInfo(gh_std_out,&bInfo); 29 | return bInfo.dwCursorPosition.X; 30 | } 31 | //返回光标y 32 | int getY() 33 | { 34 | HANDLE gh_std_out; 35 | gh_std_out = GetStdHandle(STD_OUTPUT_HANDLE); 36 | CONSOLE_SCREEN_BUFFER_INFO bInfo; 37 | GetConsoleScreenBufferInfo(gh_std_out,&bInfo); 38 | return bInfo.dwCursorPosition.Y; 39 | } 40 | //pcb复制,node复制到x 41 | void copy_pcb(pcb * node,pcb * x) 42 | { 43 | x->arrivetime=node->arrivetime; 44 | x->end=node->end; 45 | x->name=node->name; 46 | x->priority=node->priority; 47 | x->pturn=node->pturn; 48 | x->runtime=node->runtime; 49 | x->start=node->start; 50 | x->state=node->state; 51 | x->turnaroundtime=node->turnaroundtime; 52 | x->restime=node->restime; 53 | x->remain=node->remain; 54 | x->slice=node->slice; 55 | } 56 | //入队 57 | void push(lqueuetp * lq,pcb * node) 58 | { 59 | pcb * x=(pcb *)malloc(sizeof(pcb)); 60 | copy_pcb(node,x); 61 | x->next=NULL; 62 | lq->rear->next=x; 63 | lq->rear=x; 64 | } 65 | //初始化pcb函数 66 | void init_pcb(pcb * p) 67 | { 68 | p->state='f';//未执行完状态 69 | p->name=(char *)malloc(sizeof(char)*50);//为字符串申请空间 70 | memset(p->name,'\0',sizeof(char)*50); 71 | } 72 | //手动输入函数 73 | void manually_entered(int pro_num,pcb * head) 74 | { 75 | pcb * p=NULL,* tail; 76 | int i; 77 | tail=head; 78 | for(i=0;iname); 84 | //到达时间 85 | printf("\t到达时间:"); 86 | scanf("%d",&p->arrivetime); 87 | //运行时间 88 | printf("\t运行时间:"); 89 | scanf("%d",&p->runtime); 90 | //优先级 91 | printf("\t优先级:"); 92 | scanf("%d",&p->priority); 93 | p->remain=p->runtime; 94 | p->start=-1; 95 | tail->next=p; 96 | p->next=NULL; 97 | tail=p; 98 | } 99 | } 100 | //读取文件 101 | void read_file(int pro_num,pcb * head) 102 | { 103 | pcb * p=NULL,* tail; 104 | tail=head; 105 | int i; 106 | char ch; 107 | FILE * fp; 108 | if((fp=fopen("process.txt","r"))==NULL) 109 | { 110 | printf("OPEN FAILED!\n"); 111 | return ; 112 | } 113 | for(i=0;iname,&p->arrivetime,&p->runtime,&p->priority); 119 | //读入换行符 120 | fscanf(fp,"%c",&ch); 121 | p->remain=p->runtime; 122 | p->start=-1; 123 | tail->next=p; 124 | p->next=NULL; 125 | tail=p; 126 | } 127 | } 128 | //释放申请的空间 129 | void free_pcb(pcb * head) 130 | { 131 | pcb * tmp=NULL,* tmpnext=NULL; 132 | tmp=head->next; 133 | while(tmp) 134 | { 135 | tmpnext=tmp->next; 136 | free(tmp); 137 | tmp=tmpnext; 138 | } 139 | free(head); 140 | } 141 | //处理机调度算法菜单函数 142 | int menu(pcb * head) 143 | { 144 | int n; 145 | printf("\t\t\t\t\t\t\t****************处理机调度算法********************\n"); 146 | printf("\t\t\t\t\t\t\t\t\t1->先来先服务\n"); 147 | printf("\t\t\t\t\t\t\t\t\t2->短作业优先\n"); 148 | printf("\t\t\t\t\t\t\t\t\t3->最短剩余时间优先\n"); 149 | printf("\t\t\t\t\t\t\t\t\t4->时间片轮转\n"); 150 | printf("\t\t\t\t\t\t\t\t\t5->基于静态优先级的调度(非抢占式)\n"); 151 | printf("\t\t\t\t\t\t\t\t\t6->基于静态优先级的调度(抢占式)\n"); 152 | printf("\t\t\t\t\t\t\t\t\t7->基于高响应比优先的动态优先级调度\n"); 153 | printf("\t\t\t\t\t\t\t\t\t8->多级反馈队列调度等处理机调度算法的实现\n"); 154 | printf("\t\t\t\t\t\t\t\t\t0->退出\n"); 155 | printf("\t\t\t\t\t\t\t*************************************************\n"); 156 | simple_prin(head); 157 | fflush(stdin); 158 | printf("选择算法:"); 159 | scanf("%d",&n); 160 | return n; 161 | } 162 | //简单的打印观察排序结果 163 | void simple_prin(pcb * head) 164 | { 165 | pcb * tmp=NULL; 166 | tmp=head->next; 167 | printf("\t\t\t\t\t\t\t\t----------进程控制块-------------\n"); 168 | printf("\t\t\t\t\t\t\t\t进程名\t|到达时间|运行时间|优先级\n"); 169 | while(tmp!=NULL) 170 | { 171 | printf("\t\t\t\t\t\t\t\t---------------------------------\n"); 172 | 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); 173 | tmp=tmp->next; 174 | } 175 | printf("\t\t\t\t\t\t\t\t---------------------------------\n"); 176 | } 177 | //交换pcb的值 178 | void change_pcb(pcb * p1,pcb * p2) 179 | { 180 | double tmp_dou; 181 | int tmp_int; 182 | char * tmp_str,tmp_char; 183 | tmp_str=(char *)malloc(sizeof(char)*50); 184 | //交换到达时间 185 | tmp_int=p1->arrivetime; 186 | p1->arrivetime=p2->arrivetime; 187 | p2->arrivetime=tmp_int; 188 | //交换结束时间 189 | tmp_int=p1->end; 190 | p1->end=p2->end; 191 | p2->end=tmp_int; 192 | //交换优先级 193 | tmp_int=p1->priority; 194 | p1->priority=p2->priority; 195 | p2->priority=tmp_int; 196 | //交换运行时间 197 | tmp_int=p1->runtime; 198 | p1->runtime=p2->runtime; 199 | p2->runtime=tmp_int; 200 | //交换开始时间 201 | tmp_int=p1->start; 202 | p1->start=p2->start; 203 | p2->start=tmp_int; 204 | //交换周转时间 205 | tmp_int=p1->turnaroundtime; 206 | p1->turnaroundtime=p2->turnaroundtime; 207 | p2->turnaroundtime=tmp_int; 208 | //交换带权周转时间 209 | tmp_dou=p1->pturn; 210 | p1->pturn=p2->pturn; 211 | p2->pturn=tmp_dou; 212 | //交换状态 213 | tmp_char=p1->state; 214 | p1->state=p2->state; 215 | p2->state=tmp_char; 216 | //交换名字 217 | tmp_str=p1->name; 218 | p1->name=p2->name; 219 | p2->name=tmp_str; 220 | //交换响应时间 221 | tmp_int=p1->restime; 222 | p1->restime=p2->restime; 223 | p2->restime=tmp_int; 224 | //交换剩余时间 225 | tmp_int=p1->remain; 226 | p1->remain=p2->remain; 227 | p2->remain=tmp_int; 228 | //交换时间片 229 | tmp_int=p1->slice; 230 | p1->slice=p2->slice; 231 | p2->slice=tmp_int; 232 | } 233 | //根据到达时间从小到大排序 234 | void sort_by_arrivetime(pcb * head) 235 | { 236 | pcb * p,* q,* min; 237 | p = head->next; 238 | while(p!=NULL) 239 | { 240 | min=p; 241 | q=p->next; 242 | while(q!=NULL) 243 | { 244 | if(min->arrivetime>q->arrivetime) 245 | min=q; 246 | q=q->next; 247 | } 248 | // 交换值 249 | if(min!=p) 250 | { 251 | change_pcb(min,p); 252 | } 253 | p=p->next; 254 | } 255 | } 256 | //打印进程运行过程 257 | void prin_head(pcb * head,int pro_num) 258 | { 259 | double turnall=0,pturnall=0; 260 | pcb * tmp; 261 | printf("\t\t\t\t\t\t---------------------------进程控制块-----------------------------\n"); 262 | printf("\t\t\t\t\t\t进程名\t|到达时间|开始时间|运行时间|结束时间|周转时间|带权周转时间\n"); 263 | tmp=head->next; 264 | //队列不空 265 | while(tmp!=NULL) 266 | { 267 | printf("\t\t\t\t\t\t------------------------------------------------------------------\n"); 268 | 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); 269 | turnall+=tmp->turnaroundtime; 270 | pturnall+=tmp->pturn; 271 | tmp=tmp->next; 272 | } 273 | printf("\t\t\t\t\t\t------------------------------------------------------------------\n"); 274 | //打印平均周转时间、平均带权周转时间 275 | printf("\t\t\t\t\t\t\t\t平均周转时间为%.2lfs\n",turnall/pro_num); 276 | printf("\t\t\t\t\t\t\t\t平均带权周转时间为%.2lfs\n",pturnall/pro_num); 277 | printf("\t\t\t\t\t\t------------------------------------------------------------------\n"); 278 | } 279 | //初始化矩阵,记录进程在各秒是否运行的情况 280 | void init_matrix(int matrix[MAX][MAX],int pro_num) 281 | { 282 | int i,j; 283 | for(i=0;inext; 296 | for(i=0;iname); 312 | tmp=tmp->next; 313 | printf("\n\n"); 314 | } 315 | x=getX(); 316 | y=getY(); 317 | y0=y; 318 | x=x+2; 319 | setxy(x,y); 320 | for(i=0;inext; 353 | while(tmp!=NULL) 354 | { 355 | if(tmp->state!='t') 356 | return 0; 357 | tmp=tmp->next; 358 | } 359 | return 1; 360 | } 361 | //找到链表中等待态的最小作业时间进程 362 | pcb * min_runtime_pcb(pcb * head) 363 | { 364 | int min=9999; 365 | pcb * tmp=NULL,* min_tmp=NULL; 366 | tmp=head->next; 367 | while(tmp!=NULL) 368 | { 369 | if(tmp->runtimestate=='w') 370 | { 371 | min=tmp->runtime; 372 | min_tmp=tmp; 373 | } 374 | tmp=tmp->next; 375 | } 376 | return min_tmp; 377 | } 378 | //找到链表中等待态的最小作业时间进程 379 | pcb * min_priority_pcb(pcb * head) 380 | { 381 | int min=9999; 382 | pcb * tmp=NULL,* min_tmp=NULL; 383 | tmp=head->next; 384 | while(tmp!=NULL) 385 | { 386 | if(tmp->prioritystate=='w') 387 | { 388 | min=tmp->priority; 389 | min_tmp=tmp; 390 | } 391 | tmp=tmp->next; 392 | } 393 | return min_tmp; 394 | } 395 | //找到链表中等待态的最短剩余时间的进程 396 | pcb * min_remain_pcb(pcb * head) 397 | { 398 | int min=9999; 399 | pcb * tmp=NULL,* min_tmp=NULL; 400 | tmp=head->next; 401 | while(tmp!=NULL) 402 | { 403 | if(tmp->remainstate=='w') 404 | { 405 | min=tmp->remain; 406 | min_tmp=tmp; 407 | } 408 | tmp=tmp->next; 409 | } 410 | return min_tmp; 411 | } 412 | //找到链表中等待态的最大响应比的进程 413 | pcb * max_restime_pcb(pcb * head,int time) 414 | { 415 | int max=0; 416 | pcb * tmp=NULL,* max_tmp=NULL; 417 | tmp=head->next; 418 | while(tmp!=NULL) 419 | { 420 | tmp->restime=(time-tmp->arrivetime+tmp->runtime)/tmp->runtime; 421 | if(tmp->restime>max&&tmp->state=='w') 422 | { 423 | max=tmp->restime; 424 | max_tmp=tmp; 425 | } 426 | tmp=tmp->next; 427 | } 428 | return max_tmp; 429 | } 430 | //最小时间片 431 | int min_slice(pcb * head) 432 | { 433 | int min=9999; 434 | pcb * tmp=NULL; 435 | tmp=head->next; 436 | while(tmp!=NULL) 437 | { 438 | if(tmp->slicestate=='w') 439 | { 440 | min=tmp->slice; 441 | } 442 | tmp=tmp->next; 443 | } 444 | return min; 445 | } 446 | //找到链表中等待态的最小时间片中最小到达时间的进程 447 | pcb * min_slice_arrivetime(pcb * head) 448 | { 449 | int min=9999,minslice; 450 | pcb * tmp=NULL,* min_tmp=NULL; 451 | tmp=head->next; 452 | minslice=min_slice(head); 453 | while(tmp!=NULL) 454 | { 455 | if(tmp->arrivetimestate=='w'&&tmp->slice==minslice) 456 | { 457 | min=tmp->arrivetime; 458 | min_tmp=tmp; 459 | } 460 | tmp=tmp->next; 461 | } 462 | return min_tmp; 463 | } 464 | //返回到达时间较小的进程 465 | pcb * min_pcb(pcb * x1,pcb * x2) 466 | { 467 | if(x1==NULL) 468 | return x2; 469 | else if(x2==NULL) 470 | return x1; 471 | else if(x1->arrivetimearrivetime) 472 | return x1; 473 | else 474 | return x2; 475 | } 476 | //名字转数字 477 | int name_to_int(pcb * head,pcb * x) 478 | { 479 | int i=0; 480 | pcb * tmp; 481 | tmp=head->next; 482 | while(tmp) 483 | { 484 | if(strcmp(tmp->name,x->name)==0) 485 | return i; 486 | tmp=tmp->next; 487 | i++; 488 | } 489 | return 0; 490 | } 491 | //设置head都为f 492 | void set_all_f(pcb * head) 493 | { 494 | pcb * tmp; 495 | tmp=head->next; 496 | while(tmp) 497 | { 498 | tmp->state='f'; 499 | tmp->start=-1; 500 | tmp->remain=tmp->runtime; 501 | tmp=tmp->next; 502 | } 503 | } 504 | //先来先服务 505 | void FCFS(pcb * head,int pro_num) 506 | { 507 | int time=0,i,j=0; 508 | int proMatrix[MAX][MAX]={0}; 509 | pcb * tmp; 510 | sort_by_arrivetime(head); 511 | tmp=head->next; 512 | while(tmp!=NULL) 513 | { 514 | if(tmp->arrivetime>time) 515 | { 516 | time++; 517 | continue; 518 | } 519 | else 520 | { 521 | tmp->start=time; 522 | tmp->end=tmp->start+tmp->runtime; 523 | tmp->turnaroundtime=tmp->end-tmp->arrivetime; 524 | tmp->pturn=(double)tmp->turnaroundtime/tmp->runtime; 525 | tmp->state='t'; 526 | for(i=time;iruntime;i++) 527 | proMatrix[j][i]=1; 528 | j++; 529 | time+=tmp->runtime; 530 | tmp=tmp->next; 531 | } 532 | } 533 | printf("先来先服务\n"); 534 | prin_move(proMatrix,pro_num,time,head); 535 | prin_head(head,pro_num); 536 | } 537 | //短作业优先 538 | void SJF(pcb * head,int pro_num) 539 | { 540 | int time=0,i; 541 | int proMatrix[MAX][MAX]={0}; 542 | pcb * tmp=NULL,* tmp2=NULL,* tmp3=NULL; 543 | tmp=head->next; 544 | tmp2=tmp; 545 | //按到达时间升序排序 546 | sort_by_arrivetime(head); 547 | //所有的进程没有都执行完 548 | while(!all_t(head)) 549 | { 550 | //找到最小运行时间的进程 551 | tmp3=min_runtime_pcb(head); 552 | //如果当前时间未达到进程下一个进程的开始时间,则时间加一 553 | if(min_pcb(tmp,tmp3)->arrivetime>time) 554 | { 555 | time++; 556 | continue; 557 | } 558 | else if(tmp3!=NULL) 559 | { 560 | //运行等待态的进程 561 | while(tmp3!=NULL) 562 | { 563 | tmp3->start=time; 564 | tmp3->end=tmp3->start+tmp3->runtime; 565 | tmp3->state='t'; 566 | tmp3->turnaroundtime=tmp3->end-tmp3->arrivetime; 567 | tmp3->pturn=(double)tmp3->turnaroundtime/tmp3->runtime; 568 | for(i=time;iruntime;i++) 569 | proMatrix[name_to_int(head,tmp3)][i]=1; 570 | time+=tmp3->runtime; 571 | tmp2=tmp; 572 | //运行过程中到达的进程入队 573 | while(tmp2!=NULL && tmp2->arrivetime <=time && tmp2->state=='f') 574 | { 575 | tmp2->state='w';//记为等待态 576 | tmp2=tmp2->next; 577 | } 578 | //找到最小运行时间的进程 579 | tmp3=min_runtime_pcb(head); 580 | } 581 | tmp=tmp2; 582 | } 583 | else 584 | { 585 | tmp->start=time; 586 | tmp->end=tmp->start+tmp->runtime; 587 | tmp->state='t'; 588 | tmp->turnaroundtime=tmp->end-tmp->arrivetime; 589 | tmp->pturn=(double)tmp->turnaroundtime/tmp->runtime; 590 | for(i=time;iruntime;i++) 591 | proMatrix[name_to_int(head,tmp)][i]=1; 592 | time+=tmp->runtime; 593 | tmp2=tmp->next; 594 | //运行过程中到达的进程入队 595 | while(tmp2!=NULL && tmp2->arrivetime <=time && tmp2->state=='f') 596 | { 597 | tmp2->state='w';//记为等待态 598 | tmp2=tmp2->next; 599 | } 600 | tmp=tmp2; 601 | } 602 | } 603 | printf("短作业优先\n"); 604 | prin_move(proMatrix,pro_num,time,head); 605 | prin_head(head,pro_num); 606 | } 607 | //最短剩余时间优先 608 | void SRT(pcb * head,int pro_num) 609 | { 610 | int time=0; 611 | int proMatrix[MAX][MAX]={0}; 612 | pcb * tmp=NULL,* tmp2=NULL,* tmp3=NULL; 613 | tmp=head->next; 614 | tmp2=tmp; 615 | //按到达时间升序排序 616 | sort_by_arrivetime(head); 617 | //所有的进程没有都执行完 618 | while(!all_t(head)) 619 | { 620 | //找到最短剩余时间的进程 621 | tmp3=min_remain_pcb(head); 622 | //如果当前时间未达到进程下一个进程的开始时间,则时间加一 623 | if(min_pcb(tmp,tmp3)->arrivetime>time) 624 | { 625 | time++; 626 | continue; 627 | } 628 | else if(tmp3!=NULL) 629 | { 630 | //运行等待态的进程 631 | while(tmp3!=NULL) 632 | { 633 | if(tmp3->state=='f' || tmp3->start==-1) 634 | { 635 | tmp3->start=time; 636 | tmp3->state='w'; 637 | } 638 | tmp3->remain-=1; 639 | if(tmp3->remain==0) 640 | { 641 | tmp3->end=time+1; 642 | tmp3->state='t'; 643 | tmp3->turnaroundtime=tmp3->end-tmp3->arrivetime; 644 | tmp3->pturn=(double)tmp3->turnaroundtime/tmp3->runtime; 645 | } 646 | proMatrix[name_to_int(head,tmp3)][time]=1; 647 | time++; 648 | tmp2=tmp; 649 | //运行过程中到达的进程入队 650 | while(tmp2!=NULL) 651 | { 652 | if(tmp2->arrivetime <=time && tmp2->state=='f') 653 | { 654 | tmp2->state='w';//记为等待态 655 | } 656 | tmp2=tmp2->next; 657 | } 658 | //找到最短剩余时间的进程 659 | tmp3=min_remain_pcb(head); 660 | } 661 | tmp=tmp2; 662 | } 663 | else 664 | { 665 | if(tmp->state=='f' || tmp->start==-1) 666 | { 667 | tmp->start=time; 668 | tmp->state='w'; 669 | } 670 | tmp->remain-=1; 671 | if(tmp->remain==0) 672 | { 673 | tmp->end=time+1; 674 | tmp->state='t'; 675 | tmp->turnaroundtime=tmp->end-tmp->arrivetime; 676 | tmp->pturn=(double)tmp->turnaroundtime/tmp->runtime; 677 | } 678 | proMatrix[name_to_int(head,tmp)][time]=1; 679 | time++; 680 | tmp2=tmp->next; 681 | //运行过程中到达的进程入队 682 | while(tmp2!=NULL && tmp2->arrivetime <=time && tmp2->state=='f') 683 | { 684 | tmp2->state='w';//记为等待态 685 | tmp2=tmp2->next; 686 | } 687 | tmp=tmp2; 688 | } 689 | } 690 | printf("最短剩余时间优先\n"); 691 | prin_move(proMatrix,pro_num,time,head); 692 | prin_head(head,pro_num); 693 | } 694 | //基于静态优先级的调度(抢占式) 695 | void PSA_grab(pcb * head,int pro_num) 696 | { 697 | int time=0; 698 | int proMatrix[MAX][MAX]={0}; 699 | pcb * tmp=NULL,* tmp2=NULL,* tmp3=NULL; 700 | tmp=head->next; 701 | tmp2=tmp; 702 | //按到达时间升序排序 703 | sort_by_arrivetime(head); 704 | //所有的进程没有都执行完 705 | while(!all_t(head)) 706 | { 707 | //找到最大优先级的进程 708 | tmp3=min_priority_pcb(head); 709 | //如果当前时间未达到进程下一个进程的开始时间,则时间加一 710 | if(min_pcb(tmp,tmp3)->arrivetime>time) 711 | { 712 | time++; 713 | continue; 714 | } 715 | else if(tmp3!=NULL) 716 | { 717 | //运行等待态的进程 718 | while(tmp3!=NULL) 719 | { 720 | if(tmp3->state=='f' || tmp3->start==-1) 721 | { 722 | tmp3->start=time; 723 | tmp3->state='w'; 724 | } 725 | tmp3->remain-=1; 726 | if(tmp3->remain==0) 727 | { 728 | tmp3->end=time+1; 729 | tmp3->state='t'; 730 | tmp3->turnaroundtime=tmp3->end-tmp3->arrivetime; 731 | tmp3->pturn=(double)tmp3->turnaroundtime/tmp3->runtime; 732 | } 733 | proMatrix[name_to_int(head,tmp3)][time]=1; 734 | time++; 735 | tmp2=tmp; 736 | //运行过程中到达的进程入队 737 | while(tmp2!=NULL) 738 | { 739 | if(tmp2->arrivetime <=time && tmp2->state=='f') 740 | { 741 | tmp2->state='w';//记为等待态 742 | } 743 | tmp2=tmp2->next; 744 | } 745 | //找到最小运行时间的进程 746 | tmp3=min_priority_pcb(head); 747 | } 748 | tmp=tmp2; 749 | } 750 | else 751 | { 752 | if(tmp->state=='f' || tmp->start==-1) 753 | { 754 | tmp->start=time; 755 | tmp->state='w'; 756 | } 757 | tmp->remain-=1; 758 | if(tmp->remain==0) 759 | { 760 | tmp->end=time+1; 761 | tmp->state='t'; 762 | tmp->turnaroundtime=tmp->end-tmp->arrivetime; 763 | tmp->pturn=(double)tmp->turnaroundtime/tmp->runtime; 764 | } 765 | proMatrix[name_to_int(head,tmp)][time]=1; 766 | time++; 767 | tmp2=tmp->next; 768 | //运行过程中到达的进程入队 769 | while(tmp2!=NULL && tmp2->arrivetime <=time && tmp2->state=='f') 770 | { 771 | tmp2->state='w';//记为等待态 772 | tmp2=tmp2->next; 773 | } 774 | tmp=tmp2; 775 | } 776 | } 777 | printf("基于静态优先级的调度(抢占式)\n"); 778 | prin_move(proMatrix,pro_num,time,head); 779 | prin_head(head,pro_num); 780 | } 781 | //基于静态优先级的调度(非抢占式) 782 | void PSA(pcb * head,int pro_num) 783 | { 784 | int time=0,i; 785 | int proMatrix[MAX][MAX]={0}; 786 | pcb * tmp=NULL,* tmp2=NULL,* tmp3=NULL; 787 | tmp=head->next; 788 | tmp2=tmp; 789 | sort_by_arrivetime(head); 790 | while(!all_t(head)) 791 | { 792 | //找到最大优先级的进程 793 | tmp3=min_priority_pcb(head); 794 | if(min_pcb(tmp,tmp3)->arrivetime>time) 795 | { 796 | time++; 797 | continue; 798 | } 799 | else if(tmp3!=NULL) 800 | { 801 | while(tmp3!=NULL) 802 | { 803 | tmp3->start=time; 804 | tmp3->end=tmp3->start+tmp3->runtime; 805 | tmp3->state='t'; 806 | tmp3->turnaroundtime=tmp3->end-tmp3->arrivetime; 807 | tmp3->pturn=(double)tmp3->turnaroundtime/tmp3->runtime; 808 | tmp3->remain=0; 809 | for(i=time;iruntime;i++) 810 | proMatrix[name_to_int(head,tmp3)][i]=1; 811 | time+=tmp3->runtime; 812 | tmp2=tmp; 813 | while(tmp2!=NULL && tmp2->arrivetime <=time && tmp2->state=='f') 814 | { 815 | tmp2->state='w';//记为等待态 816 | tmp2=tmp2->next; 817 | } 818 | //找到最大优先级的进程 819 | tmp3=min_priority_pcb(head); 820 | } 821 | tmp=tmp2; 822 | } 823 | else 824 | { 825 | tmp->start=time; 826 | tmp->end=tmp->start+tmp->runtime; 827 | tmp->state='t'; 828 | tmp->turnaroundtime=tmp->end-tmp->arrivetime; 829 | tmp->pturn=(double)tmp->turnaroundtime/tmp->runtime; 830 | tmp->remain=0; 831 | for(i=time;iruntime;i++) 832 | proMatrix[name_to_int(head,tmp)][i]=1; 833 | time+=tmp->runtime; 834 | tmp2=tmp->next; 835 | while(tmp2!=NULL && tmp2->arrivetime <=time && tmp2->state=='f') 836 | { 837 | tmp2->state='w';//记为等待态 838 | tmp2=tmp2->next; 839 | } 840 | tmp=tmp2; 841 | } 842 | } 843 | printf("基于静态优先级的调度(非抢占式)\n"); 844 | prin_move(proMatrix,pro_num,time,head); 845 | prin_head(head,pro_num); 846 | } 847 | //基于高响应比优先的动态优先级调度 848 | void HRRN(pcb * head,int pro_num) 849 | { 850 | int time=0,i; 851 | int proMatrix[MAX][MAX]={0}; 852 | pcb * tmp=NULL,* tmp2=NULL,* tmp3=NULL; 853 | tmp=head->next; 854 | tmp2=tmp; 855 | sort_by_arrivetime(head); 856 | while(!all_t(head)) 857 | { 858 | //找到最高响应比的进程 859 | tmp3=max_restime_pcb(head,time); 860 | if(min_pcb(tmp,tmp3)->arrivetime>time) 861 | { 862 | time++; 863 | continue; 864 | } 865 | else if(tmp3!=NULL) 866 | { 867 | while(tmp3!=NULL) 868 | { 869 | tmp3->start=time; 870 | tmp3->end=tmp3->start+tmp3->runtime; 871 | tmp3->state='t'; 872 | tmp3->turnaroundtime=tmp3->end-tmp3->arrivetime; 873 | tmp3->pturn=(double)tmp3->turnaroundtime/tmp3->runtime; 874 | tmp3->restime=1; 875 | tmp3->remain=0; 876 | for(i=time;iruntime;i++) 877 | proMatrix[name_to_int(head,tmp3)][i]=1; 878 | time+=tmp3->runtime; 879 | tmp2=tmp; 880 | while(tmp2!=NULL && tmp2->arrivetime <=time && tmp2->state=='f') 881 | { 882 | tmp2->state='w';//记为等待态 883 | tmp2=tmp2->next; 884 | } 885 | //找到最高响应比的进程 886 | tmp3=max_restime_pcb(head,time); 887 | } 888 | tmp=tmp2; 889 | } 890 | else 891 | { 892 | tmp->start=time; 893 | tmp->end=tmp->start+tmp->runtime; 894 | tmp->state='t'; 895 | tmp->turnaroundtime=tmp->end-tmp->arrivetime; 896 | tmp->pturn=(double)tmp->turnaroundtime/tmp->runtime; 897 | tmp->restime=1; 898 | tmp->remain=0; 899 | for(i=time;iruntime;i++) 900 | proMatrix[name_to_int(head,tmp)][i]=1; 901 | time+=tmp->runtime; 902 | tmp2=tmp->next; 903 | while(tmp2!=NULL && tmp2->arrivetime <=time && tmp2->state=='f') 904 | { 905 | tmp2->state='w';//记为等待态 906 | tmp2=tmp2->next; 907 | } 908 | tmp=tmp2; 909 | } 910 | } 911 | printf("基于高响应比优先的动态优先级调度\n"); 912 | prin_move(proMatrix,pro_num,time,head); 913 | prin_head(head,pro_num); 914 | } 915 | //时间片轮转 916 | void TIMEROUND(pcb * head,int pro_num) 917 | { 918 | //定义变量 919 | pcb * tmp,* tail,* pre,*sort_head,* q,* r,*final_head,* copy; 920 | int time_slice,time,i; 921 | int proMatrix[MAX][MAX]={0}; 922 | //初始化 923 | sort_head=(pcb *)malloc(sizeof(pcb)); 924 | sort_head->next=head->next; 925 | final_head=(pcb *)malloc(sizeof(pcb)); 926 | final_head->next=NULL; 927 | copy=(pcb *)malloc(sizeof(pcb)); 928 | copy->next=NULL; 929 | tail=sort_head; 930 | //输入时间片长度 931 | printf("时间片长度为:"); 932 | scanf("%d",&time_slice); 933 | sort_by_arrivetime(head); 934 | time=head->next->arrivetime; 935 | tmp=head->next; 936 | //备份head 937 | while(tmp) 938 | { 939 | q=(pcb *)malloc(sizeof(pcb)); 940 | copy_pcb(tmp,q); 941 | q->next=copy->next; 942 | copy->next=q; 943 | tmp=tmp->next; 944 | } 945 | sort_by_arrivetime(copy); 946 | tmp=head->next; 947 | //sort_head、head均不为空 948 | while(sort_head->next!=NULL || head->next!=NULL) 949 | { 950 | tmp=head->next; 951 | pre=head; 952 | while(tmp!=NULL) 953 | { 954 | if(tmp->arrivetime>time) 955 | { 956 | tmp=tmp->next; 957 | pre=pre->next; 958 | } 959 | else 960 | { 961 | q=tmp; 962 | tmp=tmp->next; 963 | pre->next=tmp; 964 | tail->next=q; 965 | q->next=NULL; 966 | tail=q; 967 | } 968 | } 969 | //提取sort_head的第一个元素,出队 970 | r=sort_head->next; 971 | sort_head->next=r->next; 972 | r->next=NULL; 973 | if(sort_head->next==NULL) 974 | tail=sort_head; 975 | //运行该元素 976 | if(r->start==-1) 977 | { 978 | r->start=time; 979 | r->state='w'; 980 | } 981 | if(r->remain>time_slice) 982 | { 983 | r->remain-=time_slice; 984 | for(i=time;iremain==time_slice) 989 | { 990 | r->remain=0; 991 | for(i=time;iend=time; 995 | r->turnaroundtime=r->end-r->arrivetime; 996 | r->pturn=(double)r->turnaroundtime/r->runtime; 997 | r->state='t'; 998 | } 999 | else 1000 | { 1001 | for(i=time;iremain;i++) 1002 | proMatrix[name_to_int(copy,r)][i]=1; 1003 | time+=r->remain; 1004 | r->remain=0; 1005 | r->end=time; 1006 | r->turnaroundtime=r->end-r->arrivetime; 1007 | r->pturn=(double)r->turnaroundtime/r->runtime; 1008 | r->state='t'; 1009 | } 1010 | //在运行时间内到达的元素入队 1011 | tmp=head->next; 1012 | pre=head; 1013 | while(tmp!=NULL) 1014 | { 1015 | if(tmp->arrivetime>time) 1016 | { 1017 | tmp=tmp->next; 1018 | pre=pre->next; 1019 | } 1020 | else 1021 | { 1022 | q=tmp; 1023 | tmp=tmp->next; 1024 | pre->next=tmp; 1025 | tail->next=q; 1026 | q->next=NULL; 1027 | tail=q; 1028 | } 1029 | } 1030 | //r继续入队 1031 | if(r->remain==0) 1032 | { 1033 | r->next=final_head->next; 1034 | final_head->next=r; 1035 | } 1036 | else 1037 | { 1038 | tail->next=r; 1039 | r->next=NULL; 1040 | tail=r; 1041 | } 1042 | } 1043 | printf("时间片轮转\n"); 1044 | head=final_head; 1045 | sort_by_arrivetime(head); 1046 | prin_move(proMatrix,pro_num,time,head); 1047 | prin_head(head,pro_num); 1048 | } 1049 | //多级反馈队列 1050 | void FEEDBACK(pcb * head,int pro_num) 1051 | { 1052 | int time=0,time_slice; 1053 | int proMatrix[MAX][MAX]={0}; 1054 | pcb * tmp=NULL,* tmp2=NULL,* tmp3=NULL; 1055 | tmp=head->next; 1056 | tmp2=tmp; 1057 | printf("输入第一个队列的时间片\n"); 1058 | fflush(stdin); 1059 | scanf("%d",&time_slice); 1060 | //按到达时间升序排序 1061 | sort_by_arrivetime(head); 1062 | //所有的进程没有都执行完 1063 | while(!all_t(head)) 1064 | { 1065 | //找到最大优先级的进程 1066 | tmp3=min_slice_arrivetime(head); 1067 | //如果当前时间未达到进程下一个进程的开始时间,则时间加一 1068 | if(min_pcb(tmp,tmp3)->arrivetime>time) 1069 | { 1070 | time++; 1071 | continue; 1072 | } 1073 | else if(tmp3!=NULL) 1074 | { 1075 | //运行等待态的进程 1076 | while(tmp3!=NULL) 1077 | { 1078 | if(tmp3->state=='f' || tmp3->start==-1) 1079 | { 1080 | tmp3->start=time; 1081 | tmp3->state='w'; 1082 | tmp3->slice=time_slice; 1083 | tmp3->remain_slice=tmp3->slice; 1084 | } 1085 | tmp3->remain-=1; 1086 | tmp3->remain_slice-=1; 1087 | if(tmp3->remain==0) 1088 | { 1089 | tmp3->end=time+1; 1090 | tmp3->state='t'; 1091 | tmp3->turnaroundtime=tmp3->end-tmp3->arrivetime; 1092 | tmp3->pturn=(double)tmp3->turnaroundtime/tmp3->runtime; 1093 | } 1094 | if(tmp3->remain_slice==0) 1095 | { 1096 | tmp3->slice*=2; 1097 | tmp3->remain_slice=tmp3->slice; 1098 | } 1099 | proMatrix[name_to_int(head,tmp3)][time]=1; 1100 | time++; 1101 | tmp2=tmp; 1102 | //运行过程中到达的进程入队 1103 | while(tmp2!=NULL) 1104 | { 1105 | if(tmp2->arrivetime <=time && tmp2->state=='f') 1106 | { 1107 | tmp2->state='w';//记为等待态 1108 | } 1109 | tmp2=tmp2->next; 1110 | } 1111 | //找到最小运行时间的进程 1112 | tmp3=min_slice_arrivetime(head); 1113 | } 1114 | tmp=tmp2; 1115 | } 1116 | else 1117 | { 1118 | if(tmp->state=='f' || tmp->start==-1) 1119 | { 1120 | tmp->start=time; 1121 | tmp->state='w'; 1122 | tmp->slice=time_slice; 1123 | tmp->remain_slice=tmp->slice; 1124 | } 1125 | tmp->remain-=1; 1126 | if(tmp->remain==0) 1127 | { 1128 | tmp->end=time+1; 1129 | tmp->state='t'; 1130 | tmp->turnaroundtime=tmp->end-tmp->arrivetime; 1131 | tmp->pturn=(double)tmp->turnaroundtime/tmp->runtime; 1132 | } 1133 | if(tmp->remain_slice==0) 1134 | { 1135 | tmp->slice*=2; 1136 | tmp->remain_slice=tmp->slice; 1137 | } 1138 | proMatrix[name_to_int(head,tmp)][time]=1; 1139 | time++; 1140 | tmp2=tmp->next; 1141 | //运行过程中到达的进程入队 1142 | while(tmp2!=NULL && tmp2->arrivetime <=time && tmp2->state=='f') 1143 | { 1144 | tmp2->state='w';//记为等待态 1145 | tmp2=tmp2->next; 1146 | } 1147 | tmp=tmp2; 1148 | } 1149 | } 1150 | printf("多级反馈队列调度\n"); 1151 | prin_move(proMatrix,pro_num,time,head); 1152 | prin_head(head,pro_num); 1153 | } 1154 | //最终运行函数 1155 | void run() 1156 | { 1157 | pcb * head=NULL; 1158 | int pro_num,i=1; 1159 | head=(pcb *)malloc(sizeof(pcb)); 1160 | head->next=NULL; 1161 | while(i) 1162 | { 1163 | printf("请输入进程的个数\n"); 1164 | fflush(stdin); 1165 | scanf("%d",&pro_num); 1166 | printf("\t\t\t\t\t\t\t********************数据源************************\n"); 1167 | printf("\t\t\t\t\t\t\t\t\t1->读取文件\n"); 1168 | printf("\t\t\t\t\t\t\t\t\t2->手动输入\n"); 1169 | printf("\t\t\t\t\t\t\t\t\t0->退出\n"); 1170 | printf("\t\t\t\t\t\t\t**************************************************\n"); 1171 | fflush(stdin); 1172 | scanf("%d",&i); 1173 | switch(i) 1174 | { 1175 | case 1:read_file(pro_num,head);break;//读取文件 1176 | case 2:manually_entered(pro_num,head);break;//手动输入 1177 | case 0:break;//退出 1178 | } 1179 | while(i) 1180 | { 1181 | i=menu(head); 1182 | switch(i) 1183 | { 1184 | case 1:set_all_f(head);FCFS(head,pro_num);break;//先来先服务 1185 | case 2:set_all_f(head);SJF(head,pro_num);break;//短作业优先 1186 | case 3:set_all_f(head);SRT(head,pro_num);break;//最短剩余时间优先 1187 | case 4:set_all_f(head);TIMEROUND(head,pro_num);read_file(pro_num,head);break;//时间片轮转 1188 | case 5:set_all_f(head);PSA(head,pro_num);break;//基于静态优先级的调度(非抢占式) 1189 | case 6:set_all_f(head);PSA_grab(head,pro_num);break;//基于静态优先级的调度(抢占式) 1190 | case 7:set_all_f(head);HRRN(head,pro_num);break;//基于高响应比优先的动态优先级调度 1191 | case 8:set_all_f(head);FEEDBACK(head,pro_num);break;//多级反馈队列 1192 | case 0:break;//退出 1193 | default:break;//退出 1194 | } 1195 | } 1196 | } 1197 | free_pcb(head); 1198 | } --------------------------------------------------------------------------------