當前位置:首頁 » 操作系統 » 進程的調度演算法代碼

進程的調度演算法代碼

發布時間: 2024-04-25 04:25:20

⑴ 求進程調度演算法

進程調度源程序如下:

jingchendiao.cpp

#include "stdio.h"

#include <stdlib.h>

#include <conio.h>

#define getpch(type) (type*)malloc(sizeof(type))

#define NULL 0

struct pcb { /* 定義進程式控制制塊PCB */

char name[10];

char state;

int super;

int ntime;

int rtime;

struct pcb* link;

}*ready=NULL,*p;

typedef struct pcb PCB;

sort() /* 建立對進程進行優先順序排列函數*/

{

PCB *first, *second;

int insert=0;

if((ready==NULL)||((p->super)>(ready->super))) /*優先順序最大者,插入隊首*/

{

p->link=ready;

ready=p;

}

else /* 進程比較優先順序,插入適當的位置中*/

{

first=ready;

second=first->link;

while(second!=NULL)

{

if((p->super)>(second->super)) /*若插入進程比當前進程優先數大,*/

{ /*插入到當前進程前面*/

p->link=second;

first->link=p;

second=NULL;

insert=1;

}

else /* 插入進程優先數最低,則插入到隊尾*/

{

first=first->link;

second=second->link;

}

}

if(insert==0) first->link=p;

}

}

input() /* 建立進程式控制制塊函數*/

{

int i,num;

clrscr(); /*清屏*/

printf("\n 請輸入進程號?");

scanf("%d",&num);

for(i=0;i<num;i++)

{

printf("\n 進程號No.%d:\n",i);

p=getpch(PCB);

printf("\n 輸入進程名:");

scanf("%s",p->name);

printf("\n 輸入進程優先數:");

scanf("%d",&p->super);

printf("\n 輸入進程運行時間:");

scanf("%d",&p->ntime);

printf("\n");

p->rtime=0;p->state='w';

p->link=NULL;

sort(); /* 調用sort函數*/

}

}

int space()

{

int l=0; PCB* pr=ready;

while(pr!=NULL)

{

l++;

pr=pr->link;

}

return(l);

}

disp(PCB * pr) /*建立進程顯示函數,用於顯示當前進程*/

{

printf("\n qname \t state \t super \t ndtime \t runtime \n");

printf("|%s\t",pr->name);

printf("|%c\t",pr->state);

printf("|%d\t",pr->super);

printf("|%d\t",pr->ntime);

printf("|%d\t",pr->rtime);

printf("\n");

}
check() /* 建立進程查看函數 */

{

PCB* pr;

printf("\n **** 當前正在運行的進程是:%s",p->name); /*顯示當前運行進程*/

disp(p);

pr=ready;

printf("\n ****當前就緒隊列狀態為:\n"); /*顯示就緒隊列狀態*/

while(pr!=NULL)

{

disp(pr);

pr=pr->link;

}

}

destroy() /*建立進程撤消函數(進程運行結束,撤消進程)*/

{

printf("\n 進程 [%s] 已完成.\n",p->name);

free(p);

}

running() /* 建立進程就緒函數(進程運行時間到,置就緒狀態*/

{

(p->rtime)++;

if(p->rtime==p->ntime)

destroy(); /* 調用destroy函數*/

else

{

(p->super)--;

p->state='w';

sort(); /*調用sort函數*/

}

}

main() /*主函數*/

{

int len,h=0;

char ch;

input();

len=space();

while((len!=0)&&(ready!=NULL))

{

ch=getchar();

h++;

printf("\n The execute number:%d \n",h);

p=ready;

ready=p->link;

p->link=NULL;

p->state='R';

check();

running();

printf("\n 按任一鍵繼續......");

ch=getchar();

}

printf("\n\n 進程已經完成.\n");

ch=getchar();

}

⑵ 設計一個按優先數調度演算法實現處理器調度的程序。 高手幫忙!!

