當前位置:首頁 » 編程語言 » 進程調度c語言程序
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

進程調度c語言程序

發布時間: 2023-02-16 16:09:28

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;
}