操作系统处理机调度实验报告

时间:2025-02-25

处理机调度算法实验报告

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 …… 此处隐藏:3902字,全部文档内容请下载后查看。喜欢就下载吧 ……

操作系统处理机调度实验报告.doc 将本文的Word文档下载到电脑

    精彩图片

    热门精选

    大家正在看

    × 游客快捷下载通道(下载后可以自由复制和排版)

    限时特价:7 元/份 原价:20元

    支付方式:

    开通VIP包月会员 特价:29元/月

    注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
    微信:fanwen365 QQ:370150219