操作系统处理机调度实验报告
发布时间:2024-11-17
发布时间:2024-11-17
处理机调度算法实验报告
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 while(p){ printf("%s",p->name); printf(" "); printf("%d",p->cputime); printf(" "); printf("%d",p->needtime); printf(" ");
state\n");
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 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;
state\n");
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 name cputime jing1 aaaa* 0 bbbb2 0 ffff2 0 ggg 0 rtyucputime:2 name cputime jing* 2 aaaa* 0 bbbb2 0 ffff2 0 ggg 0 5cputime:3 name cputime jing* 2 aaaa* 0 bbbb2 0 ffff2 0 ggg 1 2cputime:4 name cputime jing* 2 aaaa' 1 bbbb2 0 ffff2 0 ggg 1 cputime:5 name cputime jing* 2 aaaa' 1 bbbb2 0 ffff2 0 ggg 2 cputime:6 name cputime jing* 2 aaaa$ 2 bbbb2 0
needtime 1 8 0 0 6 needtime 0 8 0 0 6 needtime 0 8 0 0 5 needtime 0 7 0 0 5
priority 45 42 50 50 44 priority 42 42 50 50 44 priority 42 42 50 50 41 priority 42 39 50 50 41
state execute ready finish finish ready state execute ready finish finish ready state finish ready finish finish execute state finish execute finish finish ready
needtime 0 7 0 0 4 needtime 0 6 0
priority 42 39 50 50 38 priority 42 36 50
state finish ready finish finish execute state finish execute finish
ffff2 0 ggg 2 cputime:7 name cputime jing* 2 aaaa$ 2 bbbb2 0 ffff2 0 ggg 3 cputime:8 name cputime jing* 2 aaaa! 3 bbbb2 0 ffff2 0 ggg 3 cputime:9 name cputime jing* 2 aaaa! 3 bbbb2 0 ffff2 0 ggg 4 cputime:10 name cputime jing* 2 aaaa4 bbbb2 0 ffff2 0 ggg 4 cputime:11 name cputime jing* 2 aaaa4 bbbb2 0 ffff2 0 ggg 5 cputime:12 name cputime jing* 2 aaaa 5 bbbb2 0 ffff2 0 ggg 5
0 4 needtime 0 6 0 0 3 needtime 0 5 0 0 3 needtime 0 5 0 0 2 needtime 0 4 0 0 2 needtime 0 4 0 0 1 needtime 0 3 0 0 1
50 38 priority 42 36 50 50 35 priority 42 33 50 50 35 priority 42 33 50 50 32 priority 42 30 50 50 32 priority 42 30 50 50 29 priority 42 27 50 50 29
finish ready state finish ready finish finish execute state finish execute finish finish ready state finish ready finish finish execute
state finish execute finish finish ready state finish ready finish finish execute state finish execute finish finish ready
cputime:13 name cputime needtime jing* 2 0 aaaa 5 3 bbbb2 0 0 ffff2 0 0 ggg 6 0 cputime:14 name cputime needtime jing* 2 0 aaaa 6 2 bbbb2 0 0 ffff2 0 0 ggg 6 0 3 cputime:15 name cputime needtime jing* 2 0 aaaa 7 1 bbbb2 0 0 ffff2 0 0 ggg 6 0 cputime:16 name cputime needtime jing* 2 0 aaaa 8 0 bbbb2 0 0 ffff2 0 0 ggg 6 0 All processes have finished [root@rhel5hbzy ~]# rtyu2 bash: rtyu2: command not found [root@rhel5hbzy ~]# 3 bash: 3: command not found 三、分析与体会
priority 42 27 50 50 26 priority 42 24 50 50 26
state finish ready finish finish execute state finish execute finish finish finish
priority 42 21 50 50 26 priority 42 18 50 50 26
state finish execute finish finish finish state finish execute finish finish finish
多道程序设计中,通常是若干个进程同时处于就绪状态,必须依照某 种策略来决定哪个进程优先占有处理机。因而引起进程调度。本实验模拟单 处理机情况下的处理机调度问题,使我加深了对进程调度的理解。通过本次 试验,使我加深了对进程调度的具体细节的理解,掌握 LINUX 系统结构、实 现处理机和各种典型的算法,系统了解了操作系统的设计和实验思路,动手 能力更强了。