新聞中心
編寫代碼實現(xiàn)作業(yè)的三種調(diào)度算法
#includewindows.h
為荷塘等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計制作服務(wù),及荷塘網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為成都網(wǎng)站設(shè)計、網(wǎng)站制作、荷塘網(wǎng)站設(shè)計,以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!
#includeiostream
#includestdio.h
#includestring
using namespace std;
const int maxnum=100;
int N; /*進程數(shù)*/
double start[maxnum],endtime[maxnum],arrive[maxnum],runtime[maxnum],zhou[maxnum];
double averagezhou; // 平均周轉(zhuǎn)時間
double average_zhou; //平均帶權(quán)周轉(zhuǎn)時間
char name; //進程名
double dqzhou[maxnum]; //帶權(quán)周轉(zhuǎn)時間
typedef struct node
{
char name[10]; //進程名
int round; //進程時間輪轉(zhuǎn)時間片
int cputime; //進程占用CPU時間
int needtime; //進程到完成還要的時間
char state; //進程的狀態(tài)
struct node *next; //鏈指針
}PCB;
PCB *finish,*ready,*tail,*run; /*隊列指針*/
void firstin() /*將就緒隊列中的第一個進程投入運行*/
{
run=ready; /*就緒隊列頭指針賦值給運行頭指針*/
run-state='R'; /*進程狀態(tài)變?yōu)檫\行態(tài)*/
ready=ready-next; /*就緒對列頭指針后移到下一進程*/
}
void print1(PCB *q) /*進程PCB輸出*/
{
printf("進程名 已運行時間 還需要時間 時間片 狀態(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) /*如果運行指針不空*/
print1(run); /*輸出當前正在運行的PCB*/
p=ready; /*輸出就緒隊列PCB*/
while(p!=NULL)
{
print1(p);
p=p-next;
}
p=finish; /*輸出完成隊列的PCB*/
while(p!=NULL)
{
print1(p);
p=p-next;
}
}
void insert(PCB *p2) //輪轉(zhuǎn)法插入函數(shù)
{
tail-next=p2; //將新的PCB插入在當前就緒隊列的尾
tail=p2;
p2-next=NULL;
}
void create() /*創(chuàng)建進程PCB*/
{
PCB *p;
int i,time;
char na[10];
ready=NULL;
finish=NULL;
run=NULL;
printf("請輸入進程名稱和所需要CPU的時間:\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; /*時間片*/
if(ready!=NULL)
insert(p);
else
{
p-next=ready;
ready=p;
tail=p;
}
}
printf("------------------------------------------------------------------\n");
print(); /*輸出進程PCB信息*/
run=ready; /*將就緒隊列的第一個進程投入運行*/
ready=ready-next;
run-state='R';
}
void RR() //時間片輪轉(zhuǎn)調(diào)度
{
while(run!=NULL)
{
run-cputime=run-cputime+1;
run-needtime=run-needtime-1;
if(run-needtime==0) /*運行完將其變?yōu)橥瓿蓱B(tài),插入完成隊列*/
{
run-next=finish;
finish=run;
run-state='F';
run=NULL;
if(ready!=NULL)
firstin(); /*就緒對列不空,將第一個進程投入運行*/
}
else
if(ready!=NULL) /*如就緒隊列不空*/
{
run-state='W'; /*將進程插入到就緒隊列中等待輪轉(zhuǎn)*/
insert(run);
firstin(); /*將就緒對列的第一個進程投入運行*/
}
printf("------------------------------------------------------------------\n");
print(); /*輸出進程信息*/
}
}
void FCFS(double *arrive,double *runtime,double n) //先來先服務(wù)調(diào)度算法
{
start[0]=arrive[0]; //開始執(zhí)行時間=到達時間
endtime[0]=start[0]+runtime[0]; //完成時間=開始時間+服務(wù)時間
zhou[0]=endtime[0]-arrive[0]; //周轉(zhuǎn)時間=完成時間-到達時間
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"完成時間為:"endl;
for(int j=0;jn;j++)
coutendtime[j]" "endl;
cout"周轉(zhuǎn)時間為:"endl;
for(int k=0;kn;k++)
coutzhou[k]" "endl;
cout"帶權(quán)周轉(zhuǎn)時間為:"endl;
for(int m=0;mn;m++)
coutdqzhou[m]" "endl;
cout"平均周轉(zhuǎn)時間為:"endl;
coutaveragezhou" "endl;
cout"平均帶權(quán)周轉(zhuǎn)時間為:"endl;
coutaverage_zhou" "endl;
}
void SJF(double *arrive,double *runtime,double n) //短作業(yè)優(yōu)先調(diào)度算法
{
int end[maxnum]; //用于標記進程是否已經(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í)行完的進程號,next表示下一個要執(zhí)行的進程號
start[0]=arrive[0]; //開始執(zhí)行時間=到達時間
endtime[0]=start[0]+runtime[0]; //完成時間=開始時間+服務(wù)時間
zhou[0]=endtime[0]-arrive[0]; //周轉(zhuǎn)時間=完成時間-到達時間
dqzhou[0]=zhou[0]/runtime[0]; //帶權(quán)周轉(zhuǎn)時間=周轉(zhuǎn)時間/服務(wù)時間
averagezhou=zhou[0];
average_zhou=dqzhou[0];
end[now]=1; //執(zhí)行完的進程設(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"完成時間為:"endl;
for(int j=0;jn;j++)
coutendtime[j]" "endl;
cout"周轉(zhuǎn)時間為:"endl;
for(k=0;kn;k++)
coutzhou[k]" "endl;
cout"帶權(quán)周轉(zhuǎn)時間為:"endl;
for(int m=0;mn;m++)
coutdqzhou[m]" "endl;
cout"平均周轉(zhuǎn)時間為:"endl;
coutaveragezhou" "endl;
cout"平均帶權(quán)周轉(zhuǎn)時間為:"endl;
coutaverage_zhou" "endl;
}
int EDF() //最早截止時間的調(diào)度算法
{
int arrive_A,arrive_B; //標記進程A,進程B的到達時間
int zhouqi_A,zhouqi_B,serve_A,serve_B; //進程的周期時間和服務(wù)時間
int i,j,a=0,b=0,ka=0,kb=0; //ka,kb為開關(guān),i,j,a,b為進程下標
int num_a=0,num_b=0; //服務(wù)累計時間
printf("輸入進程A的周期時間,服務(wù)時間: ");
scanf("%d%d",zhouqi_A,serve_A);
printf("輸入進程B的周期時間,服務(wù)時間: ");
scanf("%d%d",zhouqi_B,serve_B);
for(int T=0;T=100;T++)
{
if(num_a==serve_A) //進程A完成
{
num_a=serve_A+1;
printf("當T=%d時",T);
printf("進程A%d結(jié)束\n",a);
if(num_bserve_B)
{
printf(" 調(diào)用進程B%d\n",b);
kb=1;
}
ka=0;
}
if(num_b==serve_B)
{
num_b=serve_B+1;
printf("當T=%d時",T);
printf("進程B%d結(jié)束\n",b);
if(num_aserve_A)
{
printf(" 調(diào)用進程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("當T=%d時,進程A%d和進程B%d同時產(chǎn)生,此時,",T,j,i);
if(zhouqi_A=zhouqi_B)
{
printf("調(diào)用進程A%d,阻塞進程B%d\n",j,i);
ka=1;
kb=0;
}
else
{
printf("調(diào)用進程B%d,阻塞進程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("當T=%d時",T);
printf("進程A%d產(chǎn)生 ",++a); //不可能與進程A競爭處理器
num_a=0;
if(num_bserve_B) //進程B沒有完成
if(arrive_B+zhouqi_Barrive_A+zhouqi_A) //若進程B最早截止時間大于進程A的,則執(zhí)行進程A
{
printf("進程A%d執(zhí)行。\n",a);
ka=1;
kb=0;
}
else //若進程B最早截止時間小于等于進程A的
printf("進程B%d繼續(xù)執(zhí)行。\n",b);
else //進程B完成
{
printf("進程A%d執(zhí)行。\n",a);
ka=1;
}
}
if(T%zhouqi_A!=0 T%zhouqi_B==0)
{
arrive_B=T;
printf("當T=%d時",T);
printf("進程B%d產(chǎn)生,",++b); //不可能與進程B競爭處理器
num_b=0;
if(num_aserve_A) //進程A沒有完成
if(arrive_B+zhouqi_B=arrive_A+zhouqi_A) //進程A的最早截止時間不小于B
printf("進程A%d繼續(xù)執(zhí)行。\n",a);
else
{
printf("進程B%d執(zhí)行。\n",b);
kb=1;
ka=0;
}
else //進程A完成
{
printf("進程B%d執(zhí)行。\n",b);
kb=1;
}
}
if(ka)
num_a++;
if(kb)
num_b++;
}
return 1;
}
int main()
{
system("color 0b"); //設(shè)置顏色
cout"最早截止時間的調(diào)度算法如下: "endlendl;
EDF();
int n;
coutendl;
cout"請輸入進程的數(shù)目: ";
cinn;
cout"請按進程到達時間從小到大依次輸入n個進程的到達時間: "endl;
for(int i=0;in;i++)
cinarrive[i];
cout"請按上面進程的順序依次輸入n個進程的服務(wù)時間: "endl;
for(int j=0;jn;j++)
cinruntime[j];
cout"先來先服務(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)建進程的數(shù)目:\n");
scanf("%d",N);
create();
RR();
return 0;
}
怎樣用 java 調(diào)度 SqlServer 代理的作業(yè)?
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
優(yōu)先級調(diào)度算法如何用JAVA實現(xiàn)
沒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)先級*/
#define TIME 5
/*數(shù)據(jù)結(jié)構(gòu)*/
/****************************************************************/
enum _Status/*進程狀態(tài)枚舉*/
{
READY =0,/*就緒*/
RUN,/*執(zhí)行中*/
SUSPEND,/*掛起*/
};
typedef enum _Status Status;
/****************************************************************/
struct _Pcb/*進程結(jié)構(gòu)*/
{
int PID;/*進程ID,ID為負數(shù)的進程為系統(tǒng)后備隊列的作業(yè)*/
int Time;/*進程運行需要的時間*/
int Prior;/*進程的優(yōu)先級,越大越優(yōu)先*/
Status Sts;/*狀態(tài)*/
struct _Pcb *Next;/*指向本進程隊列中下個進程的PCB*/
};
typedef struct _Pcb PCB;
/****************************************************************/
struct _Batch/*多道處理中的道結(jié)構(gòu)*/
{
PCB *pcb;/*該道當前正在處理的進程*/
struct _Batch *Next;/*下一道*/
};
typedef struct _Batch Batch;
/****************************************************************/
/*多道系統(tǒng)相關(guān)全局變量*/
PCB *ProcQueue = NULL;/*進程鏈表,按優(yōu)先級從大到小排列*/
Batch *BatchQueue = NULL;/*系統(tǒ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);/*向進程鏈表中按優(yōu)先級大小插入一個新進程*/
int InsertIDLE();/*向進程隊列中加入后備進程,當系統(tǒng)空閑時將被調(diào)入*/
int SortProcQueue();/*將進程鏈表按優(yōu)先級從大到小排列*/
int AddBatch();/*增加系統(tǒng)道數(shù)*/
int DeleteBatch();/*減少系統(tǒng)道數(shù)*/
int UnSuspendProc(int id);/*解除ID為id的進程的掛起狀態(tài)*/
int UpdateBatchs();/*多道系統(tǒng)根據(jù)進程鏈表進行更新,并將執(zhí)行完畢的進程刪除*/
int PassSeconds(int n);/*系統(tǒng)經(jīng)過n秒后計算數(shù)據(jù)并進行優(yōu)先級調(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)加入過多少進程,此值將是新進程的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)/*如果進程隊列為空*/
{
ProcQueue = pcb;
pcb-Next = NULL;
return TRUE;
}
last = ProcQueue;
now = last-Next;
if (pcb-Prior last-Prior)/*pcb將排在隊頭*/
{
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;/*上次遍歷是否無交換產(chǎn)生*/
if (ProcQueue==NULL || ProcQueue-Next==NULL)/*如果鏈表中無進程或只有一個進程*/
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)/*如果只有最后一個后備空閑進程*/
return FALSE;/**/
last = ProcQueue;
now = last-Next;
while (now-Next != NULL)/*查找到最后一個進程,該進程必定是后備空閑進程*/
{
last = now;
now = last-Next;
}
if (now==NULL || now-PID=0)/*未查找到后備進程*/
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)/*如果該進程是運行中的一般進程并已執(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)過多少時間段*/
time = time%TIME;
}
while (bt != NULL)/*更新進程運行時間*/
{
if (bt-pcb-PID=0)
{
bt-pcb-Time -= n;
if (bt-pcb-Time = 0)/*進程結(jié)束*/
{
ProcEnd = TRUE;
}
}
bt = bt-Next;
}
if (i 0)
{
while (pcb != NULL)/*更新進程優(yōu)先權(quán)(動態(tài)優(yōu)先權(quán))*/
{
if (pcb-Sts == RUN pcb-PID=0)/*運行的進程優(yōu)先權(quán)降低*/
{
pcb-Prior -= i;
if (pcb-Prior 0)
pcb-Prior = 0;
}
else if (pcb-Sts == SUSPEND pcb-PID=0)/*掛起的進程優(yōu)先權(quán)升高*/
pcb-Prior += i;
pcb = pcb-Next;
}
}
if (i0)
SortProcQueue();/*如果優(yōu)先級有變動則重新排序*/
if (ProcEnd || i0)
{
UpdateBatchs();/*更新多道進程*/
}
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()/*畫邊框和表頭*/
{
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");/*掛起隊列列表頭*/
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");/*就緒隊列列表頭*/
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;/*運行掛起和就緒的進程各有多少*/
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)/*該進程為正在運行的進程*/
{
putimage(x-WIDTH+1,y+1,black,COPY_PUT);
gprintf(x-WIDTH+2,y+2,"%d",numRun);
}
if (now-PID=0)/*該進程不是后備進程*/
{
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)/*如果輸入了回車鍵*/
{
if(pos!=0)/*如果位置不在第一位(回車鍵不準第一個輸入)*/
{
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)先級**********/
if (!DlgGetNum(buf,l,t,r,b,FALSE))
goto exit;
prior = atoi(buf);
/*******輸入時間**********/
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)/*時間過了一秒*/
{
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;
}
用c語言或者Java設(shè)計出一個任務(wù)調(diào)度器。。。
公眾:類PrivilegeProcess {
公共靜態(tài)無效的主要(字串[] args){
MyQueue的MyQueue的新MyQueue的();/ /聲明隊列
印刷電路板[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; + +){/ /初始化先進行排序,選擇排序這里使用的是高優(yōu)先級的一線隊
(J =我; pcb.length; J + +){
(PCB [I]。特權(quán)PCB [J]。特權(quán)){
段= PCB [1];
PCB [I] = PCB [J];
PCB [J] =段;
}
}
}
體系。通過out.println(“入隊后第一時間的進程的順序:”);
(INT I = 0; pcb.length; + +){
的System.out調(diào)用println(第一次入隊#程序名稱:“+ PCB [我]。名稱+ totaltime:”+ PCB [I]。totaltime +“的”特權(quán)“+ PCB [我]。特權(quán)); }
();
myqueue.start(PCB);
}
}
類MyQueue的{
INT指數(shù)= 0;
PCB [] PC =新的PCB [5];
PCB [] PC1 =新的PCB [4];
PCB溫度=新的PCB() BR /公共無效排隊(PCB工藝){/ /排隊算法
(指數(shù)== 5){
(“出界!”);
返回
}
PC [索引] =進程;
指數(shù)+ +;
}
公共:PCB DEQUEUE(){/ /出隊算法(索引== 0)
返回空;
(INT I = 0; pc1.length; + +){
PC1 [I] = PC [ +1];
}
指數(shù) -
溫度= PC [0];
(INT I = 0; pc1.length; + +){ BR / PC [I] = PC1 [I];
}
回報條件;
}
公共無效啟動(PCB [] PC){/ /進程表算法
(PC [0]。isNotFinish ==真| | PC [1 isNotFinish ==真| | PC [2 isNotFinish ==真| | PC [3]。時isNotFinish ==真| | PC [4]。isNotFinish ==){
/ / *注:| |運算符都是假的,所有的表達式結(jié)果為假,否則真
(INT I = 0; PC長度; + +){
PC [I]。運行(這一點); /} 的System.out.println();
(INT I = 0; pc.length; + +){/ /處理每個運行一次運行的時間片的長度重新排序優(yōu)先一旦
(J =我; pc.length; J + +){
如果(PC [I]特權(quán)PC [J]。特權(quán)){
溫度= PC [I];
PC [I] = PC [J];
PC [J] =溫度;
}
}
}
}
}
}
類PCB {/ /聲明過程級
和int名,totaltime ,運行時特權(quán);
布爾isNotFinish的;
公眾PCB(){
}
公開PCB(名稱,詮釋totaltime特權(quán)){
this.name =的名稱;/ /進程名
this.totaltime = totaltime ;/ /
this.privilege =特權(quán);/ /總時間優(yōu)先 this.runtime = 2 ;/ /時間片值是2
this.isNotFinish =真;/ /是否執(zhí)行完成
(“初始值:程序名稱:”+名+“totaltime:”+ totaltime +“特權(quán)”+特權(quán));
System.out的。調(diào)用println();
}
MyQueue的MQ公共無效的run(){/ /處理的基礎(chǔ)上實施的時間片算法
(totalTime 1){ totaltime =運行;/ /總時間大于1,總時間=總時間 - 時間片
特權(quán) -
(“程序名稱:”+姓名+“ remaintime:“+ +”特權(quán)“+特權(quán)); totaltime
的} else if(totaltime == 1){
totaltime - ;/ /總時間為1時,執(zhí)行時間為1
特權(quán) -
(“程序名稱:”+姓名+“remaintime:”+ totaltime +“特權(quán)”+特權(quán));
}其他{
isNotFinish =假;/ / 0,將isNotFinish標志設(shè)置為假
}
如果(isNotFinish ==真){br mq.deQueue(); mq.enQueue(本); }
}
}
本文題目:作業(yè)調(diào)度java代碼 作業(yè)調(diào)度算法sjf的實現(xiàn)
當前路徑:http://www.dlmjj.cn/article/dojjhpj.html