#include <stdio.h>
#include <stdlib.h> //提供atoi()函數
#include <conio.c> //提供clrscr()函數
#define M 10 //字元串大小常量
#define N 3 //進程數常量
#define SLOT 2
typedef struct node{
char name[M];
int prio; //優先順序
int round; //時間片長度
int cputime; //已經使用的cpu時間
int needtime;//需要多少cpu時間
int count; //計數器
char state; //進程的當前狀態
struct node *next; //指向下一個進程
}PCB;

PCB *finish,*ready,*tail,*run;

void ShowHead(char *s1,char *s2);
int Menu_Select();
void Creat(int select);
void InsertPriority(PCB *q);
void InsertSlot(PCB *q);
void PrintOneProcess(int select,PCB *q);
void PrintAllProcess(int select);
void FirstIn();
void FIFODo(int select);
void PriorityDo(int select);
void SlotDo(int select);
void Quit();
main()
{
while(1)
{
clrscr();
switch(Menu_Select())
{
case 1:
Creat(1);
FIFODo(1);
printf("\n\n\t\t按回車鍵返回菜單...");
getchar();
getchar();
break;
case 2:
Creat(2);
PriorityDo(2);
printf("\n\n\t\t按回車鍵返回菜單...");
getchar();
getchar();
break;
case 3:
Creat(3);
SlotDo(3);
printf("\n\n\t\t按回車鍵返回菜單...");
getchar();
getchar();
break;
case 0:
Quit();
break;
}
}
}

/*列印每個界面的開頭顯示*/
void ShowHead(char *s1,char *s2)
{
printf("\t ==================%s========================\n\n",s1);
printf("\t\t\t\t%s\n\n",s2);
printf("\t ========================================================\n\n");
}

/*主菜單*/
int Menu_Select()
{
int choose;
char s[2];
clrscr();
printf("====================進程調度演算法模擬程序=====================\n\n");
printf("\t\t 1.先進先出調度策略\n");
printf("\t\t 2.優先數調度策略\n");
printf("\t\t 3.時間片輪轉調度策略\n");
printf("\t\t 0.退出系統\n\n");
printf("=============================================================\n\n");
do
{
printf("\n請輸入你的選擇(0-3):");
scanf("%s",s);
choose=atoi(s);
}while(choose<0 || choose>3);
return choose;
}

/*創建調度演算法的鏈表*/
void Creat(int select)
{
PCB *p,*q; //q為就緒隊列的最後一個結點
int i,time,rounds;
char na[M];
ready=NULL;
finish=NULL;
run=NULL;
if(select==1) //先進先出調度創建
{
printf("\nEnter name and time of process:\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->state='W';
p->next=NULL;
if(ready!=NULL) //就緒隊列不為空
{
q->next=p;
q=p;
}
else //就緒隊列為空
{
p->next=ready;
ready=p;
q=ready;
}
}
clrscr();
ShowHead("先進先出調度策略","FIFO dispatching algorithm ");
printf("\t\t name cputime needtime state\n");
PrintAllProcess(select); //列印出初始狀態的信息
}
else if(select==2) //優先數調度創建
{
printf("\nEnter name and time of process:\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->state='W';
p->prio=50-time;
if(ready!=NULL) //就緒隊列不為空的時候
InsertPriority(p);
else //就緒隊列為空
{
p->next=ready;
ready=p;
}
}//end of for()
clrscr();
ShowHead("優先順序調度策略","Priority dispatching algorithm ");
printf("\t\t name cputime needtime prio state\n");
PrintAllProcess(select); //列印出初始狀態的信息
}//end of else if()
else if(select==3) //時間片輪轉調度創建
{
printf("\nEnter name and the time of process:\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->round=SLOT;
p->state='W';
if(ready!=NULL)
InsertSlot(p);
else
{
p->next=ready;
ready=p;
}
}
clrscr();
ShowHead("時間片輪轉調度策略","Time slot dispatching algorithm ");
printf("\n\t\t name cputime needtime count slot state\n");
PrintAllProcess(select); //列印出初始狀態的信息
}
run=ready; //從就緒隊列取一個進程,使其運行,同時就緒隊列的頭指針後移
run->state='R';
ready=ready->next;
}

/*優先調度:把進程插入到合適的位置,就緒隊列按優先順序由高到低的順序排列*/
void InsertPriority(PCB *q)
{
PCB *pre,*p;
int flag=1;
pre=NULL;
p=ready;
while(p!=NULL && flag)
{
if(p->prio>q->prio)
{
pre=p;
p=p->next;
}
else
flag=0;
}
if(pre==NULL)
{
q->next=ready;
ready=q;
}
else
{
pre->next=q;
q->next=p;
}
}

/*時間片輪轉:把結點插入到就緒隊列的末尾*/
void InsertSlot(PCB *q)
{
PCB *pre,*p;
pre=NULL;
p=ready;
while(p!=NULL)
{
pre=p;
p=p->next;
}
pre->next=q;
q->next=NULL; /*由於插入到隊列的末尾,所以必須要使其的下一個結點為空值*/
}

/*列印一個信息*/
void PrintOneProcess(int select,PCB *q)
{
if(select==1)
printf("\t\t %-10s%-10d%-10d%c\n",q->name,q->cputime,q->needtime,q->state);
else if(select==2)
printf("\t\t %-10s%-10d%-10d%-10d%c\n",q->name,q->cputime,q->needtime,q->prio,q->state);
else if(select==3)
printf("\t\t %-10s%-10d%-10d%-10d%-10d%c\n",q->name,q->cputime,q->needtime,q->count,q->round,q->state);
}

/*將所有的進程列印出來,按運行,就緒,完成順序列印*/
void PrintAllProcess(int select)
{
PCB *p;
if(run!=NULL)
PrintOneProcess(select,run);
p=ready;
while(p!=NULL)
{
PrintOneProcess(select,p);
p=p->next;
}
p=finish;
while(p!=NULL)
{
PrintOneProcess(select,p);
p=p->next;
}
}

/*把就緒隊列的第一個進程調入運行*/
void FirstIn()
{
run=ready;
ready=ready->next;
run->state='R';
}

/*先進先出調度策略*/
void FIFODo(int select)
{
while(run!=NULL)
{

run->cputime=run->cputime+1;
run->needtime=run->needtime-1;
if(run->needtime==0) //進程執行結束
{
printf("\n\t\t name cputime needtime state\n");
run->next=finish;
finish=run;
run->state='F';
run=NULL;
if(ready!=NULL)
FirstIn();
PrintAllProcess(1);
}
}
}

/*優先順序演算法*/
void PriorityDo(int select)
{
while(run!=NULL)
{
printf("\n\t\t name cputime needtime prio state\n");
run->cputime=run->cputime+1;
run->needtime=run->needtime-1;
run->prio=run->prio-3;
if(run->needtime==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';
InsertPriority(run);
FirstIn();
}
PrintAllProcess(select);
}
}

/*時間片輪轉演算法*/
void SlotDo(int select)
{
while(run!=NULL)
{
printf("\n\t\t name cputime needtime count slot state\n");
run->count=run->count+1;
run->cputime=run->cputime+1;
run->needtime=run->needtime-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; //時間片
if(ready!=NULL)
{
run->state='W';
InsertSlot(run);
FirstIn();
}
}
PrintAllProcess(select); //列印本次的所有記錄
}
}

void Quit()
{
char ch;
clrscr();
gotoxy(10,5);
printf("==========================================================\n\n");
printf(" Thank you for you using\n\n");
gotoxy(10,9);
printf("==========================================================\n\n");
gotoxy(13,15);

getchar();
printf("\n\t\tDo you really want to quit(y/Y or n/N):");
scanf("%c",&ch);
if(ch=='y' || ch=='Y')
{
printf("\n\t\t按任意鍵退出系統...");
getchar();
exit(0);
}

}

⑶ 進程調度演算法

演算法原理: 就是誰先來誰就先執行

