📄 lun.cpp
字号:
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "time.h"
#include <iostream.h>
int num=0;
typedef struct node
{
char name[10]; /*进程标识符*/
int round; /*进程时间轮转时间片*/
int cputime; /*进程占用CPU时间*/
int needtime; /*进程到完成还要的时间*/
int count; /*计数器*/
char state; /*进程的状态*/
struct node *next; /*链指针*/
int ftime;
int T;
int W;
}PCB;
PCB *finish,*ready,*tail,*run; /*队列指针*/
int N; /*进程数*/
char *output[100];
int point=0;
/*将就绪队列中的第一个进程投入运行*/
void firstin()
{
run=ready; /*就绪队列头指针赋值给运行头指针*/
run->state='R'; /*进程状态变为运行态*/
ready=ready->next; /*就绪对列头指针后移到下一进程*/
}
void prt1()
{
printf(" name cputime needtime count round state\n");
}
/*进程PCB输出*/
void prt2(PCB *q)
{
printf(" %-10s%-10d%-10d%-10d%-10d %-c\n",q->name,
q->cputime,q->needtime,q->count,q->round,q->state);
}
/*输出函数*/
void prt()
{
PCB *p;
prt1(); /*输出标题*/
if(run!=NULL) /*如果运行指针不空*/
prt2(run); /*输出当前正在运行的PCB*/
p=ready; /*输出就绪队列PCB*/
while(p!=NULL)
{
prt2(p);
p=p->next;
}
p=finish; /*输出完成队列的PCB*/
while(p!=NULL)
{
prt2(p);
p=p->next;
}
}
/*插入函数*/
void insert2(PCB *p2)
{
tail->next=p2; /*将新的PCB插入在当前就绪队列的尾*/
tail=p2;
p2->next=NULL;
}
/*创建进程PCB*/
void create()
{
PCB *p;
int i,time;
char na[10];
ready=NULL;
finish=NULL;
run=NULL;
printf("请输入进程名和执行时间\n");
for(i=1;i<=N;i++)
{
p=(PCB*)malloc(sizeof(PCB));
scanf("%s",na);
scanf("%d",&time);
strcpy(p->name,na);
p->cputime=0;
p->needtime=time;
p->count=0;
p->state='w';
p->round=2;
p->ftime=0;
p->T=0;
p->W=0;
if(ready!=NULL)
insert2(p);
else
{
p->next=ready;
ready=p;
tail=p;
}
}
printf(" 轮转法处理机调度过程\n");
printf("************************************************\n");
prt(); /*输出进程PCB信息*/
run=ready; /*将就绪队列的第一个进程投入运行*/
ready=ready->next;
run->state='R';
}
/*时间片轮转法*/
void roundrun()
{
while(run!=NULL)
{
run->cputime=run->cputime+1;
num++;
run->needtime=run->needtime-1;
run->count=run->count+1;
if(run->needtime==0)/*运行完将其变为完成态,插入完成队列*/
{
output[point]=run->name;
point++;
run->next=finish;
finish=run;
run->ftime=num;
run->state='F';
run=NULL;
if(ready!=NULL)
firstin(); /*就绪对列不空,将第一个进程投入运行*/
prt();
}
else
if(run->count==run->round) /*如果时间片到*/
{
run->count=0; /*计数器置0*/
output[point]=run->name;
point++;
if(ready!=NULL) /*如就绪队列不空*/
{
run->state='W'; /*将进程插入到就绪队列中等待轮转*/
insert2(run);
firstin(); /*将就绪对列的第一个进程投入运行*/
}
prt();
}
}
}
/*主函数*/
void main()
{
int i=0;
for(;i<100;i++)
{
output[i]="";
}
int temp=0,temp2=0;
srand((unsigned)time(NULL));
while(N<2 || N>6)
N=(int)(100*rand()/(RAND_MAX+1.0))+1;
printf("进程数为%d\n",N);
create(); /*轮转法*/
roundrun();
while(finish->next!=NULL)
{
finish->T=finish->ftime;
temp+=finish->T;
finish->W=finish->ftime/finish->cputime;
temp2+=finish->W;
finish=finish->next;
}
finish->T=finish->ftime;
temp+=finish->T;
finish->W=finish->ftime/finish->cputime;
temp2+=finish->W;
cout<<"进程的执行顺序是:";
for(i=0;i<point-1;i++)
{
cout<<output[i]<<"->";
}
cout<<output[i]<<endl;
printf("平均周转时间=%f\n平均带权周转时间=%f\n",(float)temp/(float)N,(float)temp2/(float)N);
getchar();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -