进程调度算法论文优先级调度~

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

题目操作系统课程设计

实验一:进程调度算法

1.实验目的

通过优先权法和轮转算法的模拟加深对进程概念和进程调度过程的理解,掌握进程状态之间的切换,同时掌握进程调度算法的实现方法和技巧。2.实验内容

1)用C语言或C++语言来实现对n个进程采用优先权算法以及轮转算法的进程调度。

2)每个用来标识进程的进程控制块PCB用结构来描述,包括以下字段:(1)进程标识ID,其中0为闲逛进程,用户进程标识数为1,2,3…。

(2)进程优先级Priority,闲逛进程(idle)的优先级为0,用户进程的优先级大于0,且随机产生,标识数越大,优先级越高。

(3)进程占用CPU时间CPUtime,进程每运行一次,累计值等于4.

(4)进程总共需要运行时间Alltime,利用随机函数产生。

(5)进程状态,0-就绪态;1-运行态;2-阻塞态。

(6)队列指针next,用来将多个进程控制块PCB链接为队列。

3)优先数改变的原则

(1)进程在就绪队列中每呆一个时间片,优先数增加1。

(2)进程每运行一个时间片,优先数减3.

4)在调度前,系统中拥有的进程数PCB_number由键盘输入,经初始化后,所有的进程控制块PCB链接成就绪队列。

3.实验步骤

a)画出程序流程图

a)动态优先权的进程调度算法模拟流程

b)轮转法进程调度算法模拟流程

b)程序算法如下:

#include "stdafx.h"

#define NULL 0

#include

#include

#include

using namespace std;

/*进程PCB结构*/

struct PCB

{

int ID; //进程标识

int priority; //优先级

int CPUtime; // 进程占用CPU的时间

int ALLtime; // 进程总共需要运行的时间

int State; // 进程状态

struct PCB *next; // 指向下一节点的指针

};

typedef struct PCB pcb;

void init(); //产生idle进程,输入用户进程数目,

//调用insert()

void print(PCB *pcb); //输出进程属性信息

void print_init(PCB *pcb);

void insert(); //生成进程属性信息,插入进程就绪队列

void run_priority(PCB *pcb); //运行进程,随机阻塞进程,产生新进程,插入就绪队列,唤醒阻塞进程

void run_loop(PCB *pcb); //运行进程,随机阻塞进程,产生新进程,插入就绪队列,唤醒阻塞进程

void block(PCB *pcb); //调用destroy(),将进程插入阻塞队列

void wakeup_priority(); //唤醒进程,插入就绪队列

void wakeup_loop(); //唤醒进程,插入就绪队列

void proc_priority(); //优先权调度算法模拟

void proc_loop(); //轮转法调度算法模拟

void update(PCB *pcb);//更新进程信息

PCB *ready_queue,*block_queue,*idleprocess; //就绪队列,阻塞队列及闲逛进程指针变量

int main(int argc, char* argv[])

{

int i=0;

while(1)

{

cout<<("\n Please select a number (1,2,0)");

cout<<("\n 1--priority");

cout<<("\n 2--loop");

cout<<("\n 0--exit\n");

cin>>i;

if(i==1)

{

cout<<("\nThis is an example for priority processing:\n");

init();

::proc_priority();

}

else if(i==2)

{

cout<<("\nThis is an example for round robin processing:\n");

init();

proc_loop();

}

else if(i==0)

exit(1);

}

return 0;

}

//输出所有PCB的初始值,此函数用于测试程序

void print_init(PCB *pcb)

{

PCB *temp=pcb->next ;

cout<<("\n ID priority CPUtime ALLtime

State\n");

while(temp!=NULL)

{

cout<<"\n"<<" "<ID<<" "<priority <<"

"<CPUtime <<" "<ALLtime;

if(temp->State ==0)

cout<<(" ready");

else if(temp->State ==1)

cout<<(" running");

else

cout<<(" blocked");

temp=temp->next ;

}

}

//输出进程属性信息

void print(PCB *pcb)

{

PCB *temp;

temp=pcb;

if(pcb->ID ==0)

cout<<("\n\t The idle process is running!");

else

{

cout<<"\n"<<" "<ID<<" "<priority <<" "<CPUtime <<" "<ALLtime;

if(temp->State ==0)

cout<<(" ready");

else if(temp->State ==1)

cout<<(" running");

else

cout<<(" blocked");

}

}

void insert_queue(PCB *queue,PCB *item)

{//将item插入到队列中,使得插入后队列中按照优先级从高到低有序

PCB *p,*q;

q=queue;

p=q->next ;

while(p!=0 && p->priority >= item->priority )

{

相关文档
最新文档