演算法優點 :易於理解且實現簡單,只需要一個隊列,公平
演算法缺點 :有利於長進程,不利於短進程,有利於CPU 繁忙的進程,不利於I/O 繁忙的進程

演算法原理: 對預計執行時間短的進程優先執行。
演算法優點 :相比FCFS 演算法,該演算法可改善平均周轉時間和平均帶權周轉時間,縮短進程的等待時間,提高系統的吞吐量。
演算法缺點: 對長進程不利,可能長時間得不到執行產生飢餓,不能判斷執行的優先順序。

演算法原理: 同時考慮每個作業的等待時間長短和估計需要的執行時間長短,從中選出響應比最高的作業投入執行。響應比R定義: R =(W+T)/T = 1+W/T
T為該作業估計需要的執行時間,W為作業在後備狀態隊列中的等待時間。執行之前系統計算每個作業的響應比,選擇其中R最大者執行。這種演算法是介於前面兩種之間的一種折中演算法。
演算法優點: 長作業也有機會投入運行,避免了飢餓。
演算法缺點: 每次調度前要計算響應比,增加系統開銷。

演算法原理: 設置一個時間片,每個進程輪流使用時間片,若一個時間片內進程還沒結束,也會被其他的進程搶占時間片而退出執行,進入等待隊列。
演算法優點: 簡單易行、平均響應時間短。
演算法缺點: 不利於處理緊急作業。時間片的大小的設置對系統性能的影響很大,因此時間片的大小應選擇恰當

演算法原理: 根據優先順序的來判斷執行哪個進程。可以分為靜態優先順序和動態優先順序,即優先順序可以根據情況改變。比如如果一個進程等了很久,我們就可以把他的優先順序適當的提高。

演算法優點 :可以優先處理緊急事件,適用於實時操作系統。

演算法缺點 :可能導致那些優先順序低的進程飢餓。

UNIX操作系統採取的便是這種調度演算法。
演算法原理

實現先說明執行隊列優先順序Q1>Q2>Q3.......>Qn,分配的時間片Qn>Qn-1>...Q1.

進程在進入待調度的隊列等待時,首先進入優先順序最高的隊列Q1等待。若在Q1隊列裡面還沒執行完,則下放到Q2裡面,等Q1裡面的進程都執行完了之後再執行Q2。以此類推。
若在低優先順序的隊列中的進程在運行時,又有新到達的作業,那麼在運行完這個時間片後,CPU馬上分配給新到達的作業(搶占式)。
在多級反饋隊列調度演算法中,規定第一個隊列的時間片略大於多數人機交互所需之處理時間時,能夠較好的滿足各種類型用戶的需要。

⑷ 處理機調度的演算法的實現

程序設計思路:
自定義結構體PCB表(進程名name,進程優先數priority,進程執行時間time)以及進程就緒隊列Queue_Process(data[MAXSIZE]數組存放PCB,front,rear隊首隊尾指針),通過每次對進程就緒隊列進行進程優先數從大到小排序來確定進程執行的選擇,並且是採用動態優先數調度演算法(每次優先數減1,執行時間減1),每次輸出進程執行情況時只需要將隊首PCB調出執行,其他進程都處於動態就緒狀態,等進程執行結束後重新對進程就緒隊列排序。
程序中,採用結構體、隊列等數據結構,其中對隊列每次排序是採用冒泡排序演算法實現。 #include<iostream>#include<string>usingnamespacestd;#defineMAXSIZE10structPCB{intname;//進程名intpriority;//進程優先數inttime;//進程執行時間};structQueue_Process{PCBdata[MAXSIZE];//PCB隊列intfront;//隊首intrear;//隊尾};voidInitQueue(Queue_Process*Q){Q->front=Q->rear=0;}boolIsQueueEmpty(Queue_ProcessQ)//隊空判斷函數{return(Q.front==Q.rear)?true:false;}boolIsQueueFull(Queue_ProcessQ)//隊滿判斷函數{return(Q.front==(Q.rear+1)%MAXSIZE)?true:false;}voidEnQueue(Queue_Process*Q,PCBx)//入隊函數{if(IsQueueFull(*Q))//判斷隊列是否為滿{cout<<隊滿,入隊操作失敗!<<endl;exit(0);}//隊列非滿,將PCB入隊,將其中信息填入Q->data[Q->rear].name=x.name;Q->data[Q->rear].priority=x.priority;Q->data[Q->rear].time=x.time;Q->rear=(Q->rear+1)%MAXSIZE;//隊列隊尾指針後移}voidDeleQueue(Queue_Process*Q){if(IsQueueEmpty(*Q))//判斷隊列是否為空{cout<<隊空,出隊操作失敗!<<endl;exit(0);}Q->front=(Q->front+1)%MAXSIZE;//將隊列首指針後移}voidSortPCB(PCB*pcb,intn)//PCB優先數大小從大到小排列函數{PCBtemp;boolexchange=true;//交換標志for(inti=n-1;i>0&&exchange;i--)//冒泡排序演算法排序{exchange=false;for(intj=0;j<i;j++){if(pcb[j].priority<pcb[j+1].priority)//交換PCB中的數據{temp.name=pcb[j].name;temp.priority=pcb[j].priority;temp.time=pcb[j].time;pcb[j].name=pcb[j+1].name;pcb[j].priority=pcb[j+1].priority;pcb[j].time=pcb[j+1].time;pcb[j+1].name=temp.name;pcb[j+1].priority=temp.priority;pcb[j+1].time=temp.time;exchange=true;}}}}voidInput(PCB*pcb,intn)//進程信息輸入函數{cout<<請輸入每個進程的優先數和執行時間:<<endl;intp,t;for(inti=0;i<n;i++)//輸入每個進程的優先數和執行時間{cin>>p>>t;pcb[i].name=i;pcb[i].priority=p;pcb[i].time=t;}}voidDisplay(Queue_Process*queue)//輸出每個時刻的進程運行情況函數{cout<<進程名 <<優先數 <<執行時間 <<進程狀態<<endl;do{if(queue->data[queue->front].time>1){cout<<<<queue->data[queue->front].name<< <<<<queue->data[queue->front].priority<< <<<<queue->data[queue->front].time<< <<執行中...<<endl;queue->data[queue->front].priority-=1;queue->data[queue->front].time-=1;for(inti=1;i<queue->rear-queue->front;i++)cout<<<<queue->data[queue->front+i].name<< <<<<queue->data[queue->front+i].priority<< <<<<queue->data[queue->front+i].time<< <<等待中...<<endl;cout<<endl<<endl;}else{cout<<<<queue->data[queue->front].name<< <<<<queue->data[queue->front].priority<< <<<<queue->data[queue->front].time<< <<執行中...<<endl;for(inti=1;i<queue->rear-queue->front;i++)cout<<<<queue->data[queue->front+i].name<< <<<<queue->data[queue->front+i].priority<< <<<<queue->data[queue->front+i].time<< <<等待中...<<endl;cout<<進程<<queue->data[queue->front].name<<執行完畢!<<endl<<endl<<endl;DeleQueue(queue);}SortPCB(queue->data,queue->rear-queue->front);//對隊列中的優先數進程重新排序}while(!IsQueueEmpty(*queue));}voidmain(){PCBprocess[MAXSIZE-1];//進程數組Queue_Processqueue;//進程隊列intnum;//要輸入的進程數cout<<請輸入進程同步數num:<<endl;cin>>num;InitQueue(&queue);//初始化隊列Input(process,num);for(inti=0;i<num;i++)//通過循環使每個進程入隊{EnQueue(&queue,process[i]);}SortPCB(queue.data,queue.rear-queue.front);//對進程按優先數從大到小排列cout<<endl<< 進程執行情況:<<endl;Display(&queue);//進程運行函數}

⑸ 基於優先順序的時間片輪轉進程調度演算法

#include<iostream>
using namespace std;
struct PCB_Type{
char name;
int cpu_time;
};
struct QueueNode{
struct PCB_Type PCB;
struct QueueNode *next;
};

