先來先服務演算法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);
}
}
⑷ 操作系統的作業,用C語言模擬先來先服務的方法的進程調度,請大神幫忙看看我這個代碼有什麼問題
假設你的系統是win7,而你參照的代碼是在xp上面寫的【就是調用xp的底層的介面】,有些會出現這種問題。。。你就行你安裝一個不符合這個系統版本的軟體,不是也是會提示類似的錯誤?
⑸ 操作系統的一個小問題~~~~~
先來先服務的調度演算法(C語言)主要數據結構:
typedef struct Lnode
{
char name[10];
struct Lnode *point;
int arrivetime;
int runtime;
char situation;
int wtime; //用來計算周轉時間
}Lnode,*Linklist;
流程圖:
代碼:
#include<stdio.h>
#include<malloc.h>
//使用鏈表
typedef struct Lnode
{
char name[10];
struct Lnode *point;
int arrivetime;
int runtime;
char situation;
int wtime; //用來計算周轉時間
}Lnode,*Linklist; //定義結點即定義pcb塊
void creatlist(Linklist &L,int n) //給每個結點初始化
{
int i;
Linklist p;
L=(Linklist)malloc(sizeof(Lnode));
L->point=NULL;
for(i=n;i>0;i--)
{
p=(Linklist)malloc(sizeof(Lnode));
printf("Please input the name of the course\n");
getchar();
gets(p->name);
printf("then input the arrivetime and the runtime!\n");
scanf("%d %d",&p->arrivetime,&p->runtime);
p->situation='R';p->wtime=0;
p->point=L->point;L->point=p;
}
printf("\n");
}
int Run(Linklist p,int &n) //n初值為1必須為引用
{
Linklist q=p->point;
printf("Now the course %s is running!\n",p->name); //其實q即為head
printf("The time of this course has already run is %d\n",n++);
printf("The lefttime of this course is %d\n",--p->runtime);
if(p->runtime==0) p->situation='C';
printf("The leftcourse of the queue are:");
while(q!=NULL&&q->situation=='R') //situation為R即為在隊列中就緒的
{ //直接列印輸出即可
printf("%s ",q->name);
q=q->point;
}
printf("\n");
return 0;
}
Linklist adjust(Linklist &L) //結點排序!!
{
Linklist bef,now,p,q; //刪掉now結點,先得找到他前面的結點bef
Linklist M; //p是靈活結點隨時調用!!
M=(Linklist)malloc(sizeof(Lnode)); //新建一個鏈表選擇需要的然後重新插入
M->point=NULL;
q=M;
while(L->point!=NULL)
{
int min=1000;
for(p=L->point;p!=NULL;p=p->point)
if(min>p->arrivetime)
{
min=p->arrivetime;
now=p;
}
for(bef=L;;bef=bef->point) //for(bef=L->point;;bef=bef->point)
if(bef->point==now) break; //前一個有可能是頭結點
bef->point=now->point; //刪除結點操作
{now->point=NULL;q->point=now; //刪完立即插入另一個鏈表
q=now;}
}
return M;
}
void main()
{
int m,n; //n是進程的個數
int alltime=0; //即前一個進程運行完的時間
int waittime=0;
Linklist L,p,M; //L是表頭,P可以靈活的隨便指
printf("pleast input the number of the course!\n");
scanf("%d",&n);
creatlist(L,n); //創建一個頭結點為L,個數為N的單向鏈表
M=adjust(L);
alltime=M->point->arrivetime;
for(p=M->point;p!=NULL;p=p->point)
{
if(p->arrivetime<alltime)
p->wtime=alltime-p->arrivetime;
p->wtime+=p->runtime;
alltime+=p->runtime; //這4行計算周轉時間
m=1; //這里的m表示的是RUN函數執行的次數
while(p->runtime!=0)
{
Run(p,m); //執行並列印輸出所需的結果
printf("The situation of this course is %c\n\n",p->situation);
}
}
for(p=M->point;p!=NULL;p=p->point)
{
printf("The course %s's waitingtime is %d\n",p->name,p->wtime);
waittime+=p->wtime;
}
printf("The average waitingtime of those courses is %d\n",waittime/n);
}
//等待時間+運行時間=周轉時間.. /進程個數=平均周轉
實驗1.2 設計一個按優先順序調度的演算法,實現處理機調度
主要數據結構:
typedef struct Lnode
{
char name[10];
struct Lnode *point;
int priority;
int runtime;
char situation;
}Lnode,*Linklist;
流程圖:
代碼:
#include<stdio.h>
#include<malloc.h>
typedef struct Lnode
{
char name[10];
struct Lnode *point;
int priority;
int runtime;
char situation;
}Lnode,*Linklist; //定義結點即定義pcb塊
void creatlist(Linklist &L,int n) //給每個結點初始化
{
int i;
Linklist p,l;
L=(Linklist)malloc(sizeof(Lnode));
L->point=NULL;
l=L;
for(i=n;i>0;i--)
{
p=(Linklist)malloc(sizeof(Lnode));
printf("Please input the name of the course\n");
getchar();
gets(p->name);
printf("then input the priority and the runtime!\n");
scanf("%d %d",&p->priority,&p->runtime);
p->situation='R';
p->point=NULL;l->point=p;l=p; //依次構造結點並非倒著構造
}
printf("\n");
}
int Run(Linklist &L,Linklist p) //L為鏈頭指針
{
Linklist h=L->point;
printf("Now the course %s is running!\n",p->name); //其實h即為head
printf("Now The priority of the course is %d\n",p->priority); //只列印輸出即可減操作已在
//主函數執行!!
printf("And The lefttime of this course is %d\n",--p->runtime);
if(p->runtime==0) p->situation='C';
if(p->situation=='R')
printf("This course's situation is R now!\n");
if(p->situation=='C') printf("This course's situation is C now!\n");
printf("The leftcourse of the queue are:");
while(h!=NULL) //situation為R即為在隊列中就緒的
{ //直接列印輸出即可
if(h->situation=='R')
printf("%s ",h->name);
h=h->point;
}
printf("\n");
printf("\n");
return 0;
}
//返回鏈表中優先順序最大的!!!
Linklist choose (Linklist L)
{
Linklist p,m;
int max=-10000;
for(p=L->point;p!=NULL;p=p->point)
{
if(max<p->priority)
{
max=p->priority;
m=p;
}
}
return m;
}
//返回鏈表中runtime最大的值並且得是最後的一個!!!
Linklist latest (Linklist L)
{
Linklist p,m;
int max=-1;
for(p=L->point;p!=NULL;p=p->point)
{
if(max<=p->runtime)
{
max=p->runtime;
m=p;
}
}
return m;
}
void main()
{
int n; //n是進程的個數
Linklist L,m,nn,late; //L是表頭,late指向runtime最長的,m為被選中的優先順序最高的
printf("pleast input the number of the course!\n");
scanf("%d",&n);
creatlist(L,n); //創建一個頭結點為L,個數為N的單向鏈表(按順序鏈接)
late=latest(L); //找到所需時間最長的
nn=late; //給nn賦初值,沒有報錯!
while(late->situation!='C') //循環條件
{
m=choose(L); //選出優先順序最高的
if(m->priority==nn->priority&&nn->runtime!=0) //若m經一次處理後優先順序仍是最高
m=nn; //則繼續執行他!!
//if(m->priority>0) //優先順序可以為負的!!考慮當優先順序都為0的情況
m->priority--; //priority放在Run函數外執行!!
if(m->runtime>0)
Run(L,m); //倆參數,頭結點,和m.
nn=m;
late=latest(L);
}
}
3.設計一個按時間片輪轉法實現處理機調度的程序。
(1)數據結構:
PCB格式:
進程名
連接指針
到達時間
要求運行時間
進程狀態
流程圖:
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
//使用鏈表?主要結構:
typedef struct Lnode
{
char name[10];
struct Lnode *point;
int arrivetime;
int runtime;
char situation;
}Lnode,*Linklist; //定義結點即定義pcb塊
void creatlist(Linklist &L,int n) //給每個結點初始化
{
int i;
Linklist p;
L=(Linklist)malloc(sizeof(Lnode));
L->point=NULL;
for(i=n;i>0;i--)
{
p=(Linklist)malloc(sizeof(Lnode));
printf("Please input the name of the course\n");
getchar();
gets(p->name);
printf("then input the arrivetime and the runtime!\n");
scanf("%d %d",&p->arrivetime,&p->runtime);
p->situation='R';
p->point=L->point;L->point=p;
}
printf("\n");
}
int Run(Linklist &L,Linklist p)
{
Linklist h=L->point;
printf("Now the course %s is running!\n",p->name); //其實q即為head
printf("The lefttime of this course is %d\n",--p->runtime);
if(p->runtime==0) p->situation='C';
printf("The situation of this course is %c\n",p->situation);
printf("The leftcourse of the queue are:");
while(h!=NULL) //situation為R即為在隊列中就緒的
{ //直接列印輸出即可
if(h->situation=='R')
printf("%s ",h->name);
h=h->point;
}
printf("\n\n");
return 0;
}
Linklist adjust(Linklist &L) //結點排序!!
{
Linklist bef,now,p,q; //刪掉now結點,先得找到他前面的結點bef
Linklist M; //p是靈活結點隨時調用!!
M=(Linklist)malloc(sizeof(Lnode)); //新建一個鏈表選擇需要的然後重新插入
M->point=NULL;
q=M;
while(L->point!=NULL)
{
int min=1000;
for(p=L->point;p!=NULL;p=p->point)
if(min>p->arrivetime)
{
min=p->arrivetime;
now=p;
}
for(bef=L;;bef=bef->point) //for(bef=L->point;;bef=bef->point)
if(bef->point==now) break; //前一個有可能是頭結點
bef->point=now->point; //刪除結點操作
{now->point=NULL;q->point=now; //刪完立即插入另一個鏈表
q=now;}
}
return M;
}
void Del(Linklist &L,Linklist &now)
{
Linklist bef,temp;
temp=now;//!要刪掉now 先把它賦值給另外一個Linklist型temp然後釋放掉temp!
for(bef=L;;bef=bef->point)
if(bef->point==now) break;//先找到now前的一個節點bef
bef->point=now->point; //bef指向now的point
now=bef;//!不加這句話不行!沒法執行指針加1操作
free(temp);
}
void main()
{
int n; //n是進程的個數
Linklist L,p; //L是表頭,P可以靈活的隨便指
printf("pleast input the number of the course!\n");
scanf("%d",&n);
creatlist(L,n); //創建一個頭結點為L,個數為N的單向鏈表
Linklist M=adjust(L); //M即為已排好序的鏈表頭結點
while(M->point!=NULL)
{
for(p=M->point;p!=NULL;p=p->point)
{
if(p->runtime!=0)
Run(M,p); //執行並列印輸出所需的結果
else
Del(M,p);
}
}
}
⑹ 求進程調度先來先服務演算法,短進程優先演算法完整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;
}
⑺ 先來先服務調度演算法。 優先順序調度演算法。 短作業優先調度演算法 輪轉調度演算法 響應比高優先調度演算法
你試一下
#include<stdio.h>
//using namespace std;
#define MAX 10
struct task_struct
{
char name[10]; /*進程名稱*/
int number; /*進程編號*/
float come_time; /*到達時間*/
float run_begin_time; /*開始運行時間*/
float run_time; /*運行時間*/
float run_end_time; /*運行結束時間*/
int priority; /*優先順序*/
int order; /*運行次序*/
int run_flag; /*調度標志*/
}tasks[MAX];
int counter; /*實際進程個數*/
int fcfs(); /*先來先服務*/
int ps(); /*優先順序調度*/
int sjf(); /*短作業優先*/
int hrrn(); /*響應比高優先*/
int pinput(); /*進程參數輸入*/
int poutput(); /*調度結果輸出*/
void main()
{ int option;
pinput();
printf("請選擇調度演算法(0~4):\n");
printf("1.先來先服務\n");
printf("2.優先順序調度\n");
printf(" 3.短作業優先\n");
printf(" 4.響應比高優先\n");
printf(" 0.退出\n");
scanf("%d",&option);
switch (option)
{case 0:
printf("運行結束。\n");
break;
case 1:
printf("對進程按先來先服務調度。\n\n");
fcfs();
poutput();
break;
case 2:
printf("對進程按優先順序調度。\n\n");
ps();
poutput();
break;
case 3:
printf("對進程按短作業優先調度。\n\n");
sjf();
poutput();
break;
case 4:
printf("對進程按響應比高優先調度。\n\n");
hrrn();
poutput();
break;
}
}
int fcfs() /*先來先服務*/
{
float time_temp=0;
inti;
intnumber_schel;
time_temp=tasks[0].come_time;
for(i=0;i<counter;i++)
{
tasks[i].run_begin_time=time_temp;
tasks[i].run_end_time=tasks[i].run_begin_time+tasks[i].run_time;
tasks[i].run_flag=1;
time_temp=tasks[i].run_end_time;
number_schel=i;
tasks[number_schel].order=i+1;
}
return 0;
}
int ps() /*優先順序調度*/
{
float temp_time=0;
inti=0,j;
intnumber_schel,temp_counter;
intmax_priority;
max_priority=tasks[i].priority;
j=1;
while((j<counter)&&(tasks[i].come_time==tasks[j].come_time))
{
if (tasks[j].priority>tasks[i].priority)
{
max_priority=tasks[j].priority;
i=j;
}
j++;
} /*查找第一個被調度的進程*/
/*對第一個被調度的進程求相應的參數*/
number_schel=i;
tasks[number_schel].run_begin_time=tasks[number_schel].come_time;
tasks[number_schel].run_end_time=tasks[number_schel].run_begin_time+tasks[number_schel].run_time;
tasks[number_schel].run_flag=1;
temp_time=tasks[number_schel].run_end_time;
tasks[number_schel].order=1;
temp_counter=1;
while (temp_counter<counter)
{
max_priority=0;
for(j=0;j<counter;j++)
{if((tasks[j].come_time<=temp_time)&&(!tasks[j].run_flag))
if (tasks[j].priority>max_priority)
{
max_priority=tasks[j].priority;
number_schel=j;
}
} /*查找下一個被調度的進程*/
/*對找到的下一個被調度的進程求相應的參數*/
tasks[number_schel].run_begin_time=temp_time;
tasks[number_schel].run_end_time=tasks[number_schel].run_begin_time+tasks[number_schel].run_time;
tasks[number_schel].run_flag=1;
temp_time=tasks[number_schel].run_end_time;
temp_counter++;
tasks[number_schel].order=temp_counter;
}return 0;
}
int sjf() /*短作業優先*/
{
float temp_time=0;
inti=0,j;
intnumber_schel,temp_counter;
float run_time;
run_time=tasks[i].run_time;
j=1;
while((j<counter)&&(tasks[i].come_time==tasks[j].come_time))
{
if (tasks[j].run_time<tasks[i].run_time)
{
run_time=tasks[j].run_time;
i=j;
}
j++;
} /*查找第一個被調度的進程*/
/*對第一個被調度的進程求相應的參數*/
number_schel=i;
tasks[number_schel].run_begin_time=tasks[number_schel].come_time;
tasks[number_schel].run_end_time=tasks[number_schel].run_begin_time+tasks[number_schel].run_time;
tasks[number_schel].run_flag=1;
temp_time=tasks[number_schel].run_end_time;
tasks[number_schel].order=1;
temp_counter=1;
while (temp_counter<counter)
{
for(j=0;j<counter;j++)
{
if((tasks[j].come_time<=temp_time)&&(!tasks[j].run_flag))
{run_time=tasks[j].run_time;number_schel=j;break;}
}
for(j=0;j<counter;j++)
{if((tasks[j].come_time<=temp_time)&&(!tasks[j].run_flag))
if(tasks[j].run_time<run_time)
{run_time=tasks[j].run_time;
number_schel=j;
}
}
/*查找下一個被調度的進程*/
/*對找到的下一個被調度的進程求相應的參數*/
tasks[number_schel].run_begin_time=temp_time;
tasks[number_schel].run_end_time=tasks[number_schel].run_begin_time+tasks[number_schel].run_time;
tasks[number_schel].run_flag=1;
temp_time=tasks[number_schel].run_end_time;
temp_counter++;
tasks[number_schel].order=temp_counter;
}return 0;
}
int hrrn() /*響應比高優先*/
{ int j,number_schel,temp_counter;
float temp_time,respond_rate,max_respond_rate;
/*第一個進程被調度*/
tasks[0].run_begin_time=tasks[0].come_time;
tasks[0].run_end_time=tasks[0].run_begin_time+tasks[0].run_time;
temp_time=tasks[0].run_end_time;
tasks[0].run_flag=1;
tasks[0].order=1;
temp_counter=1;
/*調度其他進程*/
while(temp_counter<counter)
{
max_respond_rate=0;
for(j=1;j<counter;j++)
{
if((tasks[j].come_time<=temp_time)&&(!tasks[j].run_flag))
{respond_rate=(temp_time-tasks[j].come_time)/tasks[j].run_time;
if (respond_rate>max_respond_rate)
{
max_respond_rate=respond_rate;
number_schel=j;
}
}
} /*找響應比高的進程*/
tasks[number_schel].run_begin_time=temp_time;
tasks[number_schel].run_end_time=tasks[number_schel].run_begin_time+tasks[number_schel].run_time;
temp_time=tasks[number_schel].run_end_time;
tasks[number_schel].run_flag=1;
temp_counter+=1;
tasks[number_schel].order=temp_counter;
}
return 0;
}
int pinput() /*進程參數輸入*/
{ int i;
printf("please input the processcounter:\n");
scanf("%d",&counter);
for(i=0;i<counter;i++)
{printf("******************************************\n");
printf("please input the process of %d th :\n",i+1);
printf("please input the name:\n");
scanf("%s",tasks[i].name);
printf("please input the number:\n");
scanf("%d",&tasks[i].number);
printf("please input the come_time:\n");
scanf("%f",&tasks[i].come_time);
printf("please input the run_time:\n");
scanf("%f",&tasks[i].run_time);
printf("please input the priority:\n");
scanf("%d",&tasks[i].priority);
tasks[i].run_begin_time=0;
tasks[i].run_end_time=0;
tasks[i].order=0;
tasks[i].run_flag=0;
}
return 0;
}
int poutput() /*調度結果輸出*/
{
int i;
float turn_round_time=0,f1,w=0;
printf("name number come_time run_timerun_begin_time run_end_time priority order turn_round_time\n");
for(i=0;i<counter;i++)
{
f1=tasks[i].run_end_time-tasks[i].come_time;
turn_round_time+=f1;
w+=(f1/tasks[i].run_time);
printf(" %s, %d, %5.3f, %5.3f, %5.3f, %5.3f, %d, %d,%5.3f\n",tasks[i].name,tasks[i].number,tasks[i].come_time,tasks[i].run_time,tasks[i].run_begin_time,tasks[i].run_end_time,tasks[i].priority,tasks[i].order,f1);
}
printf("average_turn_round_timer=%5.2f\n",turn_round_time/counter);
printf("weight_average_turn_round_timer=%5.2f\n",w/counter);
return 0;
}
⑻ 先來先服務演算法(C語言版)
#include<stdio.h>
#include<stdlib.h>
typedef struct process_FCFS{
float arrivetime;//到達時間
float servetime;//服務時間
float finishtime;//完成時間
float roundtime;//周轉時間
float daiquantime;//帶權周轉時間
struct process_FCFS *link;//結構體指針
}FCFS;
FCFS *p,*q,*head=NULL;
struct process_FCFS a[100];
//按到達時間進行冒泡排序
struct process_FCFS *sortarrivetime(struct process_FCFS a[],int n)
{
int i,j;
struct process_FCFS t;
int flag;
for(i=1;i<n;i++)
{
flag=0;
for(j=0;j<n-i;j++)
{
if(a[j].arrivetime>a[j+1].arrivetime)
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
flag=1;//交換
}
}
if(flag==0)//如果一趟排序中沒發生任何交換,則排序結束
break;
}
return a;
}
//先來先服務演算法
void print(struct process_FCFS a[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("到達時間:%f",a[i].arrivetime);
printf("服務時間:%f",a[i].servetime);
printf("完成時間:%f",a[i].finishtime);
printf("周轉時間:%f",a[i].roundtime);
printf("帶權周轉時間:%f",a[i].daiquantime);
printf("\n");
}
}
void Fcfs(struct process_FCFS a[],int n)
{
int i;
a[0].finishtime=a[0].arrivetime+a[0].servetime;
a[0].roundtime=a[0].finishtime+a[0].arrivetime;
a[0].daiquantime=a[0].roundtime/a[0].servetime;
for(i=0;i<n;i++)
{
if(a[i].arrivetime<a[i-1].finishtime)
{
a[i].finishtime=a[i-1].finishtime+a[i].servetime;
a[i].roundtime=a[i].finishtime-a[i].arrivetime;
a[i].daiquantime=a[i].roundtime/a[i].servetime;
}
else
{
a[i].finishtime=a[i].arrivetime+a[i].servetime;
a[i].roundtime=a[i].finishtime-a[i].arrivetime;
a[i].daiquantime=a[i].roundtime/a[i].servetime;
}
}
printf("先來先服務\n");
print(a,n);
}
//主函數
void main()
{
int n,i;
printf("請輸入有幾個進程\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("arrivetime");
scanf("%f",&a[i].arrivetime);
printf("servetime");
scanf("%f",&a[i].servetime);
}
Fcfs(a,n);
}
⑼ 用c語言實現先到先處理和最短路徑優先的cpu調度演算法
#include <stdio.h>
#define n 20
struct fcfs
{
int id;
int atime;
int runtime;
int ftime;
}f[n];
zcx(){
int xz;
int amount;
printf("**************分割線*********************\n");
printf("1.先來先服務\n");
printf("2.優先順序\n");
printf("請輸入你的選擇:");
scanf("%d",&xz);
printf("\n\n");
if(xz==1)
{
printf("你的選擇是先來先服務\n");
printf("請輸入進程數:");
scanf("%d",&amount);
yihao(amount);
}
else
{
printf("你的選擇是優先順序\n");
printf("請輸入進程數:");
scanf("%d",&amount);
erhao(amount);
}
}
yihao(int amount)
{
int i,j,l,k;
struct fcfs f[n];
printf("\n\n");
for(i=0;i<amount;i++)
{
printf("請輸入第 %d 個程序的信息\n",i+1);
printf("進程名 :");
scanf("%d",&f[i].id);
printf("到達時間:");
scanf("%d",&f[i].atime);
printf("運行時間:");
scanf("%d",&f[i].runtime);
}
for(i=0;i<amount;i++)
{
for(j=0;j<amount-i-1;j++)
{
if(f[j].atime>f[j+1].atime)
{
l=f[j].atime;
f[j].atime=f[j+1].atime;
f[j+1].atime=l;
k=f[j].id;
f[j].id=f[j+1].id;
f[j+1].id=k;
}
}
}
printf("進程名 開始時間 運行時間 結束時間 \n");
for(i=0;i<amount;i++)
{
f[i].ftime=f[i].atime+f[i].runtime;
printf("%d %d %d %d\n",f[i].id,f[i].atime,f[i].runtime,f[i].ftime);
f[i+1].atime=f[i].ftime;
}
zcx();
}
erhao(int amount)
{
int i,j,l,k;
struct fcfs f[n];
printf("\n\n");
for(i=0;i<amount;i++)
{
printf("請輸入第 %d 個程序的信息\n",i+1);
printf("進程名 :");
scanf("%d",&f[i].id);
printf("優先順序 :");
scanf("%d",&f[i].atime);
printf("運行時間:");
scanf("%d",&f[i].runtime);
}
for(i=0;i<amount;i++)
{
for(j=0;j<amount-i-1;j++)
{
if(f[j].atime>f[j+1].atime)
{
l=f[j].atime;
f[j].atime=f[j+1].atime;
f[j+1].atime=l;
k=f[j].id;
f[j].id=f[j+1].id;
f[j+1].id=k;
}
}
}
printf("進程名 優先順序 工作時間 \n");
for(i=0;i<amount;i++)
{
f[i].ftime=f[i].atime+f[i].runtime;
printf("%d %d %d \n",f[i].id,f[i].atime,f[i].runtime);
f[i+1].ftime=f[i].ftime+f[i+1].atime;
}
zcx();
}
void main()
{
zcx();
}
這是操作系統的作業吧
⑽ 急求 程序代碼 c/c++ 操作系統中的 處理機調度演算法
#include <iostream>
#include <stdio.h>
#include <string>
//#include <windows.h>
using namespace std;
//hyugtyftydrtdtrdrrtrdrt
struct Node
{
string name;//進程(作業)名稱
int arriveTime;//到達時間
int ServerTime;//服務時間
int leftTime;//the left time
Node *link;//指向下一個節點的指針
};
class CProcess
{
public:
CProcess();//構造函數
~CProcess();//析構函數
const CProcess &operator =(const CProcess& p);//重載賦值操作符
void insertNode(string &na,int& at,int& st);//插入新元素(at由小到大)到鏈表合適的位置
void sort();//按照服務時間由大到小排序
bool isEmpty();//判斷是否為空
void destroy();//銷毀
int length();//求出鏈表長度
void print();//列印出元素
void FCFS();//先到先服務
void SJF();//短進程(作業)優先
void RR(int& q);//時間片輪轉
void priority();//優先權調度
protected:
Node *first;
Node *last;
};
const CProcess& CProcess::operator=(const CProcess& p)
{
Node *newNode;
Node *Current;
if(this!=&p)//避免自己給自己賦值
{
if(first!=NULL)//如果鏈表不為空
destroy();
if(p.first==NULL)
{//如果要拷貝的對象為空
this->first = NULL;
this->last = NULL;
}
else
{
Current = p.first;
first= new Node;
first->name=Current->name;//
first->arriveTime=Current->arriveTime;
first->ServerTime=Current->ServerTime;
first->link =NULL;
last =first;
Current = Current->link;
while(Current!=NULL)
{
newNode = new Node;
newNode->name=Current->name;
newNode->arriveTime=Current->arriveTime;
newNode->ServerTime=Current->ServerTime;
newNode->link=NULL;
last->link=newNode;
last=newNode;
Current = Current->link;
}
}
}
return *this;
}
CProcess::CProcess()
{//構造函數
first=NULL;
last=NULL;
}
CProcess::~CProcess()
{
Node *temp;
while(first!=NULL)
{
temp=first;
first=first->link;
delete temp;
}
last=NULL;
}
void CProcess::insertNode(string &na,int& at,int& st)
{//按照到達時間升序排序
Node *Current;
Node *trailCurrent;//指向Current的前一個節點
Node *newNode;
bool found;
newNode = new Node;//建立一個新節點
newNode->name=na;
newNode->arriveTime=at;
newNode->ServerTime=st;
newNode->link=NULL;//
if(first==NULL)//如果第一個節點為空(如果是第一次插入元素)
first=newNode;//將新節點賦給第一個節點
else
{//如果不是第一次
Current =first;
found = false;
while(Current!=NULL && !found)
{
if(Current->arriveTime >= at)
found = true;
else
{
trailCurrent = Current;
Current = Current->link;
}
}
if(Current==first)
{
newNode->link = first;
first = newNode;
}
else
{
trailCurrent->link = newNode;
newNode->link = Current;
}
}
}
int CProcess::length()
{
int count =0;//聲明變數,並初始化為0(用來記錄長度)
Node *Current;
Current = first;
while(Current!=NULL)//當前節點不為空,記錄值自加,一直向後遍歷,
{
count++;
Current = Current->link;
}
return count;//返回長度
}
void CProcess::sort()//按照服務時間,升序排列
{//冒泡排序
string sname;
int at;
int st;
Node *Current;//指向當前節點
Node *trailCurrent;//指向當前節點的前一個節點
for(trailCurrent=first->link;trailCurrent!=NULL;trailCurrent=trailCurrent->link)//控制條件有問題
{
for(Current=trailCurrent->link;Current!=NULL;Current=Current->link)//控制條件有問題
{
if(trailCurrent->ServerTime > Current->ServerTime)
{
sname=trailCurrent->name;
at=trailCurrent->arriveTime;
st=trailCurrent->ServerTime;
trailCurrent->name=Current->name;
trailCurrent->arriveTime=Current->arriveTime;
trailCurrent->ServerTime=Current->ServerTime;
Current->name=sname;
Current->arriveTime=at;
Current->ServerTime=st;
}
}
}
}
bool CProcess::isEmpty()//判斷是否為空
{
return (first==NULL);//如果第一個節點為空,返回值
}
void CProcess::print()
{
Node *Current;
Current = first->link;//頭節點賦給當前節點
while(Current!=NULL)//當前節點不為空,一直向後遍歷列印
{
cout<<Current->name<<" ";
cout<<Current->arriveTime<<" ";
cout<<Current->ServerTime<<"\n";
Current = Current->link;
}
}
void CProcess::destroy()
{
Node *temp;//定義一個臨時指針變數
while(first!=NULL)
{
temp=first;
first=first->link;
delete temp;
}
last=NULL;
}
void CProcess::FCFS()//先到先服務
{
Node *Current;
int T0=0;//完成時間
int T1=0;//周轉時間
Current = first->link;//頭節點賦給當前節點
while(Current!=NULL)
{
if(T0 < Current->arriveTime)
{
T0=Current->arriveTime+Current->ServerTime;
T1=T0-Current->arriveTime;
cout<<Current->name<<"\t";//列印出進程名
cout<<T0<<"\t";//列印出完成時間
cout<<T1<<"\n";//列印出周轉時間
Current = Current->link;
}
else
{
T0=Current->ServerTime+T0;
T1=T0-Current->arriveTime;//周轉時間等於,完成時間 - 到達時間
cout<<Current->name<<"\t";//列印出進程名
cout<<T0<<"\t";//列印出完成時間
cout<<T1<<"\n";//列印出周轉時間
Current = Current->link;
}
}
}
void CProcess::SJF()//短進程(作業)優先
{
//首先執行第一個到達的作業
Node *Current;
int T0=0;//完成時間
int T1=0;//周轉時間
T0=first->link->ServerTime+T0;
T1=T0-first->link->arriveTime;
cout<<first->link->name<<"\t";
cout<<T0<<"\t";//列印出完成時間
cout<<T1<<"\n";//列印出周轉時間
first->link=first->link->link;//刪除
//執行剩下的
sort();//對剩下的排序
Current = first->link;//頭節點賦給當前節點
while(Current!=NULL)
{
if(T0 < Current->arriveTime)
{
T0=Current->arriveTime+Current->ServerTime;
T1=T0-Current->arriveTime;
cout<<Current->name<<"\t";//列印出進程名
cout<<T0<<"\t";//列印出完成時間
cout<<T1<<"\n";//列印出周轉時間
Current = Current->link;
}
else
{
T0=Current->ServerTime+T0;
T1=T0-Current->arriveTime;//周轉時間等於,完成時間 - 到達時間
cout<<Current->name<<"\t";//列印出進程名
cout<<T0<<"\t";//列印出完成時間
cout<<T1<<"\n";//列印出周轉時間
Current = Current->link;
}
}
}
void CProcess::RR(int& q)//時間片輪轉
{
cout<<"時間片輪轉操作完成!\n";
}
void CProcess::priority()//優先權調度
{
cout<<"優先權操作完成!\n";
}
void main()
{
CProcess p0,p1,p2,p3,p4;
int at,st;
string na;
int judge=1;//控制退出程序
int choice;//控制選擇操作
while(judge)
{
cout<<"********************************************************\n";
cout<<"****** 說明:本程序適用於單道進程(作業) ******\n";
cout<<"******** 請選擇您的操作 ***************\n";
cout<<"*********輸入相應的數字,按下(Enter)鍵!**************\n";
cout<<"************* 5.錄入信息 ************\n";
cout<<"************* 1.先到先服務 ************\n";
cout<<"************* 2.短進程(作業)優先 ************\n";
cout<<"************* 3.時間片輪轉 ************\n";
cout<<"************* 4.優先權(靜態)調度 ************\n";
cout<<"************* 0.退出程序 ************\n";
cout<<"********************************************************\n";
cin>>choice;
switch(choice)
{
case 0:
judge=0;
break;
case 5:
cout<<"請輸入信息以「end」結束輸入!\n";
cout<<"進程名 到達時間 服務時間"<<endl;
while(na.compare("end"))//如果相等則會返回0
{
p0.insertNode(na,at,st);
cin>>na>>at>>st;
}
cout<<"錄入成功,目前的信息為:\n";
cout<<"進程名 到達時間 服務時間"<<endl;
p0.print();
break;
case 1://先到先服務
p1=p0;//拷貝一份
if(p1.isEmpty())
{
cout<<"請先錄入信息\n";
break;
}
else
{
cout<<"先到先服務\n";
cout<<"進程名 完成時間 周轉時間\n";
p1.FCFS();
break;
}
case 2://短作業優先
p2=p0;//拷貝一份
//p2.sort();
//p2.print();
if(p2.isEmpty())
{
cout<<"請先錄入信息\n";
break;
}
else
{
cout<<"短作業優先\n";
cout<<"進程名 完成時間 周轉時間\n";
p2.SJF();
break;
}
case 3://時間片輪轉
p3=p0;//拷貝一份
int q;
if(p3.isEmpty())
{
cout<<"請先錄入信息\n";
break;
}
else
{
cout<<"請輸入時間片大小";
cin>>q;
cout<<"時間片輪轉\n";
cout<<"進程名 完成時間 周轉時間\n";
p3.RR(q);
break;
}
case 4://優先權
p4=p0;//拷貝一份
if(p4.isEmpty())
{
cout<<"請先錄入信息\n";
break;
}
else
{
cout<<"時間片輪轉\n";
cout<<"進程名 完成時間 周轉時間\n";
p4.priority();
break;
}
default:
cout<<"請選擇目錄中的選項!\n";
break;
}
}
return;
}