操作系统实验-处理器调度-时间片轮转法[源码]

刚刚做完实验,归来存下好用



#include
#include
#include
#include

typedef struct node
{
char name[10];
int pr;
int round;
int already;
int need;
int count;
char state;
struct node *next;
}PCB;
PCB *finish,*ready,*tail,*run; //各种队列指针
int N; //进程数量

void firstin()
{
run=ready;
run->state=’R';
ready=ready->next;
}
//输出函数
void main1(char a)
{
if(toupper(a)==’P') //优先级法
cout<<" "< cout<<"进程名 已用时间 还需时间 轮转时间片 当前状态"< }

//进程PCB输出
void main2(char a,PCB *q)
{
if(toupper(a)=='P') //优先级法的输出
cout<name<<" "<already<<" "<need<<" "<<
q->round<<" "<state< }

//输出函数
void prt(char sign)
{
PCB *p;
main1(sign); //输出标题
if(run!=NULL) //如果运行指针不空
main2(sign,run); //输出当前正在运行的PCB
p=ready; //输出就绪队列PCB
while(p!=NULL)
{
main2(sign,p);
p=p->next;
}
p=finish; //输出完成队列的PCB
while(p!=NULL)
{
main2(sign,p);
p=p->next;
}
getchar(); //按住任意键继续
}

//时间片轮转的插入算法
void insert(PCB *q)
{
PCB *p1,*s,*r;
s=q; //待插入的PCB指针
p1=ready; //就绪队列头指针
r=p1; //*r做pl的前驱指针
while(p1!=NULL)
if(p1->round<=s->round)
{
r=p1;
p1=p1->next;
}
if(r!=p1)
{
r->next=s;
s->next=p1;
}
else
{
s->next=p1; //否则插入在就绪队列的头
ready=s;
}
}

//优先级创建初始化
void create(char alg)
{
PCB *p;
int i,time;
char na[10];
ready=NULL;
finish=NULL;
run=NULL;
cout<<"请输入 第1个 进程名 和 其所需CPU时间:"< for(i=1;i<=N;i++)
{
p=new PCB;
cin>>na;
cin>>time;
strcpy(p->name,na);
p->already=0;
p->need=time;
p->state=’R';

p->round=0;
if(ready!=NULL)
insert(p);
else
{
p->next=ready;
ready=p;
}
cout<<"请输入 第"< }
prt(alg);
run=ready;
ready=ready->next;
run->state=’R';
}

void timeslicecycle(char alg)
{
while(run!=NULL)
{
run->already=run->already+1;
run->need=run->need-1;
run->round=run->round+1;
if(run->need<=0)
{
run->next=finish;
finish=run;
run->state=’E';
run=NULL;
if(ready!=NULL)
firstin();
}
else
{
run->state=’R';
insert(run);
firstin();
}
prt(alg);
}
}

//入口
void main()
{
char sign=’P';
cout<<"请输入总进程数目:";
cin>>N;
create(sign);
timeslicecycle(sign); //调度算法
}

版权所有© HzlzH | 本文采用 BY-NC-SA 进行授权
转载需注明 转自: 《操作系统实验-处理器调度-时间片轮转法[源码]

随机文章

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

:wink: :twisted: :roll: :oops: :mrgreen: :lol: :idea: :evil: :cry: :arrow: :?: :-| :-x :-o :-P :-D :-? :) :( :!: 8-O 8)