int main(){
int m,n,t;
int usecpu=0,unusecpu=0;
cout<<"輸入就緒隊列中的進程數目m:";
cin>>m;
cout<<"輸入阻塞隊列中的進程的數目n:";
cin>>n;
cout<<"輸入喚醒系統資源的相隔時間片個數t:";
cin>>t;
struct QueueNode *readyhead=new QueueNode ,*readytail=new QueueNode,
*blockedhead=new QueueNode,*blockedtail=new QueueNode;
// readyhead=NULL;readytail=NULL;blockedhead=NULL;blockedtail=NULL;
readyhead=readytail;
blockedhead=blockedtail;
for(int i=1;i<=m;i++){
struct QueueNode *t1=new QueueNode;
cout<<"輸入就緒隊列中進程的name和cpu_time:";
cin>>t1->PCB.name>>t1->PCB.cpu_time;
readytail->next=t1;
readytail=t1;
}
for(int j=1;j<=n;j++){
struct QueueNode *t2=new QueueNode;
cout<<"輸入阻塞隊列中進程的name和cpu_time:";
cin>>t2->PCB.name>>t2->PCB.cpu_time;
blockedtail->next=t2;
blockedtail=t2;
}

cout<<"輸出就緒隊列的進程信息:";
for(struct QueueNode *t3=readyhead->next;t3!=readytail->next;t3=t3->next){
cout<<t3->PCB.name<<"、"<<t3->PCB.cpu_time<<"---> ";
}
cout<<"無進程";
cout<<endl;
cout<<"輸出阻塞隊列的進程信息:";
struct QueueNode *t4;
t4=blockedhead->next;
while(t4!=blockedtail->next){
cout<<t4->PCB.name<<"、"<<t4->PCB.cpu_time<<"---> ";
t4=t4->next;
}
cout<<"無進程";
cout<<endl<<"進程的運行順序:";
int x=0;
while(readyhead!=readytail||blockedhead!=blockedtail){
if(readyhead!=readytail){
struct QueueNode *p=readyhead->next;
cout<<p->PCB.name<<",";
p->PCB.cpu_time--;
usecpu++;
if(readyhead->next!=readytail){
if(p->PCB.cpu_time>0){
readyhead->next=p->next;//出隊列
readytail->next=p;
readytail=p;
}
else{
readyhead->next=p->next;
delete p;
}
}
else//隊列中只有兩個節點 頭結點和尾結點
{
if(p->PCB.cpu_time<=0){readytail=readyhead;//只有進程為執行完,就繼續執行,完成之後,把隊列清空,釋放指針p;就緒隊列無進程
delete p;}
}

}
else
{
unusecpu++;
cout<<"_,";
}
x++;
if(x==t){
if(blockedhead!=blockedtail){

struct QueueNode *q=blockedhead->next;
if(blockedhead->next!=blockedtail)
{
blockedhead->next=q->next;
}
else
{
blockedhead=blockedtail;
}
readytail->next=q;
readytail=q;
x=0;
}
}
}
cout<<endl;
cout<<"cpu的利用率="<<usecpu<<"/"<<usecpu+unusecpu<<endl;
return 0;
}

#include"stdio.h"
#include"stdlib.h"
#include "string.h"
#define WAIT 1
#define RUN 2
#define FINISH 3

typedef struct pcb
{
int num;
struct pcb *next;
int priority;
int timeneed;
int state;
}pcb;/*用此結構體來模擬一個進程*/

struct pcb *head;
struct pcb *run;

pcb *jccreat(int n)/*此函數用於創建進程隊列*/
{
int i=1;
pcb *head,*p,*q;

randomize();/*隨機函數的初始化*/

head=(pcb *)malloc(sizeof(pcb));/*創建一個空表頭*/
p=head;

for(i=1;i<=n;i++)/*用循環來創建指定個 結點*/
{
q=(pcb *)malloc(sizeof(pcb));
p->next=q;
q->num=i;
q->next=NULL;
q->priority=random(10);/*隨機產生優先順序*/
q->timeneed=random(10);/*隨機產生運行時間*/
q->state=WAIT;
p=q;
}

return head;/*返回表頭指針*/
}

pcb *getmaxpriority(struct pcb *head)/*此函數用來挑選一個優先順序最大的進程來執行*/
{
struct pcb *p,*q;
int max;
p=head->next;
max=p->priority;/*初始max為隊首結點的優先順序*/
q=p;
while(p) /*當p不為空時,進行逐一比較*/
{
if(p->priority>max)/*逐一比較,選出優先順序最大的結點*/
{max=p->priority;
q=p;}
p=p->next;
}
return q;
}

void delect(struct pcb *head,struct pcb *run)/*此函數用來將運行完的進程刪除出進程隊列*/
{
struct pcb *q=head;

while(q->next)/*掃描進程隊列,找到執行完了的進程*/
{
if(q->next->num==run->num)/*判斷是不是已完成的進程*/
{
if(run->next!=NULL)
q->next=run->next;
else q->next=NULL;
free(run);/*釋放申請的空間*/
return;
}
q=q->next;
}

}

void control()/*此函數是用來控制各個進程的執行和調度*/
{
struct pcb *p;
run=head->next;/*初始讓第一個進程運行*/
run->state=RUN;
while(run) /*當進程狀態是不為空時運行*/
{
if(run->timeneed>0)/*如果當前run指針指向的進程所需時間不為零,狀態為運行狀態,就讓這個進程運行*/
if(run->state==RUN)
{printf("pcb%d is running.\n",run->num);
printf("Waiting list:");/*顯示整個等待隊列*/
p=head->next;
while(p)
{
if(p!=run)
printf("pcb%d ",p->num);
p=p->next;
}
printf("\n");
delay(10000000);/*模擬進程運行*/
run->timeneed--;/*進程需要時間減一*/
run->priority=run->priority-3;/*進程優先順序減三*/
}

if(run->timeneed!=0)
{
if(run->priority<=head->next->priority)/*如果當前運行完的進程的優先順序低於隊首進程的優先順序*/
{run->state=WAIT;
run=getmaxpriority(head);/*則從進程隊列中挑選一個優先順序最大的進程來運行*/
run->state=RUN;}
}
else
{ printf("pcb%d is finished.\n",run->num);
delect(head,run);/*刪除該結點*/
if(head->next!=NULL)/*判斷進程隊列是不是為空*/
{run=head->next;
run->state=RUN;}
else
{printf("All progresses are done.\n");

return;}
}
}
}

main()
{
int n;
int flag=1;

printf("Enter the number of the progresses:");
scanf("%d",&n);/*輸入要創建的進程的數量*/

head=jccreat(n);/*創建進程隊列,將鏈表的表頭賦給head指針*/
run=head->next;/*run指針指向正在運行的進程的pcb*/
while(run)
{
printf("num: %d ,priority: %d ,timenees: %d \n",run->num,run->priority,run->timeneed);

run=run->next;
} /*將剛創建的進程隊列列印出來*/
while(flag)/*由flag的值判斷是否繼續執行control()函數*/
{
if(head->next)/*判斷進程是否完成*/
control();
else flag=0;
}
getch();
}

選一個把

⑹ (操作系統)編寫進程調度演算法程序

這個只要調個隊列就夠了,主要的代碼應該這樣寫就可以了:
while(!que.empty()) //que是進程的隊列
{
pid_t pid = fork();
switch(pid)
{
case -1 : printf("ERROR:cannot create the child process.\n"); break;
case 0: execlp(……); //這里execlp調用的是que.top(),這個進程要寫在當前目錄下
default : wait(NULL);
}
}
等待某一時間不能繼續執行,可以使wait函數的參數取具體的狀態值,希望可以幫到你!

