?? lun.cpp
字號:
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "time.h"
#include <iostream.h>
int num=0;
typedef struct node
{
char name[10]; /*進程標識符*/
int round; /*進程時間輪轉(zhuǎn)時間片*/
int cputime; /*進程占用CPU時間*/
int needtime; /*進程到完成還要的時間*/
int count; /*計數(shù)器*/
char state; /*進程的狀態(tài)*/
struct node *next; /*鏈指針*/
int ftime;
int T;
int W;
}PCB;
PCB *finish,*ready,*tail,*run; /*隊列指針*/
int N; /*進程數(shù)*/
char *output[100];
int point=0;
/*將就緒隊列中的第一個進程投入運行*/
void firstin()
{
run=ready; /*就緒隊列頭指針賦值給運行頭指針*/
run->state='R'; /*進程狀態(tài)變?yōu)檫\行態(tài)*/
ready=ready->next; /*就緒對列頭指針后移到下一進程*/
}
void prt1()
{
printf(" name cputime needtime count round state\n");
}
/*進程PCB輸出*/
void prt2(PCB *q)
{
printf(" %-10s%-10d%-10d%-10d%-10d %-c\n",q->name,
q->cputime,q->needtime,q->count,q->round,q->state);
}
/*輸出函數(shù)*/
void prt()
{
PCB *p;
prt1(); /*輸出標題*/
if(run!=NULL) /*如果運行指針不空*/
prt2(run); /*輸出當前正在運行的PCB*/
p=ready; /*輸出就緒隊列PCB*/
while(p!=NULL)
{
prt2(p);
p=p->next;
}
p=finish; /*輸出完成隊列的PCB*/
while(p!=NULL)
{
prt2(p);
p=p->next;
}
}
/*插入函數(shù)*/
void insert2(PCB *p2)
{
tail->next=p2; /*將新的PCB插入在當前就緒隊列的尾*/
tail=p2;
p2->next=NULL;
}
/*創(chuàng)建進程PCB*/
void create()
{
PCB *p;
int i,time;
char na[10];
ready=NULL;
finish=NULL;
run=NULL;
printf("請輸入進程名和執(zhí)行時間\n");
for(i=1;i<=N;i++)
{
p=(PCB*)malloc(sizeof(PCB));
scanf("%s",na);
scanf("%d",&time);
strcpy(p->name,na);
p->cputime=0;
p->needtime=time;
p->count=0;
p->state='w';
p->round=2;
p->ftime=0;
p->T=0;
p->W=0;
if(ready!=NULL)
insert2(p);
else
{
p->next=ready;
ready=p;
tail=p;
}
}
printf(" 輪轉(zhuǎn)法處理機調(diào)度過程\n");
printf("************************************************\n");
prt(); /*輸出進程PCB信息*/
run=ready; /*將就緒隊列的第一個進程投入運行*/
ready=ready->next;
run->state='R';
}
/*時間片輪轉(zhuǎn)法*/
void roundrun()
{
while(run!=NULL)
{
run->cputime=run->cputime+1;
num++;
run->needtime=run->needtime-1;
run->count=run->count+1;
if(run->needtime==0)/*運行完將其變?yōu)橥瓿蓱B(tài),插入完成隊列*/
{
output[point]=run->name;
point++;
run->next=finish;
finish=run;
run->ftime=num;
run->state='F';
run=NULL;
if(ready!=NULL)
firstin(); /*就緒對列不空,將第一個進程投入運行*/
prt();
}
else
if(run->count==run->round) /*如果時間片到*/
{
run->count=0; /*計數(shù)器置0*/
output[point]=run->name;
point++;
if(ready!=NULL) /*如就緒隊列不空*/
{
run->state='W'; /*將進程插入到就緒隊列中等待輪轉(zhuǎn)*/
insert2(run);
firstin(); /*將就緒對列的第一個進程投入運行*/
}
prt();
}
}
}
/*主函數(shù)*/
void main()
{
int i=0;
for(;i<100;i++)
{
output[i]="";
}
int temp=0,temp2=0;
srand((unsigned)time(NULL));
while(N<2 || N>6)
N=(int)(100*rand()/(RAND_MAX+1.0))+1;
printf("進程數(shù)為%d\n",N);
create(); /*輪轉(zhuǎn)法*/
roundrun();
while(finish->next!=NULL)
{
finish->T=finish->ftime;
temp+=finish->T;
finish->W=finish->ftime/finish->cputime;
temp2+=finish->W;
finish=finish->next;
}
finish->T=finish->ftime;
temp+=finish->T;
finish->W=finish->ftime/finish->cputime;
temp2+=finish->W;
cout<<"進程的執(zhí)行順序是:";
for(i=0;i<point-1;i++)
{
cout<<output[i]<<"->";
}
cout<<output[i]<<endl;
printf("平均周轉(zhuǎn)時間=%f\n平均帶權(quán)周轉(zhuǎn)時間=%f\n",(float)temp/(float)N,(float)temp2/(float)N);
getchar();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -