A. c语言,单处理机进程调度,时间片轮转
//参考一下
#include<stdio.h>
#include<stdlib.h>
#defineCPU_TIME50//CPU时间片
structmission//单个任务的结构体
{
charname[20];//任务名称
intfinished;//任务是否已完成,完成为1,未完成为0
intneed_time;//任务总共需要的CPU时间
intfinished_time;//任务已经执行的CPU时间
};
intwork_mission=5;//未完成任务计数
structmissions[5]={{"one",0,100,0},//假设有5个任务
{"two",0,380,0},
{"three",0,200,0},
{"four",0,440,0},
{"five",0,230,0}
};
intmove_mission(intflag)//任务排序
{
if(work_mission==0)
{
return1;
}
structmissiontemp;
temp=s[0];
inti;
if(flag==1)
{
for(i=1;i<work_mission+1;i++)
{
s[i-1]=s[i];
}
s[work_mission]=temp;
}
else
{
for(i=1;i<work_mission;i++)
{
s[i-1]=s[i];
}
s[work_mission-1]=temp;
}
return0;
}
intmain(intargc,char*argv[]){
structmissiontemp;
intfinished=0;
intcpu_time_count=0;
while(finished==0)
{
printf("
第%d个CPU时间片即将执行
",cpu_time_count/CPU_TIME+1);
printf("
当前任务队列(即将执行下面第一行任务,执行后将任务移到队列末尾):CPU已用时:%d
",cpu_time_count);
inti;
for(i=0;i<5;i++)
{
printf("任务名称:%5s是否已完成:%d需时间:%03d已执行时间:%03d已完成:%03.2f%%
",s[i].name,s[i].finished,s[i].need_time,s[i].finished_time,100*((float)s[i].finished_time/(float)s[i].need_time));
}
if(s[0].finished==1)
{
finished=1;
break;
}
if((s[0].need_time-s[0].finished_time)>CPU_TIME)
{
s[0].finished_time+=CPU_TIME;
cpu_time_count+=CPU_TIME;
finished=move_mission(0);
}
else
{
cpu_time_count+=(s[0].need_time-s[0].finished_time);
s[0].finished_time=s[0].need_time;
s[0].finished=1;
work_mission-=1;
finished=move_mission(1);
}
}
printf("
当前任务队列(全部执行完毕):CPU用时:%d
",cpu_time_count);
inti;
for(i=0;i<5;i++)
{
printf("任务名称:%5s是否已完成:%d需时间:%03d已执行时间:%03d已完成:%03.2f%%
",s[i].name,s[i].finished,s[i].need_time,s[i].finished_time,100*((float)s[i].finished_time/(float)s[i].need_time));
}
printf("
总共用了CPU时间:%d",cpu_time_count);
return0;
}
B. C语言模拟操作系统进程调度和管理
给,已经编译运行通过了,简单写的:
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
/*********************以下是全局数据结构和变量***********************/
/*PCB 结构*/
struct PCB{
int pname;
int pri;
int runtime;
int waittime;
struct PCB *next;
}pcb[7];
/* 运行指针*/
struct PCB *running;
/*高优先级就绪队列头指针*/
struct PCB *Hready;
/*低优先级队列头指针*/
struct PCB *Lready;
/*等待队列头指针*/
struct PCB *wait;
int sig=0;
/**************************以下是函数说明****************************/
/*利用循环实现延迟*/
void delay();
/*模拟进程3-9*/
void proc(struct PCB *running);
/*将node插入到head所指示的队列的尾部*/
void InsertIntoQueueTail(struct PCB ** head,struct PCB *node);
/*进程调度函数*/
int proc_switch();
/*进程等待函数*/
void proc_wait();
/*进程唤醒函数*/
int proc_wakeup();
/************************以下是函数定义及注释************************/
/*主函数*/
main()
{
int i;
/*初始化,创建进程3-9,置低优先级,等待时间为0,
依次插入低优先级队列*/
for(i = 0;i < 7;i++){
pcb[i].pname = i+3;
pcb[i].pri = 0;
pcb[i].waittime = 0;
InsertIntoQueueTail(&Lready,&pcb[i]);
}
/*等待队列和高优先级队列为空*/
wait = NULL;
Hready=NULL;
printf("\nThe process_switch begin:\n");
/*模拟进程调度开始*/
for(;;)
{
switch(sig){
case 0:/*无进程等待调度,打印信息并返回*/
if(!proc_switch())
{
printf("No Process to run,press any key to return:\n");
getchar();
}
break;
case 1:proc_wait();
break;
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:proc(running);
break;
default:printf("\nerror!");
exit(-1);
}
}
}
/*功能:延迟一个时间片*/
/*入口参数:无*/
/*出口参数:无*/
void delay()
{
int i,j;
for(i=0;i<20000;i++)
for(j=0;j<10000;j++)
{
}
}
/*功能:进程3-9*/
/*入口参数:运行指针*/
/*出口参数:无*/
void proc(struct PCB * running)
{
int i;
srand( (unsigned)time( NULL ) );
/*显示当前运行的进程的id*/
printf("\nNow Process %d is running\n",running->pname);
/*当前进程执行running->runtime个时间片*/
for(i=running->runtime;i>0;i--){
/*显示剩余的时间片*/
printf("%d time slice(s) left\n",i);
/*延迟*/
delay();
proc_wakeup();
/*产生一个1到1000的随机数,若该随机数小余100,当前进程等待,*/
if((rand()%1000+1)<100){
printf("Process %d begins to wait.\n",running->pname);
sig=1;
return;
}
}
/*显示时间片耗尽,进程转为低优先级就绪状态*/
printf("Time slices for process %d exhausted.\n",running->pname);
InsertIntoQueueTail(&Hready,running);
sig=0;
return;
}
/*功能:将一个节点插入队列尾部*/
/*入口参数:队列头指针地址head,待插入结点node*/
/*出口参数:无*/
void InsertIntoQueueTail(struct PCB **head,struct PCB *node)
{
struct PCB *p;
node->next=NULL;
/*被插入队列为空*/
if(*head==NULL){
*head=node;
return;
}
/*被插入队列不为空*/
else{
p=*head;
/*找到队列的最后一个结点*/
while(p->next!=NULL) p=p->next;
p->next=node;
}
}
/*功能:进程调度*/
/*入口参数:无*/
/*出口参数:若调度成功,返回1,否则返回0*/
int proc_switch()
{
/*若高优先级就绪队列和低优先级就绪队列均为空,则循环执行进程唤醒*/
while(Hready == NULL && Lready == NULL)
if(!proc_wakeup()) return 0;
/*若高优先级就绪队列非空,则执行其第一个进程,分配2个时间片*/
if(Hready != NULL){
running = Hready;
Hready = Hready -> next;
running->runtime = 2;
}
/*若高优先级就绪队列为空,则执行低优先级就绪队列的第一个进程,
分配5个时间片*/
else{
running = Lready;
Lready=Lready -> next;
running -> runtime = 5;
}
/*别调度进程的id赋给sig*/
sig = running -> pname;
return 1;
}
/*功能:进程等待。将当前运行进程置高优先级,等待时间为20,
插入等待队列尾部*/
/*入口参数:无*/
/*出口参数:无*/
void proc_wait()
{
struct PCB *p;
running->pri=1;
running->waittime=20;
InsertIntoQueueTail(&wait,running);
sig=0;
return;
}
/*功能:进程唤醒*/
/*入口参数:无*/
/*出口参数:若等待队列为空,则返回0,否则返回1*/
int proc_wakeup()
{
struct PCB *p,*last,*MoveToReady;
p = wait;
/*等待队列为空,返回0*/
if(p == NULL) return 0;
/*延迟*/
delay();
/*等待队列中每个进程的等待时间减1*/
while(p != NULL){
p -> waittime -= 1;
p=p->next;
}
p=wait;
/*从等待队列中摘除等待时间为0的进程,插入到高优先级就绪队列的尾部*/
while(p!=NULL){
if(p -> waittime == 0){
MoveToReady = p;
if (p == wait)
wait = p->next;
else
last -> next = p->next;
p = p -> next;
InsertIntoQueueTail(&Hready,MoveToReady);
}
else{
p = p -> next;
}
}
sig =0;
return 1;
}
C. 用C语言编写一段简单的程序,作业调度和低级调度算法
真不容易啊,怕是没人弄了!
优先级调度算法程序:
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
typedef struct node
{
char name[10]; /*进程标识符*/
int prio; /*进程优先数*/
int round; /*进程时间轮转时间片*/
int cputime; /*进程占用CPU时间*/
int needtime; /*进程到完成还要的时间*/
int count; /*计数器*/
char state; /*进程的状态*/
struct node *next; /*链指针*/
}PCB;
PCB *finish,*ready,*tail,*run; /*队列指针*/
int N; /*进程数*/
/*将就绪队列中的第一个进程投入运行*/
firstin()
{
run=ready; /*就绪队列头指针赋值给运行头指针*/
run->state='R'; /*进程状态变为运行态*/
ready=ready->next; /*就绪对列头指针后移到下一进程*/
}
/*标题输出函数*/
void prt1(char a)
{
if(toupper(a)=='P') /*优先数法*/
printf(" name cputime needtime priority state\n");
else
printf(" name cputime needtime count round state\n");
}
/*进程PCB输出*/
void prt2(char a,PCB *q)
{
if(toupper(a)=='P') /*优先数法的输出*/
printf(" %-10s%-10d%-10d%-10d %c\n",q->name,
q->cputime,q->needtime,q->prio,q->state);
else/*轮转法的输出*/
printf(" %-10s%-10d%-10d%-10d%-10d %-c\n",q->name,
q->cputime,q->needtime,q->count,q->round,q->state);
}
/*输出函数*/
void prt(char algo)
{
PCB *p;
prt1(algo); /*输出标题*/
if(run!=NULL) /*如果运行指针不空*/
prt2(algo,run); /*输出当前正在运行的PCB*/
p=ready; /*输出就绪队列PCB*/
while(p!=NULL)
{
prt2(algo,p);
p=p->next;
}
p=finish; /*输出完成队列的PCB*/
while(p!=NULL)
{
prt2(algo,p);
p=p->next;
}
getch(); /*压任意键继续*/
}
/*优先数的插入算法*/
insert1(PCB *q)
{
PCB *p1,*s,*r;
int b;
s=q; /*待插入的PCB指针*/
p1=ready; /*就绪队列头指针*/
r=p1; /*r做p1的前驱指针*/
b=1;
while((p1!=NULL)&&b) /*根据优先数确定插入位置*/
if(p1->prio>=s->prio)
{
r=p1;
p1=p1->next;
}
else
b=0;
if(r!=p1) /*如果条件成立说明插入在r与p1之间*/
{
r->next=s;
s->next=p1;
}
else
{
s->next=p1; /*否则插入在就绪队列的头*/
ready=s;
}
}
/*轮转法插入函数*/
insert2(PCB *p2)
{
tail->next=p2; /*将新的PCB插入在当前就绪队列的尾*/
tail=p2;
p2->next=NULL;
}
/*优先数创建初始PCB信息*/
void create1(char alg)
{
PCB *p;
int i,time;
char na[10];
ready=NULL; /*就绪队列头指针*/
finish=NULL; /*完成队列头指针*/
run=NULL; /*运行队列指针*/
printf("Enter name and time of process\n"); /*输入进程标识和所需时间创建PCB*/
for(i=1;i<=N;i++)
{
p=malloc(sizeof(PCB));
scanf("%s",na);
scanf("%d",&time);
strcpy(p->name,na);
p->cputime=0;
p->needtime=time;
p->state='w';
p->prio=50-time;
if(ready!=NULL) /*就绪队列不空调用插入函数插入*/
insert1(p);
else
{
p->next=ready; /*创建就绪队列的第一个PCB*/
ready=p;
}
}
clrscr();
printf(" output of priority:\n");
printf("************************************************\n");
prt(alg); /*输出进程PCB信息*/
run=ready; /*将就绪队列的第一个进程投入运行*/
ready=ready->next;
run->state='R';
}
/*轮转法创建进程PCB*/
void create2(char alg)
{
PCB *p;
int i,time;
char na[10];
ready=NULL;
finish=NULL;
run=NULL;
printf("Enter name and time of round process\n");
for(i=1;i<=N;i++)
{
p=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; /*时间片*/
if(ready!=NULL)
insert2(p);
else
{
p->next=ready;
ready=p;
tail=p;
}
}
clrscr();
printf(" output of round\n");
printf("************************************************\n");
prt(alg); /*输出进程PCB信息*/
run=ready; /*将就绪队列的第一个进程投入运行*/
ready=ready->next;
run->state='R';
}
/*优先数调度算法*/
priority(char alg)
{
while(run!=NULL) /*当运行队列不空时,有进程正在运行*/
{
run->cputime=run->cputime+1;
run->needtime=run->needtime-1;
run->prio=run->prio-3; /*每运行一次优先数降低3个单位*/
if(run->needtime==0) /*如所需时间为0将其插入完成队列*/
{
run->next=finish;
finish=run;
run->state='F'; /*置状态为完成态*/
run=NULL; /*运行队列头指针为空*/
if(ready!=NULL) /*如就绪队列不空*/
firstin(); /*将就绪对列的第一个进程投入运行*/
}
else /*没有运行完同时优先数不是最大,则将其变为就绪态插入到就绪队列*/
if((ready!=NULL)&&(run->prio<ready->prio))
{
run->state='W';
insert1(run);
firstin(); /*将就绪队列的第一个进程投入运行*/
}
prt(alg); /*输出进程PCB信息*/
}
}
/*时间片轮转法*/
roundrun(char alg)
{
while(run!=NULL)
{
run->cputime=run->cputime+1;
run->needtime=run->needtime-1;
run->count=run->count+1;
if(run->needtime==0)/*运行完将其变为完成态,插入完成队列*/
{
run->next=finish;
finish=run;
run->state='F';
run=NULL;
if(ready!=NULL)
firstin(); /*就绪对列不空,将第一个进程投入运行*/
}
else
if(run->count==run->round) /*如果时间片到*/
{
run->count=0; /*计数器置0*/
if(ready!=NULL) /*如就绪队列不空*/
{
run->state='W'; /*将进程插入到就绪队列中等待轮转*/
insert2(run);
firstin(); /*将就绪对列的第一个进程投入运行*/
}
}
prt(alg); /*输出进程信息*/
}
}
/*主函数*/
main()
{
char algo; /*算法标记*/
clrscr();
printf("type the algorithm:P/R(priority/roundrobin)\n");
scanf("%c",&algo); /*输入字符确定算法*/
printf("Enter process number\n");
scanf("%d",&N); /*输入进程数*/
if(algo=='P'||algo=='p')
{
create1(algo); /*优先数法*/
priority(algo);
}
else
if(algo=='R'||algo=='r')
{
create2(algo); /*轮转法*/
roundrun(algo);
}
}
D. 用C语言编写并调试一个模拟的进程调度程序,采用“简单时间片轮转法”调度算法对五个进程进行调度。
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
struct PCB {
char NAME[10]; /*进程名*/
int ROUND; /*进程轮转时间片*/
int REACHTIME; /*进程到达时间*/
int CPUTIME; /*进程占用CPU时间*/
int COUNT; /*计数器*/
int NEEDTIME; /*进程完成还要的CPU时间*/
char STATE; /*进程的状态*/
struct PCB *NEXT; /*链指针*/
};
struct LINK { /*PCB的链结构*/
struct PCB *RUN; /*当前运行进程指针*/
struct PCB *READY; /*就绪队列头指针*/
struct PCB *TAIL; /*就绪队列尾指针*/
struct PCB *FINISH; /*完成队列头指针*/
};
void INIT(LINK *); /*对PCB的链结构初始化*/
void INSERT(LINK *); /*将执行了一个单位时间片数且还未完成的进程的PCB插到就绪队列的队尾*/
void FIRSTIN(LINK *); /*将就绪队列中的第一个进程投入运行*/
void PRINT(LINK *); /*打印每执行一个时间片后的所有进程的状态*/
void PR(PCB *); /*打印一个进程的状态*/
int CREATE(LINK *,int); /*创建新的进程*/
void ROUNDSCH(LINK *); /*按时间片轮转法调度进程*/
void main() {
LINK pcbs;
int i;
INIT(&pcbs);
i=0;
printf("创建5个进程\n\n");
while(i<5) {
if(CREATE(&pcbs,i+1)==1) {
printf("进程已创建\n\n");
i++;
}
else
printf("进程创建失败\n\n");
}
FIRSTIN(&pcbs);
ROUNDSCH(&pcbs);
}
void ROUNDSCH(LINK *p) {
PCB *pcb;
while(p->RUN!=NULL) {
pcb=(PCB *)malloc(sizeof(PCB));
strcpy(pcb->NAME,p->RUN->NAME);
pcb->ROUND=p->RUN->ROUND;
pcb->REACHTIME=p->RUN->REACHTIME;
pcb->CPUTIME=p->RUN->CPUTIME;
pcb->COUNT=p->RUN->COUNT;
pcb->NEEDTIME=p->RUN->NEEDTIME;
pcb->STATE=p->RUN->STATE;
pcb->NEXT=p->RUN->NEXT;
pcb->CPUTIME++;
pcb->NEEDTIME--;
pcb->COUNT++;
if(pcb->NEEDTIME==0) {
pcb->NEXT=p->FINISH->NEXT;
p->FINISH->NEXT=pcb;
pcb->STATE='F';
p->RUN=NULL;
if(p->READY!=p->TAIL)
FIRSTIN(p);
}
else {
p->RUN=pcb;
if(pcb->COUNT==pcb->ROUND) {
pcb->COUNT=0;
if(p->READY!=p->TAIL) {
pcb->STATE='W';
INSERT(p);
FIRSTIN(p);
}
}
}
PRINT(p);
}
}
void INIT(LINK *p) {
p->RUN=NULL;
p->TAIL=p->READY=(PCB *)malloc(sizeof(PCB));
p->READY->NEXT=NULL;
p->FINISH=(PCB *)malloc(sizeof(PCB));
p->FINISH->NEXT=NULL;
}
int CREATE(LINK *p,int n) {
PCB *pcb,*q;
pcb=(PCB *)malloc(sizeof(PCB));
flushall();
printf("请输入第%d个进程的名称:\n",n);
gets(pcb->NAME);
printf("请输入第%d个进程的轮转时间片数:\n",n);
scanf("%d",&(pcb->ROUND));
printf("请输入第%d个进程的到达时间:\n",n);
scanf("%d",&(pcb->REACHTIME));
pcb->CPUTIME=0;
pcb->COUNT=0;
printf("请输入第%d个进程需运行的时间片数:\n",n);
scanf("%d",&(pcb->NEEDTIME));
pcb->STATE='W';
pcb->NEXT=NULL;
if(strcmp(pcb->NAME,"")==0||pcb->ROUND<=0||pcb->NEEDTIME<=0) /*输入错误*/
return 0;
q=p->READY;
while(q->NEXT!=NULL&&q->NEXT->REACHTIME<=pcb->REACHTIME)
q=q->NEXT;
pcb->NEXT=q->NEXT;
q->NEXT=pcb;
if(pcb->NEXT==NULL)
p->TAIL=pcb;
return 1;
}
void FIRSTIN(LINK *p) {
PCB *q;
q=p->READY->NEXT;
p->READY->NEXT=q->NEXT;
q->NEXT=NULL;
if(p->READY->NEXT==NULL)
p->TAIL=p->READY;
q->STATE='R';
p->RUN=q;
}
void INSERT(LINK *p) {
PCB *pcb;
pcb=(PCB *)malloc(sizeof(PCB));
strcpy(pcb->NAME,p->RUN->NAME);
pcb->ROUND=p->RUN->ROUND;
pcb->REACHTIME=p->RUN->REACHTIME;
pcb->CPUTIME=p->RUN->CPUTIME;
pcb->COUNT=p->RUN->COUNT;
pcb->NEEDTIME=p->RUN->NEEDTIME;
pcb->STATE=p->RUN->STATE;
pcb->NEXT=p->RUN->NEXT;
p->TAIL->NEXT=pcb;
p->TAIL=pcb;
p->RUN=NULL;
pcb->STATE='W';
}
void PRINT(LINK *p) {
PCB *pcb;
printf("执行一个时间片后的所有进程的状态:\n\n");
if(p->RUN!=NULL)
PR(p->RUN);
if(p->READY!=p->TAIL) {
pcb=p->READY->NEXT;
while(pcb!=NULL) {
PR(pcb);
pcb=pcb->NEXT;
}
}
pcb=p->FINISH->NEXT;
while(pcb!=NULL) {
PR(pcb);
pcb=pcb->NEXT;
}
}
void PR(PCB *p) {
printf("进程名:%s\n",p->NAME);
printf("进程轮转时间片:%d\n",p->ROUND);
printf("进程到达时间:%d\n",p->REACHTIME);
printf("进程占用CPU时间:%d\n",p->CPUTIME);
printf("计数器:%d\n",p->COUNT);
printf("进程完成还要的CPU时间:%d\n",p->NEEDTIME);
printf("进程的状态:%c\n\n",p->STATE);
}
E. 用C语言程序写的进程调度
你这时模拟的吧?
F. 求进程调度先来先服务算法,短进程优先算法完整c语言代码
/*(一)进程调度
进程调度算法有FIFO,优先数调度算法,时间片轮转调度算法,分级调度算法,
输入:进程流文件,其中存储的是一系列要执行的进程,
每个作业包括三个数据项:
进程名 所需时间 优先数(0级最高)
输出:
进程执行流 等待时间 平均等待时间
本程序包括:FIFO,优先数调度算法,时间片轮转调度算法
进程流文件process_stream.txt
测试数据:
p0 16 2
p1 5 1
p2 4 3
p3 8 0
p4 9 4
p5 7 6
VC++调试通过
*/
#include <stdio.h>
#include <string.h>
#include <iostream.h>
#include <stdlib.h>
const int Quatum=2;//定义时间片的长度为2秒
const int MAXPCB=100;//定义最大进程数
//定义进程结构体
typedef struct node
{
char name[20];//进程名
int time; //进程运行时间
int privilege;//进程优先级(静态)
int finished;//进程完成标志,0-未完成,1-已完成
int wait_time;//进程等待时间
}pcb;
pcb pcbs[MAXPCB];
int quantiry;//进程流文件中的进程总数
void initial()
{
int i;
for (i=0;i<MAXPCB;i++)
{
strcpy(pcbs[i].name,"");
pcbs[i].time=0;
pcbs[i].privilege=0;
pcbs[i].finished=0;
pcbs[i].wait_time=0;
}
quantiry=0;
}
int readData()
{
FILE *fp;
char fname[20];
int i;
cout<<"请输入进程流文件名:"<<endl;
cin>>fname;
if ((fp=fopen(fname,"r"))==NULL)
{
cout<<"错误,文件打不开,请检查文件名"<<endl;
}
else
{
while (!feof(fp))
{
fscanf(fp,"%s %d %d %d",pcbs[quantiry].name,
&pcbs[quantiry].time,&pcbs[quantiry].privilege);
quantiry++;
}
//输出所读入得数据
cout<<"输出所读入的数据"<<endl;
cout<<"进程流文件中的进程总数="<<quantiry<<endl;
cout<<"进程名 所需时间 优先数"<<endl;
for (i=0;i<quantiry;i++)
{
cout<<" "<<pcbs[i].name<<" "<<pcbs[i].time<<" "<<pcbs[i].privilege<<endl;
}
return 1;
}
return 0;
}
//重置数据,以供另一个算法使用
void init()
{
int i;
for (i=0;i<MAXPCB;i++)
{
pcbs[i].finished=0;
pcbs[i].wait_time=0;
}
}
void FIFO()
{
int i,j;
int total;
//输出FIFO算法执行流
cout<<endl<<"---------------------------------------------------------------"<<endl;
cout<<"FIFO算法执行流:"<<endl;
cout<<"进程名 等待时间"<<endl;
for (i=0;i<quantiry;i++)
{
cout<<" "<<pcbs[i].name<<" "<<pcbs[i].wait_time<<endl;
for (j=i+1;j<quantiry;j++)
{
pcbs[j].wait_time+=pcbs[i].time;
}
}
total=0;
for (i=0;i<quantiry;i++)
{
total+=pcbs[i].wait_time;
}
cout<<"总等待时间:"<<total<<" "<<"平均等待时间:"<<total/quantiry<<endl;
}
//优先度调度算法
void privilege()
{
int i,j,p;
int passed_time=0;
int total;
int queue[MAXPCB];
int current_privielege=1000;
for (i=0;i<quantiry;i++)
{
current_privielege=1000;
for (j=0;j<quantiry;j++)
{
if ((pcbs[j].finished==0)&&(pcbs[j].privilege<current_privielege))
{
p=j;
current_privielege=pcbs[j].privilege;
}
}
queue[i]=p;
pcbs[p].finished=1;
pcbs[p].wait_time+=passed_time;
passed_time+=pcbs[p].time;
}
//输出优先数调度执行流
cout<<endl<<"-----------------------------------------"<<endl;
cout<<"优先数调度执行流:"<<endl;
cout<<"进程名 等待时间"<<endl;
for (i=0;i<quantiry;i++)
{
cout<<" "<<pcbs[queue[i]].name<<" "<<pcbs[queue[i]].wait_time<<"--"<<queue[i]<<endl;
}
total=0;
for (i=0;i<quantiry;i++)
{
total+=pcbs[i].wait_time;
}
cout<<"总等待时间:"<<total<<" 平均等待时间:"<<total/quantiry<<endl;
}
//时间片轮转调度算法
void timer()
{
int i,j,sum,flag=1;
int passed_time=0;
int max_time=0;
int round=0;
int queue[1000];
int total=0;
while(flag==1)
{
flag=0;
for (i=0;i<quantiry;i++)
{
if (pcbs[i].finished==0)
{
flag=1;
queue[total]=i;
total++;
if (pcbs[i].time<=Quatum*(round+1))
pcbs[i].finished=1;
}
}
round++;
}
cout<<endl<<"---------------------------------------------------------------"<<endl;
cout<<"时间片轮转调度执行流:";
for(i=0;i<total;i++)
{
cout<<pcbs[queue[i]].name<<" ";
}
cout<<endl;
cout<<"进程名 结束时间 运行时间 等待时间"<<endl;
sum=0;
for (i=0;i<quantiry;i++)
{
for(j=total-1;j>=0;j--)//从轮转调度执行流序列由后往前比较,找到同名进程即可计算其完成时间
{
if (strcmp(pcbs[queue[j]].name,pcbs[i].name)==0)
{
cout<<" "<<pcbs[i].name<<" "<<(j+1)*Quatum<<" ";
cout<<pcbs[i].time<<" "<<(j+1)*Quatum-pcbs[i].time<<endl;
sum+=(j+1)*Quatum-pcbs[i].time;
break;
}
}
}
cout<<"总等待时间:"<<sum<<" "<<"平均等待时间:"<<sum/quantiry<<endl;
}
//显示版权信息函数
void version()
{
cout<<endl<<endl;
cout<<" ┏━━━━━━━━━━━━━━━━━━━━━━━┓"<<endl;
cout<<" ┃ 进程调度模拟系统 ┃"<<endl;
cout<<" ┠───────────────────────┨"<<endl;
cout<<" ┃ version 2011 ┃"<<endl;
cout<<" ┗━━━━━━━━━━━━━━━━━━━━━━━┛"<<endl;
cout<<endl<<endl;
}
//主函数
int main()
{
int flag;
version();
initial();
flag=readData();
if(flag==1){
FIFO();
init();
privilege();
init();
timer();
}
cout<<endl;
system("pause");
return 0;
}