时间片轮转算法和优先级调度算法 C语言模拟实现

合集下载

时间片轮转调度算法(vc版)

时间片轮转调度算法(vc版)

时间片轮转调度算法设计目的:熟悉各种作业/进程调度算法的原理。

设计要求:用高级语言编写和调试多个实现不同的作业/进程调度算法的程序。

设计内容:基于时间片的轮转调度的模拟。

设计一个系统,提供一个界面,供用户输入等待调度的作业信息,显示调度的结果。

//实验名称:时间片轮转调度算法//实验日期:2008/07/16//实验者:qhu-hh//实验环境:Visual C++ 2008 ExPress Edition//编程语言:C语言//程序思想:先模拟建立进程就绪链表----置所有进程的到达时间Parrive_time均为0,依PCB链接顺序从第一个进程PCB开始,////////////////使Pid依次为1,2,3,4……;就绪链表中进程的数量,由常量MAXSIZE 控制; ////////////再模拟建立调度函数--------取表头PCB,修改进程执行时间,得到的新时间,即为剩余执行时间,当剩余时间小于或等于0////////////////时,将此进程的PCB取出,依完成的先后次序链到完成链表中,记录当前完成进程的完成时间Pend_time,////////////////同时修改就绪链表表头;////////////最后计算和打印里程调度信息-----计算出各进程周转时间及所有进程的平均周转时间。

#include"stdio.h"#include"malloc.h"#define TIMESLICE 2//时间片;#define MAXSIZE 4//就绪链中进程数量;typedef struct PCB//定义进程控制块的信息结构;{int Pid;//进程标识号;int Parrive_time;//进程到达时间;int Pend_time;//进程结束时间;int Pexe_time;//进程执行时间;struct PCB *next;//链向一下进程;}Node,*PNode;PNode PCurrent=NULL;//定义全局变量,指向当前进程的PCB;PNode PLast=NULL;//定义全局变量,指向上一进程PCB;PNode PHead=NULL;//定义全局变量,指向第一个进程的PCB;PNode PTail=NULL;//全局变量,指向就绪进程链的最后一个进程的PCB; int Exe_time[MAXSIZE];bool judge=0;//建立进程就绪链表int Create(){int i=1;if(PCurrent==NULL)//就绪链表为空时;{PCurrent=(PNode)malloc(sizeof(Node));PCurrent->Pid=i;i++;//标识新建进程;PCurrent->Parrive_time=0;//初始化;printf("输入执行用时:");scanf("%d",&(PCurrent->Pexe_time));//新进程执行用时; Exe_time[i-2]=PCurrent->Pexe_time;//进程执行用时备份; printf("\n");PCurrent->Pend_time=0;//初始化;PCurrent->next=NULL;//初始化;PHead=PTail=PCurrent;//改变链头、链尾指向;};do{PCurrent=(PNode)malloc(sizeof(Node));PCurrent->Pid=i;i++;//标识新进程;PCurrent->Parrive_time=0;//初始化printf("输入执行用时:");scanf("%d",&(PCurrent->Pexe_time));//新进程执行用时;Exe_time[i-2]=PCurrent->Pexe_time;//进程执行用时备份;printf("\n");PCurrent->Pend_time=0;//初始化;PCurrent->next=NULL;//初始化;PTail->next=PCurrent;//链到链尾PTail=PCurrent;//改变链尾if(i>MAXSIZE)judge=1;}while(judge==0);printf("%d,%d\n",PHead,PTail);return 0;}int Prin(PNode P)//检验就绪链表中各进程PCB储存信息;{printf("打印就绪链表:\n");while(P!=NULL)//就绪链空时,检验完;{printf("标识号:%d,执行时间:%d,到达时间:%d\n ",P->Pid,P->Pexe_time,P->Parrive_time);P=P->next;}printf("打印结束:\n");return 0;}//时间片轮转调度PNode Time_slice_dispatch(PNode PH,PNode PT){printf("I'm Time_slice_dispatch(1);\n");PNode P_Head=NULL;PNode P_Tail=NULL;////处理完成的进程组成的链表头结点和尾结点;//PCurrent=PLast=NULL;//PCurrent=PH;int Time_count=0;//计时器;printf("计时器初始值:%d\n",Time_count);//printf("%d",PH);while(PH!=NULL)//就绪进程链表未处理完时,继续处理; {Time_count+=TIMESLICE;//当前时间;printf("计时器值——————————————%d\n",Time_count); printf("进程 %d 要求执行时间:%d\n",PH->Pid,PH->Pexe_time);PH->Pexe_time-=TIMESLICE;//当前处理进程剩余时间;printf("进程 %d 剩余执行时间:%d\n",PH->Pid,PH->Pexe_time);if(PH->Pexe_time<=0)//当前目标进程执行完时的处理方式;{if(P_Head==NULL)//{P_Head=P_Tail=PH;//处于完成态的进程的链表的表头与表尾; P_Head->Pend_time=Time_count;//记录第一个结束的进程的结束时间;printf("first overti me:………………%d\n",Time_count); PH=PH->next;//改变就绪进程链表的表头;PT->next=NULL;P_Tail->next=NULL;//结束进程的表尾next的域置空;}else{P_Tail->next=PH;//链到结束进程的表尾;P_Tail=PH;P_Tail->Pend_time=Time_count;//记录结束时间;//P_Tail=PCurrent;//改变结束进程的表尾;PH=PH->next;//改变就绪进程链表的表头;//PCurrent=PCurrent->next;/*PLast->next=PCurrent->next;*/P_Tail->next=NULL;//结束进程的表尾next的域置空;}}else//当前目标进程未处理完时的处理方式;{PT->next=PH;//将当前未完成的进程链到就绪链表表尾;PT=PH;///////////////////////////////////////////////**** **********/////////////////////PLast=PCurrent;PH=PH->next;//PCurrent;//这两句是改变就绪进程链表的表头;PT->next=NULL;}}/*if(P_Head==NULL)printf("kkkkkkkkkkkkk");*/ int b;scanf("%c",&b);return P_Head;}int Print(PNode P_H){PCurrent=P_H;char c;double a=0;while(PCurrent!=NULL){printf("\n进程标识号:%d,执行时间:%d,结束时间%d,周转时间:%d",PCurrent->Pid,Exe_time[PCurrent->Pid-1],PCurrent->Pend_time,( PCurrent->Pend_time)-(PCurrent->Parrive_time));printf("\n");a+=(PCurrent->Pend_time)-(PCurrent->Parrive_time); PCurrent=PCurrent->next;}printf("平均周转时间:%f",a/MAXSIZE);scanf("%c",&c);printf("%c",c);return 0;}//////////////////////////////int main(){PNode head;Create();Prin(PHead);head=Time_slice_dispatch(PHead,PTail); Print(head);return 0;}。

进程调度算法(时间片轮转法+优先级)

进程调度算法(时间片轮转法+优先级)

进程调度算法时间片轮转法+优先级#include<stdio.h>#include<stdlib.h>#include<iostream.h>#define P_NUM 5//进程数#define P_TIME 50//时间片数//状态enum state{ready,//就绪execute,//执行block,//阻塞finish//结束};//PCB结构体struct pcb{char name[5];//进程名IDint priority;//优先级int cputime;//进程已占用时间片int alltime;//还需占用时间片state process;//进程状态pcb * next;//队列指针next,用来将多个进程控制块PCB链接为队列}PCB;pcb * get_process(){pcb *q;pcb *t;pcb *p;int i=0;cout<<"input ID and ALLTIME"<<endl;while (i<P_NUM){q=(struct pcb *)malloc(sizeof(pcb));//申请cin>>q->name;cin>>q->alltime;//输入名字及时间q->cputime=0;//初始q->priority=P_TIME-q->alltime;//优先数越大,优先级越高q->process=ready;//就绪q->next=NULL;if (i==0){p=q;t=q;}else{t->next=q;t=q;}i++;}return p;}//输出过程void display(pcb *p){cout<<" ID"<<" "<<"CPUTIME"<<" "<<"ALLTIME"<<" "<<"PRIORITY"<<" "<<"STATE"<<endl;while(p){cout<<" "<<p->name<<" "<<p->cputime<<" "<<p->alltime<<" "<<p->priority<<" ";switch(p->process){case ready:cout<<"ready"<<endl;break;case execute:cout<<"execute"<<endl;break;case block:cout<<"block"<<endl;break;case finish:cout<<"finish"<<endl;break;}p=p->next;cout<<"\n";}}//状态的函数//结束int process_finish(pcb *q){int a=1;while(a&&q){a=a&&q->alltime==0;q=q->next;}return a;}//执行void cpuexe(pcb *q){pcb *t=q;int tp=0;while(q){if (q->process!=finish){q->process=ready;if(q->alltime==0){q->process=finish;}}if(tp<q->priority&&q->process!=finish){tp=q->priority;t=q;}q=q->next;}if(t->alltime!=0){t->priority-=3;//进程每运行一个时间片,优先数减3t->alltime--;t->process=execute;t->cputime++;}}//进程调度void process_way(){pcb * p;p=get_process();display(p);int cpu=0;while(!process_finish(p)){cpu++;cout<<"cputime:"<<cpu<<endl;cpuexe(p);display(p);}printf("All processes have finished"); }void main(){process_way();}结果:。

进程调度实验报告

进程调度实验报告

进程调度实验报告一、实验目的。

本实验旨在通过对进程调度算法的模拟和实验,加深学生对进程调度原理的理解,掌握各种进程调度算法的特点和应用场景,提高学生的实际操作能力和分析问题的能力。

二、实验环境。

本次实验使用了C语言编程语言,通过模拟实现了先来先服务(FCFS)、最短作业优先(SJF)、时间片轮转(RR)和多级反馈队列(MFQ)四种进程调度算法。

三、实验过程。

1. 先来先服务(FCFS)调度算法。

先来先服务调度算法是一种非抢占式的调度算法,按照进程到达的先后顺序进行调度。

在本次实验中,我们通过模拟多个进程到达并排队等待CPU执行,观察其平均等待时间和平均周转时间。

实验结果表明,先来先服务调度算法适用于作业长度差异较大的情况,但容易产生“饥饿”现象。

2. 最短作业优先(SJF)调度算法。

最短作业优先调度算法是一种非抢占式的调度算法,按照作业执行时间的长短进行调度。

在本次实验中,我们通过模拟多个作业的执行时间,观察其平均等待时间和平均周转时间。

实验结果表明,最短作业优先调度算法能够最大程度地减少平均等待时间,但可能会导致长作业被“饿死”。

3. 时间片轮转(RR)调度算法。

时间片轮转调度算法是一种抢占式的调度算法,每个进程被分配一个时间片,当时间片用完后,该进程被放到队尾等待。

在本次实验中,我们通过模拟多个进程的执行和时间片的调度,观察其平均等待时间和平均周转时间。

实验结果表明,时间片轮转调度算法能够保证每个进程都能得到一定的执行时间,但可能会导致上下文切换频繁。

4. 多级反馈队列(MFQ)调度算法。

多级反馈队列调度算法是一种综合性的调度算法,根据进程的优先级和执行时间进行动态调整。

在本次实验中,我们通过模拟多个进程的执行和不同优先级队列的调度,观察其平均等待时间和平均周转时间。

实验结果表明,多级反馈队列调度算法能够兼顾短作业和长作业,提高了系统的整体性能。

四、实验总结。

通过本次实验,我们深入理解了不同进程调度算法的特点和适用场景。

抢占优先数调度算法和时间片轮转调度算法

抢占优先数调度算法和时间片轮转调度算法

