操作系统-进程调度实验.docx
《操作系统-进程调度实验.docx》由会员分享,可在线阅读,更多相关《操作系统-进程调度实验.docx(21页珍藏版)》请在冰豆网上搜索。
实验五进程调度模拟实验
//进程调度算法proc.c
#include
#include
#include
typedefstructpcb //定义PCB结构
{
charname[20];/*进程标识符*/
intcputime;/*进程占用CPU时间*/
intprio;/*进程优先数*/
intneedtime;/*进程到完成还需要的CPU时间*/
structpcb*next;/*链指针*/
}PCB;
PCB*RUN,*READY,*RTAIL,*FINSH,*FTAIL;
voidPRINTLINK(intt)/*输出3个队列*/
{
PCB*p;
printf("CPU运行次数:
___%d___\n",t);
printf("______________________\n");
printf("进程名\t运行状态\t运行次数\t还需要运行次数\n");
if(RUN!
=NULL)
{
printf("%s\t运行\t%d\t%d\n",RUN->name,RUN->cputime,RUN->needtime);
}
else
printf("*运行状态为空\n");
p=READY;
if(p!
=NULL)
{
while(p!
=NULL)
{
printf("%s\t就绪\t%d\t%d\n",p->name,p->cputime,p->needtime);
p=p->next;
}
}
else
printf("*就绪队列为空\n");
p=FINSH;
if(p!
=NULL)
{
while(p!
=NULL)
{
//printf("进程名字为:
%s\n",p->name);
printf("%s\t完成\t%d\t%d\n",p->name,p->cputime,p->needtime);
p=p->next;
}
}
else
printf("*完成队列为空\n");
getchar();
}
PCB*CPCBLINK()/*建立就绪队列*/
{
printf("建立就绪队列\n\n");
inti,n,nt,pr;
PCB*p,*q,*head;
n=0;
while
(1)
{
printf("请输入进程的个数(有效范围1-100):
");
scanf("%d",&n);
printf("\n");
if(n>=1&&n<=100)
break;
else
printf("输入有误。
请重新输入!
\n");
getchar();
}
head=(structpcb*)malloc(sizeof(structpcb));
printf("输入第1个进程的名称:
");
scanf("%s",head->name);
while
(1)
{
printf("需要的运行时间:
");
scanf("%d",&nt);
if(nt>0)
break;
else
{
printf("输入无效,重新输入!
\n");
getchar();
}
}
head->needtime=nt;
printf("优先数:
");
scanf("%d",&pr);
head->prio=pr;
head->cputime=0;/*进程已获得的运行时间*/
head->next=NULL;
q=head;
for(i=1;i {
printf("\n");
p=(structpcb*)malloc(sizeof(structpcb));
printf("输入第%d进程的名称:
",i+1);
scanf("%s",p->name);
printf("需要的运行时间:
");
scanf("%d",&nt);
p->needtime=nt;
printf("优先数:
");
scanf("%d",&pr);
p->prio=pr;
p->cputime=0;/*进程已获得的运行时间*/
p->next=NULL;
q->next=p;
q=p;
}
RTAIL=q;
returnhead;
}
voidJXDLPX()/*就绪队列按优先级从大到小排序*/
{
PCB*p,*q,*t;
chars[10];
intL=0,ct,pr,nt;
p=READY;
t=(structpcb*)malloc(sizeof(structpcb));
while(p->next!
=NULL)
{
L=0;
q=p->next;
t=p;
while(q!
=NULL)
{
if(t->prioprio)
{
t=q;
L=1;/*表示有比它优先级大的进程*/
}
q=q->next;
}
if(L==1)
{
strcpy(s,t->name);
ct=t->cputime;
pr=t->prio;
nt=t->needtime;
q=p->next;
while(strcmp(q->name,s)!
=0)
q=q->next;
strcpy(q->name,p->name);
q->cputime=p->cputime;
q->prio=p->prio;
q->needtime=p->needtime;
strcpy(p->name,s);
p->cputime=ct;
p->prio=pr;
p->needtime=nt;
}
p=p->next;
}
}
voidYXS()/*调用优先数调度算法*/
{
PCB*p;
intt=0,nt,ct,pr;
printf("您选择的是:
优先级调度算法\n");
READY=CPCBLINK();/*建立就绪队列*/
p=(structpcb*)malloc(sizeof(structpcb));
while(READY!
=NULL)
{
JXDLPX();/*就绪队列按优先级从大到小排序*/
p=READY;
READY=READY->next;
p->next=NULL;
pr=p->prio;
pr=pr-3;
p->prio=pr;/*运行1次进程优先级缩小3*/
nt=p->needtime;
nt=nt-1;
p->needtime=nt;
ct=p->cputime;
ct=ct+1;
p->cputime=ct;
RUN=p;
PRINTLINK(t);/*输出3个队列*/
if(RUN->needtime<=0)/*若运行结束进入完成队列*/
{
if(FINSH==NULL)/*第1次进入完成队列*/
{
FINSH=p;
FTAIL=p;
}
else
{
FTAIL->next=p;
FTAIL=FTAIL->next;
}
RUN=NULL;
}
else/*若运行没结束进入就绪队列*/
{
if(READY==NULL)/*当就绪队列为空*/
{
READY=p;
RTAIL=p;
}
else
{
RTAIL->next=p;
RTAIL=p;
}
RUN=NULL;
}
t++;
}
}
voidSJP()/*调用时间片循环轮转算法*/
{
PCB*p;
printf("您选择的是:
时间片循环轮转调度算法\n");
intt=0,nt,ct;
READY=CPCBLINK();/*建立就绪队列*/
p=(structpcb*)malloc(sizeof(structpcb));
while(READY!
=NULL)
{
p=READY;
READY=READY->next;
p->next=NULL;
nt=p->needtime;
nt=nt-2;
if(nt<0)
nt=0;
p->needtime=nt;
ct=p->cputime;
ct=ct+2;
p->cputime=ct;
RUN=p;
PRINTLINK(t);/*输出3个队列*/
if(RUN->needtime<=0)/*若运行结束进入完成队列*/
{
if(FINSH==NULL)/*第1次进入完成队列*/
{
FINSH=p;
FTAIL=p;
}
else
{
FTAIL->next=p;
FTAIL=FTAIL->next;
}
RUN=NULL;
}
else/*若运行没结束进入就绪队列*/
{
if(READY==NULL)/*当就绪队列为空*/
{
READY=p;
RTAIL=p;
}
else
{
RTAIL->next=p;
RTAIL=p;
}
RUN=NULL;
}
t++;
}
}
/*主程序*/
intmain()
{
intN;
RUN=(structpcb*)malloc(sizeof(structpcb));
while
(1)
{
RUN=NULL;
READY=NULL;
RTAIL=NULL;
FINSH=NULL;
FTAIL=NULL;
printf("========