优先级法、多级反馈轮转法_进程调度模拟设计(编辑修改稿)内容摘要:

H__ include include include include include include int main(int argc, char *argv[]) { char mand。 srand( (unsigned)time(NULL) )。 restart()。 while((mand = get_mand()) != 39。 039。 )。 } //接收用户选择并转入相应的操作 char get_mand() { char c。 printf(\n[%d 个进程正在进行中 , %s, %s,如果遇到问题,请按 39。 639。 查询 ]\n 请选择将要进行的操作 :\n,pre_list_size,algo_name,oper_name)。 c = _getch()。 switch(c) { case 39。 139。 :add_process()。 break。 case 39。 239。 :switch_algorithm()。 break。 case 39。 339。 : if(algorithm == 0) start_scheduling()。 else start_scheduling_rrmf()。 break。 case 39。 439。 :calculate_time_costs()。 break。 case 39。 539。 :view_list(pre_list)。 break。 case 39。 639。 :help_menu()。 break。 case 39。 739。 :restart()。 break。 case 39。 839。 :man_auto()。 break。 } return c。 } // 手动 /自动 切换 void man_auto() { if(manual == 0) { printf(切换为 39。 自动模式 39。 \n)。 strcpy(oper_name, Automatic)。 manual = 1。 } else { printf(切换为 39。 手动模式 39。 \n)。 strcpy(oper_name, Manual)。 manual = 0。 } } //显示帮助菜单 void help_menu() { printf( === Process Scheduler Simulator ===\n \n (目前算法为 : %s)\n \n 、平均带权周转时间 \n \n \n \n 、自动切换 (目前模式为 : %s)\n \n,algo_name,oper_name )。 } // 添加 node 于 head 所指队列的队尾 void append(struct PCB **head, struct PCB **node) { struct PCB *p。 //(*node)next=NULL。 if(*head==NULL){ *head=*node。 return。 } else{ p=*head。 while(pnext!=NULL) p=pnext。 pnext=*node。 } } //添加进程 void add_process() { int counterpart, i。 struct PCB *tmp。 struct PCB *p = malloc( sizeof(struct PCB) )。 printf(请输入进程名称 :)。 scanf(%s, pname)。 if(algorithm == 0) // SRR 线性优先级调度 ppriority = 0。 //优先级为 0,表明使用 SRR else // RRMF 多级反馈轮转法 ppriority = PRIORITY1。 puse_slices = USE_SLICES1。 // RRMF 专用,表示占用的时间片 parrive_time = 2 * add_idx + rand() % 1。 pused_run_time = 0。 psleep_time = 1。 pfinish_time = 1。 pnext = NULL。 printf(请输入需要的时间片数 :)。 scanf(%d, amp。 prun_time)。 // 需要的时间片数 append(amp。 pre_list, amp。 p)。 add_idx ++。 // 第几轮添加进程 pre_list_size ++。 // 是否随机增加几个同时刻的进程 printf(请输入同时刻的进程数 :)。 scanf(%d,amp。 counterpart)。 if(counterpart0) { for(i = 0。 icounterpart。 i++) { tmp = malloc( sizeof(struct PCB) )。 sprintf(tmpname, %s_%d, pname, i)。 tmppriority = ppriority。 tmparrive_time = parrive_time。 tmprun_time = prun_time。 tmpused_run_time = 0。 tmpuse_slices = puse_slices。 // RRMF 专用 tmpsleep_time = 1。 tmpfinish_time = 1。 tmpnext = NULL。 append(amp。 pre_list, amp。 tmp)。 } pre_list_size += counterpart。 } else counterpart = 0。 printf(成功增加 %d 个进程。 \n, 1+counterpart)。 } // 查看队列中内容 void view_list(struct PCB *list) { struct PCB *p。 if(list == NULL) printf( Empty \n)。 else { printf(\n)。 p = list。 while(p != NULL) { printf( )。 show_process(p)。 //显示进程内容 printf(\n)。 p = pnext。 } } } //显示进程内容 void show_process(struct PCB *node) { printf([%6s 优先级 :%3d 到达时间 :%3d 需要的时间片数 :%3d 已经用过的时间片数 :%3d 进程阻塞耗时 :%3d 完成时间 :%3d 每次占用 CPU 消耗的时间片数 :%3d 下一个 :%5s] , nodename, nodepriority, nodearrive_time, noderun_time, nodeused_run_time, nodesleep_time, nodefinish_time,nodeuse_slices, nodenext==NULL?NULL:nodenextname)。 } // 切换调度算法(线性优先级法,多级反馈轮转法) void switch_algorithm() { struct PCB *tmp。 if(algorithm == 0) { // 调度算法标记 , 0SRR 1RRMF 默认为 0 printf(切换至 39。 多级反馈轮转算法 39。 \n)。 strcpy(algo_name, RRMF)。 algorithm = 1。 // SSR 状态下添加的进程的 Priority 默认为 0,改为 PRIORITY1 tmp = pre_list。 while(tmp != NULL) { tmppriority = PRIORITY1。 tmp = tmpnext。 } } else { printf(切换至 39。 线性优先级算法 39。 \n)。 strcpy(algo_name, SSR)。 algorithm = 0。 } } // 计算并显示平均周转时间,平均带权周转时间 void calculate_time_costs() { struct PCB *p。 float sum = , weighted_sum=。 int num = 0。 // 统计 PCB 个数 if(finished_list == NULL) printf(完成队列为空 !\n)。 else { p = finished_list。 // 完成队列 while(p != NULL) { sum += (pfinish_time parrive_time) *。 weighted_sum += (pfinish_time parrive_time) * / prun_time。 num ++。
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。 用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。