實(shí)驗(yàn)室老是中毒,今天差點(diǎn)連代碼都保不住了
貼貼備份
進(jìn)程調(diào)度 實(shí)驗(yàn)一 RR 時(shí)間片輪轉(zhuǎn)

#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
{//調(diào)用進(jìn)程模塊
char name[10];//進(jìn)程名
char state; //進(jìn)程狀態(tài): 就緒,運(yùn)行,完成
int super; //進(jìn)程優(yōu)先級
int ntime;//需要運(yùn)行的時(shí)間
int rtime;//到達(dá)時(shí)間 change
struct pcb* link;//鏈接指針
}*ready=null,*p;
typedef struct pcb PCB;
void destroy();
void check();

void sort()//建立對進(jìn)程優(yōu)先級排列函數(shù)


{
PCB *first,*second;
int insert = 0;

if( ( ready == null ) || ( ( p -> rtime ) < ( ready -> rtime ) ) )
{//優(yōu)先級最大者,插入隊(duì)首
p -> link = ready;
ready = p;
}

else
{//進(jìn)程比較優(yōu)先級。插入適當(dāng)?shù)奈恢弥?/span>
first = ready;
second = first -> link;

while( second != null)
{

if( (p -> rtime ) < (second -> rtime))
{//come ing time
//插入當(dāng)前進(jìn)程前面
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 *** 當(dāng)前正在運(yùn)行的進(jì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 請輸入進(jìn)程數(shù)量");
scanf("%d",&num);

for( i = 0;i < num; i++)
{
printf(" 進(jìn)程號No.%d",i);
p=getch(PCB);
printf("\n 輸入進(jìn)程名:");
scanf("%s",p->name);
printf("\n 到達(dá)時(shí)間:");
scanf("%d",&p->rtime);
printf("\n 輸入進(jìn)程運(yùn)行時(shí)間:");
scanf("%d",&p->ntime);
printf("\n");
p -> super =0; p -> state='W';
p -> link = null;
sort();//調(diào)用函數(shù)
}
// 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()//建立進(jìn)程查看函數(shù)


{
// PCB *pr;

/**//* printf("\n *** 當(dāng)前正在運(yùn)行的進(jìn)程:%s",p->name);顯示當(dāng)前 運(yùn)行 進(jìn)程
disp(p);*/
p=front;

printf("\n ****當(dāng)前就緒隊(duì)列狀態(tài):%c",p->state);/**//* 顯示隊(duì)列狀態(tài) */

while( p != null)
{
disp(p);
p=p->link;
}
return ;
}


void destroy()/**//*建立進(jìn)程撤銷 函數(shù) (進(jìn)程 運(yùn)行結(jié)束,撤銷進(jìn)程)*/


{
printf("\n 進(jìn)程[%s]已完成。\n",p->name);
free(p);
return;
}


/**//*void running()//進(jìn)程就緒函數(shù)
{
(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 按任意鍵繼續(xù)
.");
ch=getchar();
}*/
printf("\n\n進(jìn)程已經(jīng)完成。\n");

system("pause");
return 0;
}

posted on 2008-11-05 20:43
爬 閱讀(4263)
評論(5) 編輯 收藏 引用