操作系统多级反馈轮转法的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");
}
#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");
}
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- 应用领航:盘点那些年我们一起追过的OS
- 无奇不有!盘点各国自己开发的操作系统
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- Lua下基本的网络编程示例
- 可自定义oem的萝卜家园 Ghost XP 新春装机版 V200801 下载
- C#实现判断操作系统是否为Win8以上版本
- C++联合体转换成C#结构的实现方法
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法