实验室老是中毒,今天差点连代码都保不住了
贴贴备份
进程调度 实验一 RR 时间片轮转
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<iostream>
using namespace std;
#define getch(type)(type*)malloc(sizeof(type))
#define null 0
#define MAXN 1000
int stack[MAXN];
int top;
struct pcb{//调用进程模块
char name[10];//进程名
char state; //进程状态: 就绪,运行,完成
int super; //进程优先级
int ntime;//需要运行的时间
int rtime;//到达时间 change
struct pcb* link;//链接指针
}*ready=null,*p;
typedef struct pcb PCB;
void destroy();
void check();
void sort()//建立对进程优先级排列函数
{
PCB *first,*second;
int insert = 0;
if( ( ready == null ) || ( ( p -> rtime ) < ( ready -> rtime ) ) ){//优先级最大者,插入队首
p -> link = ready;
ready = p;
}
else {//进程比较优先级。插入适当的位置中
first = ready;
second = first -> link;
while( second != null){
if( (p -> rtime ) < (second -> rtime)){//come ing time
//插入当前进程前面
p -> link = second;
first -> link = p;
second = null;
insert = 1;
}
else{
first = first -> link;
second = second -> link;
}
}
if( insert == 0) first -> link =p;
}
return ;
}
PCB *q;
PCB *front,*rear;
void RR()
{
int time=0;
q=ready;
front=ready;
rear =ready;
while(rear->link != null) rear= rear -> link;
while( front){
printf("Running Time : %d\n",++time);
if(front -> rtime <= time){
front -> state ='R';
front -> ntime--;//need time minus 1
printf("\n *** 当前正在运行的进程:%s\n",front->name);
if( front -> ntime == 0){// the one which is finished
p = front;
if(front -> link !=null )
front =front ->link;
else {printf("\n finished\n");break;}
p -> link = null;
destroy();
}
else{//else unfinished ,connect last
rear -> link = front;
p=front;
if(front -> link !=null )
front =front ->link;
else {printf("\n finished\n");break;}
p -> link = null;
}
check();//display all pcbs in queue
}
}
return ;
}
void input()
{
int i,num;
system("cls");
//clrscr();
printf("\n 请输入进程数量");
scanf("%d",&num);
for( i = 0;i < num; i++){
printf(" 进程号No.%d",i);
p=getch(PCB);
printf("\n 输入进程名:");
scanf("%s",p->name);
printf("\n 到达时间:");
scanf("%d",&p->rtime);
printf("\n 输入进程运行时间:");
scanf("%d",&p->ntime);
printf("\n");
p -> super =0; p -> state='W';
p -> link = null;
sort();//调用函数
}
// check();
return;
}
int space()
{
int l=0;
PCB *pr=ready;
while(pr != null){
l++;
pr = pr->link;
}
return l;
}
void disp(PCB *pr)
{
printf("\n qname\t state \t super \t ntime \t rtime\n");
printf("| %s\t",pr->name);
printf("| %c\t",pr->state);
printf("| %d\t",pr->super);
printf("| %d\t",pr->ntime);
printf("| %d\t",pr->rtime);
printf("\n");
return ;
}
void check()//建立进程查看函数
{
// PCB *pr;
/**//* printf("\n *** 当前正在运行的进程:%s",p->name);显示当前 运行 进程
disp(p);*/
p=front;
printf("\n ****当前就绪队列状态:%c",p->state);/**//* 显示队列状态 */
while( p != null){
disp(p);
p=p->link;
}
return ;
}
void destroy()/**//*建立进程撤销 函数 (进程 运行结束,撤销进程)*/
{
printf("\n 进程[%s]已完成。\n",p->name);
free(p);
return;
}
/**//*void running()//进程就绪函数
{
(p->rtime)++;
if( p-> rtime == p -> ntime)//
destroy();
else{
(p->super)--;
p->state = 'W';
sort();
}
return;
}
*/
int main()
{
freopen("in.txt","r",stdin);
int len,h=0;
// char ch;
input();
len=space();
RR();
/**//* while((len != 0) && ( ready !=null)){
ch=getchar();
h++;
printf("\n The execute number :%d\n",h);
p= ready;
ready = p->link;
p->link=null;//吧最前的拿出来
p->state ='R';
check();
running();
// system("pause");
printf("\n 按任意键继续.");
ch=getchar();
}*/
printf("\n\n进程已经完成。\n");
system("pause");
return 0;
}
posted on 2008-11-05 20:43
爬 阅读(4238)
评论(5) 编辑 收藏 引用