学 号 | 姓 名 | 时 间 | |||
专 业 | 班 级 | ||||
实验题目:处理机调度算法 | |||||
一、实验目的 在了解操作系统的基础上全面了解处理机调度算法的实现以及过程,详细了解处理机调度算法的机制,充分了解调度的过程及状态,采用优先数法进程调度算法来模拟演示进程调度 | |||||
二、实验内容与步骤: 1. 了解进程的三种状态状态:ready、running、finish 2.了解进程需要的CPU时间以时间片为单位确定 3.编写一段程序 #include <stdio.h> #include <stdlib.h> #define P_NUM 5 #define P_TIME 50 enum state{ ready, execute, block, finish }; struct pcbb{ char name[4]; int priority; int cputime; int needtime; int count; enum state process; struct pcbb *next; }; typedef struct pcbb pcb; void display_menu(){ printf("CHOOSE THE ALGORITHM:\n"); printf("1 PRIORITY\n"); printf("2 ROUNDROBIN\n"); printf("3 EXIT\n"); } pcb* get_process(){ pcb *q; pcb *p; pcb *t; int i = 0; printf("input name and time\n"); while (i < P_NUM){ q=(pcb *)malloc(sizeof(pcb)); scanf("%s",q->name); scanf("%d",&q->needtime); q->cputime = 0; q->priority = P_TIME - q->needtime; q->process = ready; q->next = NULL; if(i==0){ p = q; t = q; } else{ t->next = q; t = q; } i++; } return p; } void free_process(pcb *p){ pcb *q; while(p!= NULL){ q = p; p = p->next; free(q); } } void display(pcb *p){ printf("name cputime needtime priority state\n"); while(p){ printf("%s",p->name); printf(" "); printf("%d",p->cputime); printf(" "); printf("%d",p->needtime); printf(" "); printf("%d",p->priority); printf(" "); switch(p->process){ case ready:printf("ready\n");break; case execute:printf("execute\n"); break;读书的故事 case block:printf("block\n"); break; case finish:printf("finish\n"); break; } p = p->next; } } int process_finish(pcb *q){ int b1 = 1; while(b1&&q){ b1 = b1&&q->needtime==0; q = q->next; } return b1;刘湛秋 } void cpuexe(pcb *q){ pcb *t = q; int tp = 0; while(q){ if (q->process!=finish){ q->process = ready; if(q->needtime==0){ q->process = finish; } } if(tp<q->priority&&q->process!=finish){ tp = q->priority; t = q; } q = q->next; } if(t->needtime!=0){ t->priority -=3; t->needtime --; t->process = execute; t->cputime++; } } void priority_cal(){ pcb *p; p = get_process(); int cpu = 0; while(!process_finish(p)){ cpu++; printf("cputime:%d\n",cpu); cpuexe(p); display(p); sleep(2); } free_process(p); printf("All processes have finished\n"); } pcb *get_process_round(){ pcb *q; pcb *p; pcb *t; int i = 0; printf("input name and time\n"); while (i<P_NUM){ q=(pcb *)malloc(sizeof(pcb)); scanf("%s",q->name); scanf("%d",&q->needtime); q->cputime = 0; q->count = 0; q->process = ready; q->next = NULL; if(i==0){ p = q; t = q; } else{ t->next = q; t = q; } i++; } return p; } void cpu_round(pcb *q){ if(q->needtime==1) q->cputime++; else q->cputime +=2; 蒋欣 叶祖新 q->needtime -=2; if(q->needtime<0){ q->needtime = 0; } q->count++; q->process = execute; } pcb *get_next(pcb *k,pcb *head){ pcb *t; t = k; do{ t =t->next; }while ( t && t->process == finish); if(t == NULL){ t = head; while(t->next!=k && t->process == finish){ t = t->next; } } return t;} void set_state(pcb *p){ while(p){ if(p->needtime == 0){ p->process = finish; } if(p->process == execute){ p->process = ready; } p = p->next; }} void display_round(pcb *p){ printf("name cputime needtime count state\n"); while(p){ printf("%s",p->name); printf(" "); printf("%d",p->cputime); printf(" "); printf("%d",p->needtime); printf(" "); printf("%d",p->count); printf(" "); switch(p->process){ case ready:printf("ready\n");break; case execute:printf("execute\n"); break; case block:printf("block\n"); break; case finish:printf("finish\n"); break; } p = p->next; }} void round_cal(){ pcb *p; pcb *r; p = get_process_round(); int cpu = 0; r=p; while(!process_finish(p)){ if(r->needtime==1) cpu+=1; else cpu+=2; cpu_round(r); r = get_next(r,p); printf("cputime:%d\n",cpu); display_round(p); set_state(p); sleep(2); } free_process(p); } main(){ display_menu(); int k; scanf("%d",&k); switch(k){ case 1:priority_cal();break; case 2:round_cal();break; case 3:break; default:printf("YOU HAVE NOT CHOOSE ANY ALGORITHM!\n"); } } 运行后结果如下: [root@rhel5hbzy ~]# gcc -o chuliji chuliji.c 工商银行营业时间[root@rhel5hbzy ~]# ./m CHOOSE THE ALGORITHM: 1 PRIORITY 2 ROUNDROBIN 3 EXIT 1 input name and time jing 2 aaaa 8 bbbb5 ffff4 ggg 6 cputime:1 houhuiname cputime needtime priority state jing- 1 1 45 execute aaaa* 0 8 42 ready bbbb2 0 0 50 finish ffff2 0 0 50 finish ggg 0 6 44 ready rtyucputime:2 name cputime needtime priority state jing* 2 0 42 execute aaaa* 0 8 42 ready bbbb2 0 0 50 finish ffff2 0 0 50 finish ggg 0 6 44 ready 5cputime:3 name cputime needtime priority state jing* 2 0 42 finish aaaa* 0 8 42 ready bbbb2 0 0 50 finish ffff2 0 0 50 finish ggg 1 5 41 execute 2cputime:4 name cputime needtime priority state jing* 2 0 42 finish aaaa' 1 7 39 execute bbbb2 0 0 50 finish ffff2 0 0 50 finish ggg 1 5 41 ready cputime:5 name cputime needtime priority state jing* 2 0 42 finish aaaa' 1 7 39 ready bbbb2 0 0 50 finish ffff2 0 0 50 finish ggg 2 4 38 execute cputime:6 name cputime needtime priority state jing* 2 0 42 finish aaaa$ 2 6 36 execute bbbb2 0 0 50 finish ffff2 0 0 50 finish ggg 2 4 38 ready cputime:7 name cputime needtime priority state jing* 2 0 42 finish aaaa$ 2 6 36 ready bbbb2 0 0 50 finish ffff2 0 0 50 finish ggg 3 3 35 execute cputime:8 name cputime needtime priority state jing* 2 0 42 finish aaaa! 3 5 33 execute bbbb2 0 0 50 finish ffff2 0 0 50 finish ggg 3 3 35 ready cputime:9 name cputime needtime priority state jing* 2 0 42 finish aaaa! 3 5 33 ready bbbb2 0 0 50 finish ffff2 0 0 50 finish ggg 4 2 32 execute cputime:10 name cputime needtime priority state jing* 2 0 42 finish aaaa 4 4 30 execute bbbb2 0 0 50 finish ffff2 0 0 50 finish ggg 4 2 32 ready cputime:11 name cputime needtime priority state jing* 2 0 42 finish aaaa 4 4 30 ready bbbb2 0 0 50 finish ffff2 0 0 50 finish ggg 5 1 29 execute cputime:12 name cputime needtime priority state jing* 2 0 42 finish aaaa 5 3 27 execute bbbb2 0 0 50 finish ffff2 0 0 50 finish ggg 5 1 29 ready cputime:13李香凝的老公 name cputime needtime priority state jing* 2 0 42 finish aaaa 5 3 27 ready bbbb2 0 0 50 finish ffff2 0 0 50 finish ggg 6 0 26 execute cputime:14 name cputime needtime priority state jing* 2 0 42 finish aaaa 6 2 24 execute bbbb2 0 0 50 finish ffff2 0 0 50 finish ggg 6 0 26 finish 3 cputime:15 name cputime needtime priority state jing* 2 0 42 finish aaaa 7 1 21 execute bbbb2 0 0 50 finish ffff2 0 0 50 finish ggg 6 0 26 finish cputime:16 name cputime needtime priority state jing* 2 0 42 finish aaaa 8 0 18 execute bbbb2 0 0 50 finish ffff2 0 0 50 finish ggg 6 0 26 finish All processes have finished [root@rhel5hbzy ~]# rtyu2 bash: rtyu2: command not found [root@rhel5hbzy ~]# 3 bash: 3: command not found 三、分析与体会 多道程序设计中,通常是若干个进程同时处于就绪状态,必须依照某种策略来决定哪个进程优先占有处理机。因而引起进程调度。本实验模拟单处理机情况下的处理机调度问题,使我加深了对进程调度的理解。通过本次试验,使我加深了对进程调度的具体细节的理解,掌握LINUX系统结构、实现处理机和各种典型的算法,系统了解了操作系统的设计和实验思路,动手能力更强了。 | |||||
操作系统处理机调度实验报告
本文发布于:2024-11-10 21:06:18,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
- 上一篇: 控制系统仿真与CAD 实验报告
- 下一篇: 三黑龙江的发展有十大优势[分享]
发布评论