时间片轮转算法实现

#include<string.h>

#include<stdio.h>

#define NULL 0

typedef struct quen /*定义结构*/

{

char pname[8];

int time1;

int time2;

char state;

struct quen *next;

}QUEN;

main()/*主程序*/

{

QUEN *q,*p,*head,*m;

char str[8],f;

int t,d,n;

system("clear");

printf("Enter the maxnumber of nodes(n):\n");/*输入进程数*/

scanf("%d",&n);

d=n;

if(d>0)

{

printf("enter thepname:");

scanf("%s",str);

printf("enter the need time:");

scanf("%d",&t);

head=p=(QUEN *)malloc(sizeof(QUEN));

strcpy(p->pname,str);

p->time1=t;

p->time2=0;

p->state='R';

p->next=NULL;

head=p;

getchar();

--d;}

while(d>0)

{/*构建队列表*/

printf("enter the pname:");

scanf("%s",str);

printf("enter need time:");

scanf("%d",&t);

q=(QUEN *)malloc(sizeof(QUEN));

strcpy(q->pname,str);

q->time1=t;

q->time2=0;

q->state='R';

q->next=NULL;

p->next=q;

p=q;

--d;

p->next=head;

q=head;}

printf("process name need time runned static\n");

do{

printf(" %s%d %d %c\n",q->pname,q->time1,q->time2,q->state);

q=q->next;

}while(q!=head);

printf("\n");

do{

if(head->time2<head->time1)

{head->time2++;

if(head->time2==head->time1)

{

head->state='E';

q=head;

printf("The running process is %s\n",q->pname);

printf("process name left time runned static\n");

do{

/*输入队列表*/

printf(" %s %d %d %c\n",q->pname,q->time1,q->time2,q->state);

q=q->next;}

while(q!=head);

printf("\n");

head=head->next;

q=head;

p->next=head;

}

else{

printf("The running process is %s\n",q->pname);

printf("process name left time runned static\n");

do {

printf("%s%d%d %c\n",q->pname,q->time1,q->time2,q->state);

q=q->next;}while(q!=head);

printf("\n");

head=head->next;

q=head;

p=p->next;}

printf("Is it needing new process?(y or n)\n");/*是否加入新的进程*/

getchar();

scanf("%c",&f);

if(f=='Y'||f=='y'){

getchar();

printf("Enter the new pname:");

scanf("%s",str);

printf("Enter the new neededtime:");

scanf("%d",&t);

m=(QUEN

*)malloc(sizeof(QUEN));

strcpy(m->pname,str);

m->time1=t;

m->time2=0;

m->state='R';

m->next=NULL;

if(q->next->state=='E')

{p=m;

head=m;

p->next=head;

q=head;}

else{p->next=m;

m->next=head;

p=m;}}

}}while(q->next->state!='E');

printf("The processes are finished\n");

}

实验结果:

 

 

程序说明:在轮转算法中,系统根据FCFS策略,将所有的就绪进程排成一个就绪队列,并可设置每隔一定时间间隔(如30ms)即产生一次中断,激活系统中的进程调度程序,完成一次调度,将CPU分配给队首进程,令其执行。当该进程的时间片耗尽或运行完毕时,系统再次将CPU分配给新的队首进程(或新到达的紧迫进程)。由此,可以保证就绪队列中的所有进程在一个确定的时间段内,都能够获得一次CPU执行。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值