您的位置:首页 > 编程语言 > C语言/C++

操作系统多级反馈轮转法的c语言模拟

2015-11-18 22:31 471 查看
#include <stdio.h>

#include <stdlib.h>

enum Status{
RUN,OVER,HEAD

};

struct PCB

{
char name[5];
int runtime;
int curtime;
int prior;
Status type;
PCB* next;
PCB* prev;

};

PCB* head;

int pnum;

void init();

void showinfo(int round,PCB* pcur,int pnum);

void Run();

PCB* getMaxPrior(PCB* P);

void init()

{
head=(PCB*)malloc(sizeof(PCB));
head->next=NULL;
head->type=HEAD;
PCB* p;
PCB* tmp;
int i;
printf("please input the process num!\n");
scanf("%d",&pnum);
p=head;
for(i=0;i<pnum;++i)
{
tmp=p;
p=(PCB*)malloc(sizeof(PCB));
printf("please input di %i ge process info",i);
scanf("%s",p->name);
scanf("%d",&p->runtime);
scanf("%d",&p->prior);
p->curtime=0;
p->type=RUN;
p->prev=tmp;
tmp->next=p;
}
p->next=head;

    head->prev=p;
PCB* reg=head->next;
reg->prev=head;
for (i=0;i<pnum;++i,reg=reg->next)
{

       if (reg->prior<reg->next->prior)

       {

          tmp=reg->next;
 for (;reg->prior<tmp->prior&®->type!=HEAD;reg=reg->prev)
 {
 }
 PCB*flag=reg->next;
 tmp->prev->next=tmp->next;
 tmp->next->prev=tmp->prev;
 reg->next=tmp;
 tmp->prev=reg;
 tmp->next=flag;
 flag->prev=tmp;

       }
}

}

void showinfo(int round,PCB* pcur,int pnum)

{
printf("now is %d,you %d\n",round,pnum);
int i;
PCB* p=pcur;
for(i=0;i<=pnum;i++)
{
if(p->type==RUN)
printf("di %s ge process,run time is %d,you xian ji is %d\n",p->name,p->curtime,p->prior);
p=p->next;
}

}

PCB* HeadMaxPrior(PCB*reg,int pnum)

{
int i;
PCB* head;
PCB*tmp;
//先从head开始

    for (;reg->type!=HEAD;reg=reg->next)

    {

    }

    head=reg;
reg=head->next;

    reg->prev=head;
for (i=0;i<pnum;++i,reg=reg->next)
{

if (reg->prior<reg->next->prior)
{
tmp=reg->next;
for (;reg->prior<tmp->prior&®->type!=HEAD;reg=reg->prev)
{
}
PCB*flag=reg->next;
tmp->prev->next=tmp->next;
tmp->next->prev=tmp->prev;
reg->next=tmp;
tmp->prev=reg;
tmp->next=flag;
flag->prev=tmp;
}
}
return head;

}

void Run()

{
int round=0;
PCB *ptr=head->next;

// PCB* prev=P;

    while(ptr->curtime<=ptr->runtime)
{
if (ptr->type!=HEAD)
{
round++;
ptr->curtime++;
ptr->prior--;

if(ptr->prior<0)
{
ptr->prior=0;
}
showinfo(round,ptr,pnum);
}
if(ptr->curtime==ptr->runtime&&ptr->type!=HEAD)
{
printf("%s is over********\n",ptr->name);
pnum--;
ptr->prev->next=ptr->next;
ptr->next->prev=ptr->prev;
if(pnum==0)
{
printf("run is over!!!!!!!!!!!! \n");
return;
}
ptr->type=OVER;
}

   ptr=HeadMaxPrior(ptr,pnum)->next;
}
//进行剔除已经运行完的进程

}

int main()

{
init();
Run();
return 0;
//system("pause");

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息