⑺ )用C語言(或其它語言,如Java)編程實現對N個進程採用某種進程調度演算法(如動態優先權調度

公眾:類PrivilegeProcess {
公共靜態無效的主要(字串[] 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; + +){/ /初始化先進行排序,選擇排序這里使用的是高優先順序的一線隊

(J =我; <pcb.length; J + +){

(PCB [I]。特權<PCB [J]。特權){

段= PCB [1];

PCB [I] = PCB [J];

PCB [J] =段;

}

}

}

體系。通過out.println(「入隊後第一時間的進程的順序:」);

(INT I = 0; <pcb.length; + +){

的System.out調用println(第一次入隊#程序名稱:「+ PCB [我]。名稱+ totaltime:」+ PCB [I]。totaltime +「的」特權「+ PCB [我]。特權); }

();

myqueue.start(PCB);

}

}

類MyQueue的{

INT指數= 0;

PCB [] PC =新的PCB [5];

PCB [] PC1 =新的PCB [4];

PCB溫度=新的PCB() BR />公共無效排隊(PCB工藝){/ /排隊演算法

(指數== 5){

(「出界!」);

返回

}

PC [索引] =進程;

指數+ +;

}

公共:PCB DEQUEUE(){/ /出隊演算法(索引== 0)

返回空;

(INT I = 0; <pc1.length; + +){

PC1 [I] = PC [ +1];

}

指數 -

溫度= 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 ==){

/ / *註:| |運算符都是假的,所有的表達式結果為假,否則真

(INT I = 0; <PC長度; + +){

PC [I]。運行(這一點); />} 的System.out.println();

(INT I = 0; <pc.length; + +){/ /處理每個運行一次運行的時間片的長度重新排序優先一旦

(J =我; <pc.length; J + +){

如果(PC [I]特權<PC [J]。特權){

溫度= PC [I];

PC [I] = PC [J];

PC [J] =溫度;

}

}

}

}

}

}

類PCB {/ /聲明過程級

和int名,totaltime ,運行時特權;

布爾isNotFinish的;

公眾PCB(){

}

公開PCB(名稱,詮釋totaltime特權){

this.name =的名稱;/ /進程名

this.totaltime = totaltime ;/ /

this.privilege =特權;/ /總時間優先 this.runtime = 2 ;/ /時間片值是2

this.isNotFinish =真;/ /是否執行完成

(「初始值:程序名稱:」+名+「totaltime:」+ totaltime +「特權」+特權);

System.out的。調用println();

}

MyQueue的MQ公共無效的run(){/ /處理的基礎上實施的時間片演算法

(totalTime> 1){ totaltime =運行;/ /總時間大於1,總時間=總時間 - 時間片

特權 -

(「程序名稱:」+姓名+「 remaintime:「+ +」特權「+特權); totaltime

的} else if(totaltime == 1){

totaltime - ;/ /總時間為1時,執行時間為1
>特權 -

(「程序名稱:」+姓名+「remaintime:」+ totaltime +「特權」+特權);

}其他{

isNotFinish =假;/ / 0,將isNotFinish標志設置為假

}

如果(isNotFinish ==真){br mq.deQueue();

mq.enQueue(本);

}

}
}

熱點內容
如何區分安卓原裝充電器 發布:2024-05-05 01:41:23 瀏覽:72
怎麼從蘋果轉移到安卓 發布:2024-05-05 01:41:20 瀏覽:721
支付寶付款碼怎麼設置密碼 發布:2024-05-05 01:27:36 瀏覽:878
qtp錄制的腳本 發布:2024-05-05 01:14:04 瀏覽:367
如何安裝卡羅拉安卓系統 發布:2024-05-05 01:09:00 瀏覽:985
sql創建表查詢表 發布:2024-05-05 01:00:12 瀏覽:799
食色抖音上傳 發布:2024-05-05 00:55:56 瀏覽:658
java圖片下載 發布:2024-05-05 00:50:45 瀏覽:598
唱吧如何上傳伴奏 發布:2024-05-05 00:49:04 瀏覽:445
什麼配置單反拍視頻最好 發布:2024-05-05 00:30:56 瀏覽:479