#include <iostream>#include <stdlib.h>#include <cstring>#include <stdio.h>using namespace std;class pcb{public:char name;int prioORround;int cputime;int count;int needtime;char state;pcb *next;};class createpcb{public:pcb* newpcb(int count){pcb *apcb=new pcb;switch(count){case 1: apcb->name='a';break;case 2: apcb->name='b';break;case 3: apcb->name='c';break;case 4: apcb->name='d';break;case 5: apcb->name='e';break;default: cout<<"错误";}apcb->prioORround=45+rand()%5;apcb->cputime=0;apcb->count=0;apcb->needtime=10+rand()%5;apcb->state='R';apcb->next=NULL;return apcb;}private:pcb *p;};class print{public:void printf(pcb *ready,pcb *tail,pcb *finish,pcb *run){ p=run;cout<<"运行:";while(p->next!=NULL){p=p->next;cout<<p->name<<":"<<p->needtime<<"\t";}p=ready;cout<<"就绪:";while(p->next!=tail){p=p->next;cout<<p->name<<":"<<p->needtime<<"\t";}p=finish;cout<<"完成:";while(p->next!=NULL){p=p->next;cout<<p->name<<":"<<p->needtime<<"\t";}cout<<endl;}private:pcb *p;};class insert1{public:void insert(pcb *ready,pcb *tail){k=ct.newpcb(ready->count);ready->count+=1;p=ready;while(p->next!=tail&&p->next->prioORround>k->prioORround) p=p->next;k->next=p->next;p->next=k;}void insert(pcb *ready,pcb *tail,pcb *finish,pcb *run){ pt.printf(ready,tail,finish,run);k=run->next;k->prioORround-=3;k->cputime+=1;k->needtime-=1;k->state='R';k->next=NULL;run->next=NULL;if(k->needtime>0){p=ready;while(p->next!=tail&&p->next->prioORround>k->prioORround) p=p->next;k->next=p->next;p->next=k;}else{p=finish;while(p->next!=NULL)p=p->next;p->next=k;}}private:pcb *p;pcb *k;createpcb ct;print pt;};class insert2{public:void insert(pcb *ready,pcb *tail){k=ct.newpcb(ready->count);ready->count+=1;p=ready;while(p->next!=tail)p=p->next;k->next=p->next;p->next=k;void insert(pcb *ready,pcb *tail,pcb *finish,pcb *run){ pt.printf(ready,tail,finish,run);k=run->next;k->cputime+=2;k->needtime-=2;k->state='R';k->next=NULL;run->next=NULL;if(k->needtime>0){p=ready;while(p->next!=tail)p=p->next;k->next=p->next;p->next=k;}else{p=finish;while(p->next!=NULL)p=p->next;p->next=k;}}private:pcb *p;pcb *k;createpcb ct;print pt;};class firstin{public:void runs(pcb *ready,pcb *tail,pcb *run){if(ready->next!=tail){p=run;p->next=ready->next;p=p->next;ready->next=p->next;p->state='W';p->next=NULL;}private:pcb *p;};class prisch{public:prisch(){system("cls");='R';ready.count=1;='T';='F';ready.next=&tail;tail.next=NULL;finish.next=NULL;run.next=NULL;printf("\t\t\t优先级数调度\n");while(1){if(ready.count<=5)inst.insert(&ready,&tail);fir.runs(&ready,&tail,&run);inst.insert(&ready,&tail,&finish,&run);if(ready.next==&tail)break;}pt.printf(&ready,&tail,&finish,&run);getchar();}private:pcb *p;pcb ready;pcb tail;pcb finish;pcb run;insert1 inst;firstin fir;print pt;string algo;};class roundsch{public:roundsch(){system("cls");='R';ready.count=1;='T';='F';ready.next=&tail;tail.next=NULL;finish.next=NULL;run.next=NULL;printf("\t\t\t时间片轮转调度\n");while(1){if(ready.count<=5)inst.insert(&ready,&tail);fir.runs(&ready,&tail,&run);inst.insert(&ready,&tail,&finish,&run);if(ready.next==&tail)break;}pt.printf(&ready,&tail,&finish,&run);getchar();}private:pcb *p;pcb ready;pcb tail;pcb finish;pcb run;insert2 inst;firstin fir;print pt;};class menu{public:menu(){char c;bool z=false;while(1){system("cls");cout<<"\n\n\n\t\t*****抢占优先数调度算法和时间片轮转调度算法******\n\n"<<endl<<"\t\t*\t\t\t1,优先级数调度\t\t*\n"<<endl<<"\t\t*\t\t2,时间片轮转调度\t\t*\n"<<endl<<"\t\t*\t3,退出程序\t\t\t\t*\n"<<endl<<"\t\t*************************************************\ n"<<endl;if(z==true){cout<<"\t\t\t\t选择输入错误!"<<endl;z=false;}cout<<"\t\t请输入选择:";scanf("%c",&c);getchar();if(c=='1') prisch prisch;else if(c=='2') roundsch roundsch;else if(c=='3'){cout<<"\n\n\t\t\t";exit(0);}else z=true;}}};int main(int args,char** argv) {menu menu;return 0;}。

进程的调度实验报告(3篇)

进程的调度实验报告(3篇)

第1篇一、实验目的通过本次实验,加深对操作系统进程调度原理的理解,掌握先来先服务(FCFS)、时间片轮转(RR)和动态优先级(DP)三种常见调度算法的实现,并能够分析这些算法的优缺点,提高程序设计能力。

