公眾:類(lèi)PrivilegeProcess {
創(chuàng)新互聯(lián)主營(yíng)德宏州網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,重慶APP開(kāi)發(fā),德宏州h5微信小程序開(kāi)發(fā)搭建,德宏州網(wǎng)站營(yíng)銷(xiāo)推廣歡迎德宏州等地區(qū)企業(yè)咨詢(xún)
公共靜態(tài)無(wú)效的主要(字串[] args){
MyQueue的MyQueue的新MyQueue的();/ /聲明隊(duì)列
印刷電路板[PCB = {新的PCB(001 ,8,1),新的PCB(002,7,9),新的PCB(003,3,8),新的PCB(004,1,7),新的PCB(005,7,4)};
PCB段=新的PCB();
(INT I = 0; pcb.length; + +){/ /初始化先進(jìn)行排序,選擇排序這里使用的是高優(yōu)先級(jí)的一線隊(duì)
(J =我; pcb.length; J + +){
(PCB [I]。特權(quán)PCB [J]。特權(quán)){
段= PCB [1];
PCB [I] = PCB [J];
PCB [J] =段;
}
}
}
體系。通過(guò)out.println(“入隊(duì)后第一時(shí)間的進(jìn)程的順序:”);
(INT I = 0; pcb.length; + +){
的System.out調(diào)用println(第一次入隊(duì)#程序名稱(chēng):“+ PCB [我]。名稱(chēng)+ totaltime:”+ PCB [I]。totaltime +“的”特權(quán)“+ PCB [我]。特權(quán)); }
();
myqueue.start(PCB);
}
}
類(lèi)MyQueue的{
INT指數(shù)= 0;
PCB [] PC =新的PCB [5];
PCB [] PC1 =新的PCB [4];
PCB溫度=新的PCB() BR /公共無(wú)效排隊(duì)(PCB工藝){/ /排隊(duì)算法
(指數(shù)== 5){
(“出界!”);
返回
}
PC [索引] =進(jìn)程;
指數(shù)+ +;
}
公共:PCB DEQUEUE(){/ /出隊(duì)算法(索引== 0)
返回空;
(INT I = 0; pc1.length; + +){
PC1 [I] = PC [ +1];
}
指數(shù) -
溫度= PC [0];
(INT I = 0; pc1.length; + +){ BR / PC [I] = PC1 [I];
}
回報(bào)條件;
}
公共無(wú)效啟動(dòng)(PCB [] PC){/ /進(jìn)程表算法
(PC [0]。isNotFinish ==真| | PC [1 isNotFinish ==真| | PC [2 isNotFinish ==真| | PC [3]。時(shí)isNotFinish ==真| | PC [4]。isNotFinish ==){
/ / *注:| |運(yùn)算符都是假的,所有的表達(dá)式結(jié)果為假,否則真
(INT I = 0; PC長(zhǎng)度; + +){
PC [I]。運(yùn)行(這一點(diǎn)); /} 的System.out.println();
(INT I = 0; pc.length; + +){/ /處理每個(gè)運(yùn)行一次運(yùn)行的時(shí)間片的長(zhǎng)度重新排序優(yōu)先一旦
(J =我; pc.length; J + +){
如果(PC [I]特權(quán)PC [J]。特權(quán)){
溫度= PC [I];
PC [I] = PC [J];
PC [J] =溫度;
}
}
}
}
}
}
類(lèi)PCB {/ /聲明過(guò)程級(jí)
和int名,totaltime ,運(yùn)行時(shí)特權(quán);
布爾isNotFinish的;
公眾PCB(){
}
公開(kāi)PCB(名稱(chēng),詮釋totaltime特權(quán)){
this.name =的名稱(chēng);/ /進(jìn)程名
this.totaltime = totaltime ;/ /
this.privilege =特權(quán);/ /總時(shí)間優(yōu)先 this.runtime = 2 ;/ /時(shí)間片值是2
this.isNotFinish =真;/ /是否執(zhí)行完成
(“初始值:程序名稱(chēng):”+名+“totaltime:”+ totaltime +“特權(quán)”+特權(quán));
System.out的。調(diào)用println();
}
MyQueue的MQ公共無(wú)效的run(){/ /處理的基礎(chǔ)上實(shí)施的時(shí)間片算法
(totalTime 1){ totaltime =運(yùn)行;/ /總時(shí)間大于1,總時(shí)間=總時(shí)間 - 時(shí)間片
特權(quán) -
(“程序名稱(chēng):”+姓名+“ remaintime:“+ +”特權(quán)“+特權(quán)); totaltime
的} else if(totaltime == 1){
totaltime - ;/ /總時(shí)間為1時(shí),執(zhí)行時(shí)間為1
特權(quán) -
(“程序名稱(chēng):”+姓名+“remaintime:”+ totaltime +“特權(quán)”+特權(quán));
}其他{
isNotFinish =假;/ / 0,將isNotFinish標(biāo)志設(shè)置為假
}
如果(isNotFinish ==真){br mq.deQueue(); mq.enQueue(本); }
}
}
沒(méi)java的 發(fā)段源代碼給你 有興趣自己慢慢理解
#include time.h
#include dos.h
#include math.h
#include conio.h
#include stdio.h
#include stdlib.h
#include time.h
#include graphics.h
#define ESC 0x1b
#define ENTER 0x0d
#define TRUE 1
#define FALSE 0
/*每隔TIME秒就變換一次優(yōu)先級(jí)*/
#define TIME 5
/*數(shù)據(jù)結(jié)構(gòu)*/
/****************************************************************/
enum _Status/*進(jìn)程狀態(tài)枚舉*/
{
READY =0,/*就緒*/
RUN,/*執(zhí)行中*/
SUSPEND,/*掛起*/
};
typedef enum _Status Status;
/****************************************************************/
struct _Pcb/*進(jìn)程結(jié)構(gòu)*/
{
int PID;/*進(jìn)程ID,ID為負(fù)數(shù)的進(jìn)程為系統(tǒng)后備隊(duì)列的作業(yè)*/
int Time;/*進(jìn)程運(yùn)行需要的時(shí)間*/
int Prior;/*進(jìn)程的優(yōu)先級(jí),越大越優(yōu)先*/
Status Sts;/*狀態(tài)*/
struct _Pcb *Next;/*指向本進(jìn)程隊(duì)列中下個(gè)進(jìn)程的PCB*/
};
typedef struct _Pcb PCB;
/****************************************************************/
struct _Batch/*多道處理中的道結(jié)構(gòu)*/
{
PCB *pcb;/*該道當(dāng)前正在處理的進(jìn)程*/
struct _Batch *Next;/*下一道*/
};
typedef struct _Batch Batch;
/****************************************************************/
/*多道系統(tǒng)相關(guān)全局變量*/
PCB *ProcQueue = NULL;/*進(jìn)程鏈表,按優(yōu)先級(jí)從大到小排列*/
Batch *BatchQueue = NULL;/*系統(tǒng)多道鏈表*/
/****************************************************************/
/*動(dòng)態(tài)優(yōu)先權(quán)搶占式調(diào)度算法及相關(guān)函數(shù)聲明*/
/****************************************************************/
int InitBatchs(int n);/*初始化多道系統(tǒng),n為道數(shù)*/
int InsertProc(int prior, int time);/*向進(jìn)程鏈表中按優(yōu)先級(jí)大小插入一個(gè)新進(jìn)程*/
int InsertIDLE();/*向進(jìn)程隊(duì)列中加入后備進(jìn)程,當(dāng)系統(tǒng)空閑時(shí)將被調(diào)入*/
int SortProcQueue();/*將進(jìn)程鏈表按優(yōu)先級(jí)從大到小排列*/
int AddBatch();/*增加系統(tǒng)道數(shù)*/
int DeleteBatch();/*減少系統(tǒng)道數(shù)*/
int UnSuspendProc(int id);/*解除ID為id的進(jìn)程的掛起狀態(tài)*/
int UpdateBatchs();/*多道系統(tǒng)根據(jù)進(jìn)程鏈表進(jìn)行更新,并將執(zhí)行完畢的進(jìn)程刪除*/
int PassSeconds(int n);/*系統(tǒng)經(jīng)過(guò)n秒后計(jì)算數(shù)據(jù)并進(jìn)行優(yōu)先級(jí)調(diào)度*/
/****************************************************************/
/*各函數(shù)的定義*/
/****************************************************************/
int InitBatchs(int n)
{
int i;
for (i=0; in; ++i)
{
AddBatch();
}
return (UpdateBatchs());
}
int InsertProc(int prior, int time)
{
static int sysid = 0;/*該系統(tǒng)已經(jīng)加入過(guò)多少進(jìn)程,此值將是新進(jìn)程的ID*/
PCB *last,*now,*pcb;
pcb = (PCB*)malloc(sizeof(PCB));
if (pcb == NULL) return FALSE;
pcb-Prior = prior;
pcb-Time = time;
pcb-PID = (++sysid);
pcb-Sts = READY;
if (ProcQueue == NULL)/*如果進(jìn)程隊(duì)列為空*/
{
ProcQueue = pcb;
pcb-Next = NULL;
return TRUE;
}
last = ProcQueue;
now = last-Next;
if (pcb-Prior last-Prior)/*pcb將排在隊(duì)頭*/
{
pcb-Next = ProcQueue;
ProcQueue = pcb;
return TRUE;
}
while ((now != NULL) (pcb-Prior now-Prior))/*尋找插入位置*/
{
last = now;
now = last-Next;
}
last-Next = pcb;
pcb-Next = now;
return TRUE;
}
int InsertIDLE()
{
PCB *now = ProcQueue;
PCB *idle = (PCB*)malloc(sizeof(PCB));
if (idle == NULL) return FALSE;
idle-PID = -1;
idle-Prior = -1;
idle-Sts = SUSPEND;
idle-Time = -1;
idle-Next = NULL;
if (ProcQueue == NULL)
{
ProcQueue = idle;
return TRUE;
}
while(now-Next != NULL)
{
now = now-Next;
}
now-Next = idle;
return TRUE;
}
int SortProcQueue()
{ /*冒泡排序*/
PCB *last, *now;
int b = FALSE;/*上次遍歷是否無(wú)交換產(chǎn)生*/
if (ProcQueue==NULL || ProcQueue-Next==NULL)/*如果鏈表中無(wú)進(jìn)程或只有一個(gè)進(jìn)程*/
return FALSE;
while (!b)
{
b = TRUE;
last=ProcQueue;
now=last-Next;
if (last-Prior now-Prior)
{
ProcQueue = now;
last-Next = now-Next;
now-Next = last;
b = FALSE;
last = ProcQueue;
now = last-Next;
}
while (now-Next!=NULL)
{
if ((now-Prior)(now-Next-Prior))
{
last-Next = now-Next;
now-Next = now-Next-Next;
last-Next-Next = now;
b = FALSE;
}
else
last = last-Next;
now = last-Next;
}
}
return TRUE;
}
int AddBatch()
{
Batch *bt = (Batch*)malloc(sizeof(Batch));
if (bt==NULL) return FALSE;
bt-Next = BatchQueue;
BatchQueue = bt;
bt-pcb = NULL;
return (InsertIDLE());
}
int DeleteBatch()
{
Batch *bt = BatchQueue;
PCB *last, *now;
if (BatchQueue==NULL || BatchQueue-Next==NULL)/*如果只剩最后一道則不刪除*/
return FALSE;
if (ProcQueue==NULL || ProcQueue-Next==NULL)/*如果只有最后一個(gè)后備空閑進(jìn)程*/
return FALSE;/**/
last = ProcQueue;
now = last-Next;
while (now-Next != NULL)/*查找到最后一個(gè)進(jìn)程,該進(jìn)程必定是后備空閑進(jìn)程*/
{
last = now;
now = last-Next;
}
if (now==NULL || now-PID=0)/*未查找到后備進(jìn)程*/
return FALSE;/**/
free(now);
last-Next = NULL;
BatchQueue = BatchQueue-Next;
free(bt);
return TRUE;
}
int UnSuspendProc(int id)
{
PCB *now = ProcQueue;
if (ProcQueue==NULL) return FALSE;
while (now != NULL)
{
if (now-PID == id)
{
now-Sts = READY;
return TRUE;
}
}
return FALSE;
}
int UpdateBatchs()
{
Batch *bt = BatchQueue;
PCB *last = ProcQueue, *now;
while (bt != NULL)
{
bt-pcb = NULL;
bt = bt-Next;
}
if (ProcQueue == NULL) return TRUE;
while (last-Sts==RUN last-PID=0 last-Time=0)
{
ProcQueue = ProcQueue-Next;
free(last);
last = ProcQueue;
}
now = last-Next;
while (now != NULL)
{
if (now-Sts==RUN now-PID=0 now-Time=0)/*如果該進(jìn)程是運(yùn)行中的一般進(jìn)程并已執(zhí)行完畢*/
{
last-Next = now-Next;
free(now);
}
else
last = last-Next;
now = last-Next;
}
bt = BatchQueue;
now = ProcQueue;
while (bt != NULL now != NULL)
{
bt-pcb = now;
now-Sts = RUN;
bt = bt-Next;
now = now-Next;
}
while (now != NULL)
{
if (now-Sts == RUN)
{
now-Sts = SUSPEND;
}
now = now-Next;
}
return TRUE;
}
int PassSeconds(int n)
{
static int time = 0;
int i=0, ProcEnd = FALSE;
PCB *pcb = ProcQueue;
Batch *bt = BatchQueue;
if (bt == NULL) return FALSE;
time += n;
if (time=TIME)
{
i = time/TIME;/*經(jīng)過(guò)多少時(shí)間段*/
time = time%TIME;
}
while (bt != NULL)/*更新進(jìn)程運(yùn)行時(shí)間*/
{
if (bt-pcb-PID=0)
{
bt-pcb-Time -= n;
if (bt-pcb-Time = 0)/*進(jìn)程結(jié)束*/
{
ProcEnd = TRUE;
}
}
bt = bt-Next;
}
if (i 0)
{
while (pcb != NULL)/*更新進(jìn)程優(yōu)先權(quán)(動(dòng)態(tài)優(yōu)先權(quán))*/
{
if (pcb-Sts == RUN pcb-PID=0)/*運(yùn)行的進(jìn)程優(yōu)先權(quán)降低*/
{
pcb-Prior -= i;
if (pcb-Prior 0)
pcb-Prior = 0;
}
else if (pcb-Sts == SUSPEND pcb-PID=0)/*掛起的進(jìn)程優(yōu)先權(quán)升高*/
pcb-Prior += i;
pcb = pcb-Next;
}
}
if (i0)
SortProcQueue();/*如果優(yōu)先級(jí)有變動(dòng)則重新排序*/
if (ProcEnd || i0)
{
UpdateBatchs();/*更新多道進(jìn)程*/
}
return TRUE;
}
/****************************************************************/
/*圖形界面相關(guān)函數(shù)*/
/****************************************************************/
/*表格的單位寬度和高度*/
#define WIDTH 64
#define HEIGHT 12
void *black=NULL;/*背景色方格,使用它擦出表格中的圖形*/
int InitGraph()/*初始化圖形界面*/
{
int GraphDriver; /* The Graphics device driver */
int GraphMode; /* The Graphics mode value */
int ErrorCode;
GraphDriver = DETECT; /* Request auto-detection */
initgraph( GraphDriver, GraphMode, "" );
ErrorCode = graphresult(); /* Read result of initialization*/
if( ErrorCode != grOk )
{ /* Error occured during init */
printf(" Graphics System Error: %s\n", grapherrormsg( ErrorCode ) );
getch();
return FALSE;
}
cleardevice();
black = (void*)malloc(imagesize(1,1,WIDTH-1,HEIGHT-1));
getimage(1,1,WIDTH-1,HEIGHT-1,black);
DrawFrame();
DrawData();
return TRUE;
}
int DrawFrame()/*畫(huà)邊框和表頭*/
{
settextjustify(CENTER_TEXT, CENTER_TEXT);
gprintf(320, HEIGHT/2-1, "Multi-Batch System Emulation");
settextjustify(LEFT_TEXT, TOP_TEXT);
moveto(0,HEIGHT);
lineto(0,479);
lineto(639,479);
lineto(639,HEIGHT);
lineto(0,HEIGHT);
line(WIDTH*4,HEIGHT,WIDTH*4,479);
line(WIDTH*7,HEIGHT,WIDTH*7,479);
line(0,HEIGHT*2,639,HEIGHT*2);
line(0,HEIGHT*3,639,HEIGHT*3);
gprintf(HEIGHT*0+2,HEIGHT*1+2,"System Batchs");/*系統(tǒng)多道列表頭*/
gprintf(HEIGHT*0+2,HEIGHT*2+2,"Batch");
gprintf(WIDTH*1+2,HEIGHT*2+2,"ProcID");
gprintf(WIDTH*2+2,HEIGHT*2+2,"Time");
gprintf(WIDTH*3+2,HEIGHT*2+2,"Prior");
gprintf(WIDTH*4+2,HEIGHT*1+2,"Suspended Processes");/*掛起隊(duì)列列表頭*/
gprintf(WIDTH*4+2,HEIGHT*2+2,"ProcID");
gprintf(WIDTH*5+2,HEIGHT*2+2,"Time");
gprintf(WIDTH*6+2,HEIGHT*2+2,"Prior");
gprintf(WIDTH*7+2,HEIGHT*1+2,"Ready Processes");/*就緒隊(duì)列列表頭*/
gprintf(WIDTH*7+2,HEIGHT*2+2,"ProcID");
gprintf(WIDTH*8+2,HEIGHT*2+2,"Time");
gprintf(WIDTH*9+2,HEIGHT*2+2,"Prior");
}
int DrawData()/*繪制系統(tǒng)數(shù)據(jù)*/
{
int numRun=0, numSus=0, numRed=0;/*運(yùn)行掛起和就緒的進(jìn)程各有多少*/
PCB* now = ProcQueue;
int x=0, y=0;
while (now != NULL)
{
switch(now-Sts)
{
case RUN:
x = WIDTH*1;
y = HEIGHT*(3+(numRun++));
break;
case SUSPEND:
x = WIDTH*4;
y = HEIGHT*(3+(numSus++));
break;
case READY:
x = WIDTH*7;
y = HEIGHT*(3+(numRed++));
break;
}
if (now-Sts==RUN)/*該進(jìn)程為正在運(yùn)行的進(jìn)程*/
{
putimage(x-WIDTH+1,y+1,black,COPY_PUT);
gprintf(x-WIDTH+2,y+2,"%d",numRun);
}
if (now-PID=0)/*該進(jìn)程不是后備進(jìn)程*/
{
putimage(x+1,y+1,black,COPY_PUT);
gprintf(x+2,y+2,"%d",now-PID);
putimage(x+1+WIDTH,y+1,black,COPY_PUT);
gprintf(x+WIDTH+2,y+2,"%d",now-Time);
putimage(x+1+WIDTH*2,y+1,black,COPY_PUT);
gprintf(x+WIDTH*2+2,y+2,"%d",now-Prior);
}
else
{
putimage(x+1,y+1,black,COPY_PUT);
putimage(x+1+WIDTH,y+1,black,COPY_PUT);
putimage(x+1+WIDTH*2,y+1,black,COPY_PUT);
gprintf(x+2,y+2,"system idle process");
}
now = now-Next;
}
}
int DlgGetNum(char *buf,int l,int t,int r,int b,int gettime)
{
char ch;
int pos=0;
bar(l,t,r,b);
gprintf(l+10,t+5,"Add new Process");
if (gettime)
gprintf(l+10,t+20,"input the time:");
else
gprintf(l+10,t+20,"input the priority:");
while (1)
{
ch = getch();
if (ch == ENTER)/*如果輸入了回車(chē)鍵*/
{
if(pos!=0)/*如果位置不在第一位(回車(chē)鍵不準(zhǔn)第一個(gè)輸入)*/
{
buf[pos]='\0';
break;
}
}
else if (ch='0' ch='9')
{
buf[pos++]=ch;
buf[pos]='\0';
}
else if (ch == ESC)
{
return FALSE;
}
else/*其他按鍵均按BackSpace處理*/
{
--pos;
buf[pos]='\0';
}
if (pos0)
{ pos=0; buf[pos]='\0';}
else if (pos4)/*最多輸入4位數(shù)*/
{ pos=4; buf[pos]='\0';}
bar(l,t+35,r,t+47);
gprintf(l+50,t+35,buf);
}
return TRUE;
}
int NewProcDlg()
{
int l=200,t=150,r=380,b=200,pos=0,prior=0,time=0;
char buf[5],ch;
PCB *pcb;
void* bg = (void*)malloc(imagesize(l,t,r,b));
getimage(l,t,r,b,bg);
setfillstyle(1,2);
flushall();
/*******輸入優(yōu)先級(jí)**********/
if (!DlgGetNum(buf,l,t,r,b,FALSE))
goto exit;
prior = atoi(buf);
/*******輸入時(shí)間**********/
pos=0;
buf[pos]='\0';
if (!DlgGetNum(buf,l,t,r,b,TRUE))
goto exit;
time = atoi(buf);
InsertProc(prior, time);
exit:
putimage(l,t,bg,COPY_PUT);
free(bg);
return TRUE;
}
int gprintf( int xloc, int yloc, char *fmt, ... )/*圖形系統(tǒng)中的格式輸出*/
{
va_list argptr; /* Argument list pointer */
char str[140]; /* Buffer to build sting into */
int cnt; /* Result of SPRINTF for return */
va_start( argptr, format ); /* Initialize va_ functions */
cnt = vsprintf( str, fmt, argptr ); /* prints string to buffer */
outtextxy( xloc, yloc, str ); /* Send string in graphics mode */
va_end( argptr ); /* Close va_ functions */
return( cnt ); /* Return the conversion count */
}
/****************************************************************/
/*main函數(shù)*/
int main()
{
clock_t start=0, end=0;
char kb;
InitBatchs(3);/*初始化為3道系統(tǒng)*/
InitGraph();
while (1)
{
start = end = clock();
while (!kbhit())
{
start = clock();
if ((start-end)/18.2 = 1)/*時(shí)間過(guò)了一秒*/
{
start = end = clock();
PassSeconds(1);
cleardevice();
DrawFrame();
DrawData();
}
}
kb = getch();
switch (kb)
{
case ESC:
closegraph();
return 0;
case 'w':
AddBatch();
UpdateBatchs();
cleardevice();
DrawFrame();
DrawData();
break;
case 's':
DeleteBatch();
UpdateBatchs();
cleardevice();
DrawFrame();
DrawData();
break;
case 'i':
NewProcDlg();
UpdateBatchs();
DrawFrame();
DrawData();
break;
}
}
return 0;
}
#includewindows.h
#includeiostream
#includestdio.h
#includestring
using namespace std;
const int maxnum=100;
int N; /*進(jìn)程數(shù)*/
double start[maxnum],endtime[maxnum],arrive[maxnum],runtime[maxnum],zhou[maxnum];
double averagezhou; // 平均周轉(zhuǎn)時(shí)間
double average_zhou; //平均帶權(quán)周轉(zhuǎn)時(shí)間
char name; //進(jìn)程名
double dqzhou[maxnum]; //帶權(quán)周轉(zhuǎn)時(shí)間
typedef struct node
{
char name[10]; //進(jìn)程名
int round; //進(jìn)程時(shí)間輪轉(zhuǎn)時(shí)間片
int cputime; //進(jìn)程占用CPU時(shí)間
int needtime; //進(jìn)程到完成還要的時(shí)間
char state; //進(jìn)程的狀態(tài)
struct node *next; //鏈指針
}PCB;
PCB *finish,*ready,*tail,*run; /*隊(duì)列指針*/
void firstin() /*將就緒隊(duì)列中的第一個(gè)進(jìn)程投入運(yùn)行*/
{
run=ready; /*就緒隊(duì)列頭指針賦值給運(yùn)行頭指針*/
run-state='R'; /*進(jìn)程狀態(tài)變?yōu)檫\(yùn)行態(tài)*/
ready=ready-next; /*就緒對(duì)列頭指針后移到下一進(jìn)程*/
}
void print1(PCB *q) /*進(jìn)程PCB輸出*/
{
printf("進(jìn)程名 已運(yùn)行時(shí)間 還需要時(shí)間 時(shí)間片 狀態(tài)\n");
printf(" %-10s%-15d%-10d%-10d %-c\n",q-name,q-cputime,q-needtime,q-round,q-state);
}
void print() /*輸出函數(shù)*/
{
PCB *p;
if(run!=NULL) /*如果運(yùn)行指針不空*/
print1(run); /*輸出當(dāng)前正在運(yùn)行的PCB*/
p=ready; /*輸出就緒隊(duì)列PCB*/
while(p!=NULL)
{
print1(p);
p=p-next;
}
p=finish; /*輸出完成隊(duì)列的PCB*/
while(p!=NULL)
{
print1(p);
p=p-next;
}
}
void insert(PCB *p2) //輪轉(zhuǎn)法插入函數(shù)
{
tail-next=p2; //將新的PCB插入在當(dāng)前就緒隊(duì)列的尾
tail=p2;
p2-next=NULL;
}
void create() /*創(chuàng)建進(jìn)程PCB*/
{
PCB *p;
int i,time;
char na[10];
ready=NULL;
finish=NULL;
run=NULL;
printf("請(qǐng)輸入進(jìn)程名稱(chēng)和所需要CPU的時(shí)間:\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;
if(i==1)
p-state='R';
else
p-state='W';
p-round=1; /*時(shí)間片*/
if(ready!=NULL)
insert(p);
else
{
p-next=ready;
ready=p;
tail=p;
}
}
printf("------------------------------------------------------------------\n");
print(); /*輸出進(jìn)程PCB信息*/
run=ready; /*將就緒隊(duì)列的第一個(gè)進(jìn)程投入運(yùn)行*/
ready=ready-next;
run-state='R';
}
void RR() //時(shí)間片輪轉(zhuǎn)調(diào)度
{
while(run!=NULL)
{
run-cputime=run-cputime+1;
run-needtime=run-needtime-1;
if(run-needtime==0) /*運(yùn)行完將其變?yōu)橥瓿蓱B(tài),插入完成隊(duì)列*/
{
run-next=finish;
finish=run;
run-state='F';
run=NULL;
if(ready!=NULL)
firstin(); /*就緒對(duì)列不空,將第一個(gè)進(jìn)程投入運(yùn)行*/
}
else
if(ready!=NULL) /*如就緒隊(duì)列不空*/
{
run-state='W'; /*將進(jìn)程插入到就緒隊(duì)列中等待輪轉(zhuǎn)*/
insert(run);
firstin(); /*將就緒對(duì)列的第一個(gè)進(jìn)程投入運(yùn)行*/
}
printf("------------------------------------------------------------------\n");
print(); /*輸出進(jìn)程信息*/
}
}
void FCFS(double *arrive,double *runtime,double n) //先來(lái)先服務(wù)調(diào)度算法
{
start[0]=arrive[0]; //開(kāi)始執(zhí)行時(shí)間=到達(dá)時(shí)間
endtime[0]=start[0]+runtime[0]; //完成時(shí)間=開(kāi)始時(shí)間+服務(wù)時(shí)間
zhou[0]=endtime[0]-arrive[0]; //周轉(zhuǎn)時(shí)間=完成時(shí)間-到達(dá)時(shí)間
dqzhou[0]=zhou[0]/runtime[0];
for(int i=0;in;i++)
{
if(endtime[i-1]arrive[i]||endtime[i-1]==arrive[i])
endtime[i]=endtime[i-1]+runtime[i];
else
endtime[i]=arrive[i]+runtime[i];
zhou[i]=endtime[i]-arrive[i];
dqzhou[i]=zhou[i]/runtime[i];
averagezhou+=zhou[i];
average_zhou+=dqzhou[i];
}
averagezhou=averagezhou/n;
average_zhou=average_zhou/n;
cout"完成時(shí)間為:"endl;
for(int j=0;jn;j++)
coutendtime[j]" "endl;
cout"周轉(zhuǎn)時(shí)間為:"endl;
for(int k=0;kn;k++)
coutzhou[k]" "endl;
cout"帶權(quán)周轉(zhuǎn)時(shí)間為:"endl;
for(int m=0;mn;m++)
coutdqzhou[m]" "endl;
cout"平均周轉(zhuǎn)時(shí)間為:"endl;
coutaveragezhou" "endl;
cout"平均帶權(quán)周轉(zhuǎn)時(shí)間為:"endl;
coutaverage_zhou" "endl;
}
void SJF(double *arrive,double *runtime,double n) //短作業(yè)優(yōu)先調(diào)度算法
{
int end[maxnum]; //用于標(biāo)記進(jìn)程是否已經(jīng)執(zhí)行,應(yīng)經(jīng)執(zhí)行end[i]=1,否則為0;
for(int k=0;kn;k++)
end[k]=0;
int temp,now=0,next=1,p=1; //now表示剛執(zhí)行完的進(jìn)程號(hào),next表示下一個(gè)要執(zhí)行的進(jìn)程號(hào)
start[0]=arrive[0]; //開(kāi)始執(zhí)行時(shí)間=到達(dá)時(shí)間
endtime[0]=start[0]+runtime[0]; //完成時(shí)間=開(kāi)始時(shí)間+服務(wù)時(shí)間
zhou[0]=endtime[0]-arrive[0]; //周轉(zhuǎn)時(shí)間=完成時(shí)間-到達(dá)時(shí)間
dqzhou[0]=zhou[0]/runtime[0]; //帶權(quán)周轉(zhuǎn)時(shí)間=周轉(zhuǎn)時(shí)間/服務(wù)時(shí)間
averagezhou=zhou[0];
average_zhou=dqzhou[0];
end[now]=1; //執(zhí)行完的進(jìn)程設(shè)置為1;
for(int i=1;in;i++)
{
int j;
for(j=1;jn;)
{
if(arrive[j]endtime[now]||arrive[j]==endtime[now])
j++;
else
break;
}
temp=j;
int min=p;
for(j=1;j=temp;j++)
{
if(runtime[min]runtime[j] end[j]==0)
min=j;
}
next=min;
endtime[next]=endtime[now]+runtime[next];
zhou[next]=endtime[next]-arrive[next];
dqzhou[next]=zhou[next]/runtime[next];
averagezhou+=zhou[next];
average_zhou+=dqzhou[next];
end[next]=1;
now=next;
next=p;
while(end[p]!=0)
p++;
}
averagezhou=averagezhou/n;
average_zhou=average_zhou/n;
cout"完成時(shí)間為:"endl;
for(int j=0;jn;j++)
coutendtime[j]" "endl;
cout"周轉(zhuǎn)時(shí)間為:"endl;
for(k=0;kn;k++)
coutzhou[k]" "endl;
cout"帶權(quán)周轉(zhuǎn)時(shí)間為:"endl;
for(int m=0;mn;m++)
coutdqzhou[m]" "endl;
cout"平均周轉(zhuǎn)時(shí)間為:"endl;
coutaveragezhou" "endl;
cout"平均帶權(quán)周轉(zhuǎn)時(shí)間為:"endl;
coutaverage_zhou" "endl;
}
int EDF() //最早截止時(shí)間的調(diào)度算法
{
int arrive_A,arrive_B; //標(biāo)記進(jìn)程A,進(jìn)程B的到達(dá)時(shí)間
int zhouqi_A,zhouqi_B,serve_A,serve_B; //進(jìn)程的周期時(shí)間和服務(wù)時(shí)間
int i,j,a=0,b=0,ka=0,kb=0; //ka,kb為開(kāi)關(guān),i,j,a,b為進(jìn)程下標(biāo)
int num_a=0,num_b=0; //服務(wù)累計(jì)時(shí)間
printf("輸入進(jìn)程A的周期時(shí)間,服務(wù)時(shí)間: ");
scanf("%d%d",zhouqi_A,serve_A);
printf("輸入進(jìn)程B的周期時(shí)間,服務(wù)時(shí)間: ");
scanf("%d%d",zhouqi_B,serve_B);
for(int T=0;T=100;T++)
{
if(num_a==serve_A) //進(jìn)程A完成
{
num_a=serve_A+1;
printf("當(dāng)T=%d時(shí)",T);
printf("進(jìn)程A%d結(jié)束\n",a);
if(num_bserve_B)
{
printf(" 調(diào)用進(jìn)程B%d\n",b);
kb=1;
}
ka=0;
}
if(num_b==serve_B)
{
num_b=serve_B+1;
printf("當(dāng)T=%d時(shí)",T);
printf("進(jìn)程B%d結(jié)束\n",b);
if(num_aserve_A)
{
printf(" 調(diào)用進(jìn)程A%d\n",a);
ka=1;
}
kb=0;
}
if(T%zhouqi_A==0 T%zhouqi_B==0)
{
arrive_A=arrive_B=T;
j=++a;
i=++b;
printf("當(dāng)T=%d時(shí),進(jìn)程A%d和進(jìn)程B%d同時(shí)產(chǎn)生,此時(shí),",T,j,i);
if(zhouqi_A=zhouqi_B)
{
printf("調(diào)用進(jìn)程A%d,阻塞進(jìn)程B%d\n",j,i);
ka=1;
kb=0;
}
else
{
printf("調(diào)用進(jìn)程B%d,阻塞進(jìn)程A%d\n",i,j);
ka=0;
kb=1;
}
num_a=num_b=0;
}
if(T%zhouqi_A==0T%zhouqi_B!=0)
{
arrive_A=T;
printf("當(dāng)T=%d時(shí)",T);
printf("進(jìn)程A%d產(chǎn)生 ",++a); //不可能與進(jìn)程A競(jìng)爭(zhēng)處理器
num_a=0;
if(num_bserve_B) //進(jìn)程B沒(méi)有完成
if(arrive_B+zhouqi_Barrive_A+zhouqi_A) //若進(jìn)程B最早截止時(shí)間大于進(jìn)程A的,則執(zhí)行進(jìn)程A
{
printf("進(jìn)程A%d執(zhí)行。\n",a);
ka=1;
kb=0;
}
else //若進(jìn)程B最早截止時(shí)間小于等于進(jìn)程A的
printf("進(jìn)程B%d繼續(xù)執(zhí)行。\n",b);
else //進(jìn)程B完成
{
printf("進(jìn)程A%d執(zhí)行。\n",a);
ka=1;
}
}
if(T%zhouqi_A!=0 T%zhouqi_B==0)
{
arrive_B=T;
printf("當(dāng)T=%d時(shí)",T);
printf("進(jìn)程B%d產(chǎn)生,",++b); //不可能與進(jìn)程B競(jìng)爭(zhēng)處理器
num_b=0;
if(num_aserve_A) //進(jìn)程A沒(méi)有完成
if(arrive_B+zhouqi_B=arrive_A+zhouqi_A) //進(jìn)程A的最早截止時(shí)間不小于B
printf("進(jìn)程A%d繼續(xù)執(zhí)行。\n",a);
else
{
printf("進(jìn)程B%d執(zhí)行。\n",b);
kb=1;
ka=0;
}
else //進(jìn)程A完成
{
printf("進(jìn)程B%d執(zhí)行。\n",b);
kb=1;
}
}
if(ka)
num_a++;
if(kb)
num_b++;
}
return 1;
}
int main()
{
system("color 0b"); //設(shè)置顏色
cout"最早截止時(shí)間的調(diào)度算法如下: "endlendl;
EDF();
int n;
coutendl;
cout"請(qǐng)輸入進(jìn)程的數(shù)目: ";
cinn;
cout"請(qǐng)按進(jìn)程到達(dá)時(shí)間從小到大依次輸入n個(gè)進(jìn)程的到達(dá)時(shí)間: "endl;
for(int i=0;in;i++)
cinarrive[i];
cout"請(qǐng)按上面進(jìn)程的順序依次輸入n個(gè)進(jìn)程的服務(wù)時(shí)間: "endl;
for(int j=0;jn;j++)
cinruntime[j];
cout"先來(lái)先服務(wù)調(diào)度算法如下: "endl;
FCFS(arrive,runtime,n);
coutendlendl;
cout"短作業(yè)優(yōu)先調(diào)度算法如下: "endl;
SJF(arrive,runtime,n);
coutendlendl;
printf("輪轉(zhuǎn)調(diào)度算法如下:\n\n");
printf("輸入創(chuàng)建進(jìn)程的數(shù)目:\n");
scanf("%d",N);
create();
RR();
return 0;
}
USE msdb ;
GO
EXEC dbo.sp_update_job
@job_id='265CAD29-22E0-4AFC-A48C-77C27F2AABA7',
--@job_id作業(yè)ID
@description = N'Nightly backups disabled during server migration.',
--描述
@enabled = 1 ;
--狀態(tài),1為啟用,0為不啟用
GO