1、 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);1 PRIORITYn2 ROUNDROBINn3 EXITnpcb* get_process() pcb *q; pcb *p; pcb *t; int i = 0;input name and timen while (i name);%d,&q-needtime
2、); q-cputime = 0;priority = P_TIME - q-needtime;process = ready;next = NULL; if(i=0) p = q; t = q; else t-next = q; i+; return p;void free_process(pcb *p) while(p!= NULL) q = p; p = p-next; free(q);void display(pcb *p) name cputime needtime priority staten while(p) printf(,p- cputime);priority); swi
3、tch(p-process) case ready:printf(readynbreak; case execute:executen break; case block:blockn case finish:finishnint process_finish(pcb *q) int b1 = 1; while(b1&q) b1 = b1&needtime=0; q = q- return b1;void cpuexe(pcb *q) pcb *t = q; int tp = 0; while(q) if (q-process!=finish) q- if(q-needtime=0) q-pr
4、ocess = finish; if(tppriority; if(t-needtime!=0) t-priority -=3;needtime -;process = execute;cputime+;void priority_cal() p = get_process(); int cpu = 0; while(!process_finish(p) cpu+;cputime:%dn,cpu); cpuexe(p); display(p); sleep(2); free_process(p);All processes have finishednpcb *get_process_roun
5、d() while (ineedtime=1) q- elsecputime +=2;needtime -=2;needtime while ( t & t-process = finish); if(t = NULL) t = head; while(t-next!=k &process = finish) t = t- return t;void set_state(pcb *p) if(p-needtime = 0) p-process = execute) void display_round(pcb *p)name cputime needtime count statencount
6、);void round_cal() pcb *r; p = get_process_round(); r=p; if(r- cpu+=1; else cpu+=2; cpu_round(r); r = get_next(r,p); display_round(p); set_state(p);main() display_menu(); int k; scanf(k); switch(k) case 1:priority_cal(); case 2:round_cal(); case 3: default:YOU HAVE NOT CHOOSE ANY ALGORITHM!运行后结果如下:r
7、ootrhel5hbzy # gcc -o chuliji chuliji.crootrhel5hbzy # ./m1 PRIORITY2 ROUNDROBIN3 EXIT1input name and timejing 2aaaa 8bbbb5ffff4ggg 6name cputime needtime priority statejing- 1 1 45 executeaaaa* 0 8 42 readybbbb2 0 0 50 finishffff2 0 0 50 finishggg 0 6 44 readyrtyucputime:2jing* 2 0 42 execute5cputi
8、me:3jing* 2 0 42 finishggg 1 5 41 execute2cputime:4aaaa 1 7 39 executeggg 1 5 41 ready5 1 7 39 readyggg 2 4 38 execute6aaaa$ 2 6 36 executeggg 2 4 38 ready7aaaa$ 2 6 36 readyggg 3 3 35 execute8aaaa! 3 5 33 executeggg 3 3 35 ready9 3 5 33 readyggg 4 2 32 execute10aaaa 4 4 30 executeggg 4 2 32 ready11
9、aaaa 4 4 30 readyggg 5 1 29 execute12aaaa_ 5 3 27 executeggg 5 1 29 ready13aaaa_ 5 3 27 readyggg 6 0 26 execute14aaaa_ 6 2 24 executeggg 6 0 26 finish15aaaa_ 7 1 21 execute16aaaa_ 8 0 18 executeAll processes have finishedrootrhel5hbzy # rtyu2bash: rtyu2: command not foundrootrhel5hbzy # 3 3:三、分析与体会多道程序设计中,通常是若干个进程同时处于就绪状态,必须依照某种策略来决定哪个进程优先占有处理机。因而引起进程调度。本实验模拟单处理机情况下的处理机调度问题,使我加深了对进程调度的理解。通过本次试验,使我加深了对进程调度的具体细节的理解,掌握LINUX系统结构、实现处理机和各种典型的算法,系统了解了操作系统的设计和实验思路,动手能力更强了。
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1