二、实验环境- 编程语言:C语言- 操作系统:Linux- 编译器:GCC三、实验内容本实验主要实现以下内容:1. 定义进程控制块(PCB)结构体,包含进程名、到达时间、服务时间、优先级、状态等信息。

2. 实现三种调度算法:FCFS、RR和DP。

3. 创建一个进程队列,用于存储所有进程。

4. 实现调度函数,根据所选算法选择下一个执行的进程。

5. 模拟进程执行过程,打印进程执行状态和就绪队列。

四、实验步骤1. 定义PCB结构体:```ctypedef struct PCB {char processName[10];int arrivalTime;int serviceTime;int priority;int usedTime;int state; // 0: 等待,1: 运行,2: 完成} PCB;```2. 创建进程队列:```cPCB processes[MAX_PROCESSES]; // 假设最多有MAX_PROCESSES个进程int processCount = 0; // 实际进程数量```3. 实现三种调度算法:(1)FCFS调度算法:```cvoid fcfsScheduling() {int i, j;for (i = 0; i < processCount; i++) {processes[i].state = 1; // 设置为运行状态printf("正在运行进程:%s\n", processes[i].processName); processes[i].usedTime++;if (processes[i].usedTime == processes[i].serviceTime) { processes[i].state = 2; // 设置为完成状态printf("进程:%s 完成\n", processes[i].processName); }for (j = i + 1; j < processCount; j++) {processes[j].arrivalTime--;}}}```(2)RR调度算法:```cvoid rrScheduling() {int i, j, quantum = 1; // 时间片for (i = 0; i < processCount; i++) {processes[i].state = 1; // 设置为运行状态printf("正在运行进程:%s\n", processes[i].processName); processes[i].usedTime++;processes[i].serviceTime--;if (processes[i].serviceTime <= 0) {processes[i].state = 2; // 设置为完成状态printf("进程:%s 完成\n", processes[i].processName); } else {processes[i].arrivalTime++;}for (j = i + 1; j < processCount; j++) {processes[j].arrivalTime--;}}}```(3)DP调度算法:```cvoid dpScheduling() {int i, j, minPriority = MAX_PRIORITY;int minIndex = -1;for (i = 0; i < processCount; i++) {if (processes[i].arrivalTime <= 0 && processes[i].priority < minPriority) {minPriority = processes[i].priority;minIndex = i;}}if (minIndex != -1) {processes[minIndex].state = 1; // 设置为运行状态printf("正在运行进程:%s\n", processes[minIndex].processName);processes[minIndex].usedTime++;processes[minIndex].priority--;processes[minIndex].serviceTime--;if (processes[minIndex].serviceTime <= 0) {processes[minIndex].state = 2; // 设置为完成状态printf("进程:%s 完成\n", processes[minIndex].processName); }}}```4. 模拟进程执行过程:```cvoid simulateProcess() {printf("请选择调度算法(1:FCFS,2:RR,3:DP):");int choice;scanf("%d", &choice);switch (choice) {case 1:fcfsScheduling();break;case 2:rrScheduling();break;case 3:dpScheduling();break;default:printf("无效的调度算法选择。

优先级和时间片轮转调度

优先级和时间片轮转调度
}
for(j=0;j<processnum;j++)
{q=processarray[j];
printf("\n检查进程%3c是否结束,进程当前状态:%3d",q->name,qgame is over!\n");
}
void main()
{
*processarray=createprocess(processarray);
#include "stdio.h"
#include <malloc.h>
#define max 100
#define etime 4
#define pfree 0
#define running 1
#define aready 2
#define blocking 3
typedef struct node
{
char name;
int status;
int pri;
int ptime;
int ax,bx,cx,dx;
int pc;
int psw;
}pcb;
pcb *processarray[max];
static int processnum;
pcb *createprocess(pcb *processarray[])组
q->status=aready;
totaltime-=etime;}
if(i<processnum-1)
i++;
else if(i==processnum-1)
i=0;
}}
void processStaPri(pcb *processarray[])

进程调度模拟设计——时间片轮转、优先级法 程序

进程调度模拟设计——时间片轮转、优先级法 程序
rq = new rnode;
rq->next = NULL;
rq->pname = dp->pname;
rq->ctime = dp->ctime;
rq->rtime = dp->rtime;
rq->stime = dp->stime;
{
if(dp->bb != 1)
{
dp->stime = letime;
dp->bb = 1;
}
if(dp->rrtime > time)
{
dp->etime = letime + time;
dp->rrtime = dp->rrtime - time;
nn = inhead->yx;
num = inhead->num;
bb = 1;
}
inhead = inhead->next;
}
if(bb == 1)
{
inhead = head->next;
while(inhead)
while(inhead)
{
if(inhead->bj != 1)
{
rq = new rnode;
rq->next = NULL;
rq->pname = inhead->pname;
rq->ctime = inhead->ctime;
rq->etime = letime;
rq->ztime = rq->etime - rq->ctime;

操作系统实验报告时间片轮转调度算法

操作系统实验报告时间片轮转调度算法

操作系统实验报告时间片轮转调度算法“网络协议分析”实验4实验名称:用Ethereal研究DNS和HTTP协议实验目的:通过对捕获分组的分析和研究,加深对DNS协议和HTTP协议的工作原理和实现过程的理解。

实验环境:连网PC机,Ethereal网络协议分析软件实验步骤:1.安装Ethereal网络协议分析器。

2.打开Ethereal软件菜单中的Help->Contents,可学习Ethereal的使用方法。

3.开始捕获分组之前,清空客户端Web浏览器的高速缓存和DNS的高速缓存(命令为:ipconfig /flushdns)。

(想一想,为什么?)4.在Capture->Option里选择网卡类型;取消捕获分组的“混杂模式”;设置捕获过滤器为:“host 本机IP”,这样Ethereal就会只捕获从本机发出的和发往本机的分组。

5.点击Start启动协议分析器,用Ethereal捕获从本机发出和发往本机的分组。

6.在Web浏览器中输入URL(如.cn, 等,网页较简单)。

网页显示出来后,过一会儿停止捕获。

将跟踪文件保存在一个文件中。

实验结果分析:1.在跟踪列表框中找出请求网页时发出的DNS查询报文和回答报文,找出发起TCP连接的三次握手报文,找出HTTP请求报文和响应报文。

2.在协议框中找出各层协议,观察各层协议,并对照教材中DNS查询/回答报文结构和HTTP请求/响应报文结构对这些应用层报文进行分析,找出报文中各字段相应的内容,解释其含义和用途。

3.你的主机所用的DNS服务器的IP地址是多少?你的浏览器与DNS服务器之间使用传输层的什么协议进行通信?202.196.0.1DNS请求报文和应答报文的ID号一样吗?是什么?一样,0xc4a6你所请求网站的规范名是什么?mail.DNS服务器对你的域名解析请求在应答中给出了几个可用的IP地址?都是什么?2个,202.196.0.16,202.196.0.17和DNS服务器通信时,你的客户机使用的端口号是多少?DNS服务器使用的端口号是多少?64384,53你所请求Web服务器的IP地址是什么?其域名有几个层次(参看教材127页)?202.196.0.16 4个如果请求一个存在/不存在的网页,Web服务器分别会应答什么? ??等等。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

一、目得与要求ﻫ进程调度就是处理机管理得核心内容。

本实验要求用高级语言编写模拟进程调度程序,以便加深理解有关进程控制快、进程队列等概念,并体会与了解优先数算法与时间片轮转算法得具体实施办法。

二、实验内容1、设计进程控制块PCB得结构,通常应包括如下信息:进程名、进程优先数(或轮转时间片数)、进程已占用得CPU时间、进程到完成还需要得时间、进程得状态、当前队列指针等。

2、编写两种调度算法程序:优先数调度算法程序ﻫ循环轮转调度算法程序3、按要求输出结果。

ﻫ三、提示与说明分别用两种调度算法对伍个进程进行调度。

每个进程可有三种状态;执行状态(RUN)、就绪状态(READY,包括等待状态)与完成状态(FINISH),并假定初始状态为就绪状态。

ﻫ(一)进程控制块结构如下:ﻫNAME——进程标示符PRIO/ROUND——进程优先数/进程每次轮转得时间片数(设为常数2)ﻫ CPUTIME——进程累计占用CPU得时间片数ﻫ NEEDTIME——进程到完成还需要得时间片数STATE——进程状态ﻫNEXT——链指针ﻫ注:1、为了便于处理,程序中进程得得运行时间以时间片为单位进行计算;2、各进程得优先数或轮转时间片数,以及进程运行时间片数得初值,均由用户在程序运行时给定。

ﻫ(二)进程得就绪态与等待态均为链表结构,共有四个指针如下:ﻫ RUN——当前运行进程指针READY——就需队列头指针TAIL——就需队列尾指针FINISH——完成队列头指针1、在优先数算法中,进程优先数得初值设为:(三)程序说明ﻫ50-NEEDTIMEﻫ每执行一次,优先数减1,CPU时间片数加1,进程还需要得时间片数减1。

在轮转法中,采用固定时间片单位(两个时间片为一个单位),进程每轮转一次,CPU时间片数加2,进程还需要得时间片数减2,并退出CPU,排到就绪队列尾,等待2、程序得模块结构提示如下:下一次调度。

ﻫ整个程序可由主程序与如下7个过程组成:ﻫ(1)INSERT1——在优先数算法中,将尚未完成得PCB按优先数顺序插入到就绪队列中;ﻫ(2)INSERT2——在轮转法中,将执行了一个时间片单位(为2),但尚未完成得进程得PCB,插到就绪队列得队尾;(3)FIRSTIN——调度就绪队列得第一个进程投入运行;ﻫ(4)PRINT——显示每执行一次后所有进程得状态及有关信息。

ﻫ(5)CREATE——创建新进程,并将它得PCB插入就绪队列;(6)PRISCH——按优先数算法调度进程;ﻫ(7)ROUNDSCH——按时间片轮转法调度进程。

主程序定义PCB结构与其她有关变量。

ﻫ(四)运行与显示程序开始运行后,首先提示:请用户选择算法,输入进程名与相应得NEEDTIME值。

每次显示结果均为如下5个字段:name cputime needtime prioritystateﻫ注:ﻫ1.在state字段中,"R"代表执行态,"W"代表就绪(等待)态,"F"代表完成态。

ﻫ2.应先显示"R"态得,再显示"W"态得,再显示"F"态得。

ﻫ3.在"W"态中,以优先数高低或轮转顺序排队;在"F"态中,以完成先后顺序排队。

view plaincopy to clipboardprint?1./*2.操作系统实验之时间片轮转算法与优先级调度算法3.By Visual C++ 6、04.*/#include <stdio、h>#include <stdlib、h>#include <string、h>typedef struct node{charname[20]; /*进程得名字*/int prio; /*进程得优先级*/int round; /*分配CPU得时间片*/int cputime; /*CPU执行时间*/int needtime; /*进程执行所需要得时间*/char state; /*进程得状态,W——就绪态,R——执行态,F——完成态* /int count; /*记录执行得次数*/struct node *next; /*链表指针*/}PCB;PCB *ready=NULL,*run=NULL,*finish=NULL; /*定义三个队列,就绪队列,执行队列与完成队列*/int num;void GetFirst(); /*从就绪队列取得第一个节点*/void Output(); /*输出队列信息*/void InsertPrio(PCB *in); /*创建优先级队列,规定优先数越小,优先级越高*/void InsertTime(PCB*in); /*时间片队列*/void InsertFinish(PCB *in); /*时间片队列*/void PrioCreate(); /*优先级输入函数*/void TimeCreate(); /*时间片输入函数*/void Priority(); /*按照优先级调度*/void RoundRun(); /*时间片轮转调度*/int main(void){char chose;printf("请输入要创建得进程数目:\n");scanf("%d",&num);getchar();printf("输入进程得调度方法:(P/R)\n");scanf("%c",&chose);switch(chose){case 'P':case 'p':PrioCreate();Priority();break;case 'R':case 'r':TimeCreate();RoundRun();break;default:break;}Output();return0;}void GetFirst() /*取得第一个就绪队列节点*/{run = ready;if(ready!=NULL){run ->state = 'R';ready = ready ->next;run ->next =NULL;}}void Output()/*输出队列信息*/{PCB *p;p=ready;printf("进程名\t优先级\t轮数\tcpu时间\t需要时间\t进程状态\t计数器\n");while(p!=NULL){printf("%s\t%d\t%d\t%d\t%d\t\t%c\t\t%d\n",p->name,p->prio,p->round,p->cputime,p->needtime,p->state,p->co unt);p = p->next;}p =finish;while(p!=NULL){printf("%s\t%d\t%d\t%d\t%d\t\t%c\t\t%d\n",p->nam e,p->prio,p->round,p->cputime,p->needtime,p->state,p->count);p = p->next;}p = run;while(p!=NULL){printf("%s\t%d\t%d\t%d\t%d\t\t%c\t\t%d\n",p->name,p-> prio,p->round,p->cputime,p->needtime,p->state,p->coun t);p = p->next;}}void InsertPrio(PCB *in) /*创建优先级队列,规定优先数越小,优先级越低*/{PCB *fst,*nxt;fst = nxt = ready;if(ready ==NULL) /*如果队列为空,则为第一个元素*/{in->next = ready;ready = in;}else /*查到合适得位置进行插入*/{if(in ->prio >=fst ->prio) /*比第一个还要大,则插入到队头* /{in->next =ready;ready = in;}else{while(fst->next != NULL) /*移动指针查找第一个别它小得元素得位置进行插入*/{nxt= fst;fst=fst->next;}if(fst ->next == NULL) /*已经搜索到队尾,则其优先级数最小,将其插入到队尾即可*/{in->next= fst ->next;fst ->next = in;}else /*插入到队列中*/{nxt = in;in ->next = fst;}}}}void InsertTime(PCB *in) /*将进程插入到就绪队列尾部*/ {PCB *fst;fst = ready;if(ready == NULL){in->next = ready;ready= in;}else{while(fst->next != NULL){fst = fst->next;}in ->next = fst ->next;fst ->next = in;}}void InsertFinish(PCB *in) /*将进程插入到完成队列尾部*/ {PCB *fst;fst = finish;if(finish == NULL){in->next = finish;finish = in;}else{while(fst->next != NULL){fst = fst->next;}in ->next =fst ->next;fst ->next = in;}}void PrioCreate() /*优先级调度输入函数*/{PCB *tmp;inti;printf("输入进程名字与进程所需时间:\n");for(i = 0;i < num; i++){if((tmp = (PCB*)malloc(sizeof(PCB)))==NULL){perror("malloc");exit(1);}scanf("%s",tmp->name);getchar(); /*吸收回车符号*/scanf("%d",&(tmp->needtime));tmp ->cputime = 0;tmp ->state ='W';tmp ->prio = 50 - tmp->needtime; /*设置其优先级,需要得时间越多,优先级越低*/tmp ->round = 0;tmp ->count = 0;InsertPrio(tmp); /*按照优先级从高到低,插入到就绪队列*/}}void TimeCreate() /*时间片输入函数*/{PCB *tmp;int i;printf("输入进程名字与进程时间片所需时间:\n");for(i = 0;i < num;i++){if((tmp = (PCB *)malloc(sizeof(PCB)))==NULL){perror("malloc");exit(1);}scanf("%s",tmp->name);getchar();scanf("%d",&(tmp->needtime));tmp ->cputime = 0;tmp ->state ='W';tmp ->prio = 0;tmp ->round = 2; /*假设每个进程所分配得时间片就是2*/tmp ->count = 0;InsertTime(tmp);}}void Priority() /*按照优先级调度,每次执行一个时间片*/{int flag = 1;GetFirst();while(run != NULL) /*当就绪队列不为空时,则调度进程如执行队列执行*/{Output(); /*输出每次调度过程中各个节点得状态*/while(flag){run->prio -= 3; /*优先级减去三*/run->cputime++; /*CPU时间片加一*/run->needtime--;/*进程执行完成得剩余时间减一*/if(run->needtime == 0)/*如果进程执行完毕,将进程状态置为F,将其插入到完成队列*/{run ->state = 'F';run->count++; /*进程执行得次数加一*/InsertFinish(run);flag = 0;}else/*将进程状态置为W,入就绪队列*/{run->state = 'W';run->count++; /*进程执行得次数加一*/InsertTime(run);flag = 0;}}flag=1;GetFirst(); /*继续取就绪队列队头进程进入执行队列*/ }}void RoundRun() /*时间片轮转调度算法*/{int flag = 1;GetFirst();while(run != NULL){Output();while(flag){run->count++;run->cputime++;run->needtime--;if(run->needtime == 0) /*进程执行完毕*/{run ->state = 'F';InsertFinish(run);flag = 0;}else if(run->count == run->round)/*时间片用完*/ {run->state = 'W';run->count = 0; /*计数器清零,为下次做准备*/InsertTime(run);flag = 0;}}flag = 1;GetFirst();}}。

相关文档
最新文档