处理机调度操作系统实验报告.doc_第1页
处理机调度操作系统实验报告.doc_第2页
处理机调度操作系统实验报告.doc_第3页
处理机调度操作系统实验报告.doc_第4页
处理机调度操作系统实验报告.doc_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

_学年第 学期 操作系统 课程实 验 报 告学 院: 专 业: 班 级: 姓 名: 学 号: 任课教师: 精品资料实验题目处理机调度实验地点实验目的1.通过编写程序实现进程或作业先来先服务、高优先权、按时间片轮转调度算法,进一步掌握进程调度的概念和算法,加深对处理机分配的理解。 2.了解Linux中进程(线程)的调度机制。3.学习使用Linux中进程(线程)调度算法,掌握相应的与调度有关的函数。实验内容1、程序说明:(1)先来先服务算法:如果早就绪的进程排在就绪队列的前面,迟就绪的进程排在就绪队列的后面,那么先来先服务(FCFS:first come first service)总是把当前处于就绪队列之首的那个进程调度到运行状态。(2)轮转法就是按一定时间片(记为q)轮番运行各个进程。如果q是一个定值,则轮转法是一种对各进程机会均等的调度方法。(3)优先级调度的基本思想是,把当前处于就绪队列中优先级最高的进程投入运行,而不管各进程的下一个CPU周期的长短和其他因素。2、具体步骤: (1)分析问题,提出解决问题的算法(2)编制程序(3)程序调试(4)记录实验结果,以及思考是否能够改善算法(1)编辑程序jcdd.c,编译并执行。#include #include #define P_NUM 5#define P_TIME 50enum stateready,execute,block,finish;struct pcbbchar name4;int priority; /数越大优先级越高int cputime; /已占用CPU的时间int needtime; /执行时间int count;enum state process;struct pcbb *next;typedef struct pcbb pcb;void display_menu()printf(CHOOSE THE ALGORITHM:n);printf(1 PRIORITYn);printf(2 ROUNDROBINn);printf(3 EXITn);pcb* get_process()pcb *q;pcb *p; /头指针pcb *t; /尾指针int i = 0;printf(input name and timen);while (i 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;elset-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 needtimepriority staten);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(readyn);break;case execute:printf(executen); break;case block:printf(blockn); break;case finish:printf(finishn); 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) /让t指向优先级最高的进程if (q-process!=finish)q-process = ready;if(q-needtime=0)q-process = finish;if(tppriority&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:%dn,cpu);cpuexe(p); /选择优先级最高的进程执行一个时间单位display(p); /每调度一次就显示次sleep(2);free_process(p); /释放所有进程printf(All processes have finishedn);pcb *get_process_round()pcb *q;pcb *p; /头指针pcb *t; /尾指针int i = 0;printf(input name and timen);while (iname);scanf(%d,&q-needtime);q-cputime = 0;q-count = 0;q-process = ready;q-next = NULL;if(i=0)p = q;t = q;elset-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-needtimeneedtime = 0;q-count+;q-process = execute;pcb *get_next(pcb *k,pcb *head)pcb *t;t = k;dot =t-next;while ( t & t-process = finish);if(t = NULL)t = head;/k是刚刚被执行的节点,如果t-next=k,所明就绪队列除了k和t以外都已结束,按照时间片轮转算法,该t执行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 needtimecount staten);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(readyn);break;case execute:printf(executen); break;case block:printf(blockn); break;case finish:printf(finishn); 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;elsecpu+=2; /时间片长度是2cpu_round(r);r = get_next(r,p); /获得下一个需要执行的进程printf(cputime:%dn,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);(2)编辑程序先来先服务调度算法 fcfs.c,编译并执行。#include #include int SUM=0,K=0;typedef struct link int time; int averageTime; int priority; struct link *next;linknode;linknode *creat()int n,m;linknode *head,*r,*s;head=r=(linknode *)malloc(sizeof(linknode);printf(输入各进程的处理时间和优先级并以两个0为结束标志:n); while(scanf(%d %d,&n,&m)&n&m)s=(linknode *)malloc(sizeof(linknode);K+;s-time=n;s-priority=m;s-averageTime=0;r-next=s;r=s;r-next=NULL;return head;linknode *seekAverageTime(linknode *head)int sum=0;linknode *p;p=head-next;while(p)sum=sum+p-time; p-averageTime=sum;p=p-next; SUM+=sum;void print(linknode *head)linknode *p;p=head-next;printf(各进程处理时间为:); w

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论