本文目錄一覽:
C語言編程實現時間片輪轉演算法,盡量寫得簡單易懂,謝謝
#includestdlib.h
#define MAX 5 //進程數量
#define RR 2 //時間片大小
/*時間片輪轉演算法*/
struct pro
{
int num;
int arriveTime;
int burst;
int rt; //記錄進程被運行的次數
struct pro *next;
};
int TOTALTIME; //記錄所有進程的總時間
//函數聲明
struct pro* creatList();
void insert(struct pro *head,struct pro *s);
struct pro* searchByAT(struct pro *head,int AT);
void del(struct pro* p);
int getCount(struct pro *head,int time);
struct pro* searchEnd(struct pro *head);
void move(struct pro *headF,struct pro *headT,int n);
struct pro* creatList() //創建鏈表,按照進程的到達時間排列,記錄所有進程的信息
{
struct pro* head=(struct pro*)malloc(sizeof(struct pro));
head-next=NULL;
struct pro* s;
int i;
TOTALTIME=0;
for(i=0;iMAX;i++)
{
s=(struct pro*)malloc(sizeof(struct pro));
printf(“請輸入進程名:\n”);
scanf(“%d”,(s-num));
printf(“請輸入到達時間:\n”);
scanf(“%d”,(s-arriveTime));
printf(“請輸入運行時間:\n”);
scanf(“%d”,(s-burst));
TOTALTIME+=s-burst; //計算總時間
s-rt=1; //rt的初始值為1
s-next=NULL;
insert(head,s);
}
return head; //到達隊列中的進程按照其到達時間的先後順序排列
}
void insert(struct pro *head,struct pro *s) //插入節點
{
struct pro *p=searchByAT(head,s-arriveTime);
s-next=p-next;
p-next=s;
return;
}
struct pro* searchByAT(struct pro *head,int AT) //查找第一個到達時間大於等於AT的節點,返回其前一個指針
{
struct pro *p,*q;
p=head;
q=head-next;
while(q!=NULLq-arriveTime=AT)
{
p=q;
q=q-next;
}
return p;
}
void del(struct pro* p) //刪除p的下一個節點
{
struct pro *tmp;
tmp=p-next;
p-next=tmp-next;
free(tmp);
return;
}
int getCount(struct pro *head,int time) //察看在time之前到達但未移動到運行隊列的進程數量
{
int count=0;
struct pro *s,*t;
s=head;
t=s-next;
while(t!=NULLt-arriveTime=time)
{
s=t;
t=t-next;
count++; //count記錄當前時刻到達的進程數
}
return count;
}
struct pro* searchEnd(struct pro *head) //查找並返回循壞隊列的尾節點的前一個節點
{
struct pro *p,*q;
p=head;
q=head-next;
while(q-next!=head)
{
p=q;
q=q-next;
}
return p;
}
void move(struct pro *headF,struct pro *headT,int n) //將headF後的n個節點移動到循環隊列headT中
{
struct pro *r,*s,*t;
s=headF;
t=s-next;
r=t; //r記錄要移動的第一個節點
while(n1)
{
t=t-next;
n–;
}
s-next=t-next; //以上完成從原隊列中摘除相關節點,r,t分別為第一個和最後一個節點
s=searchEnd(headT);
t-next=s-next;
s-next=r;
}
void run(struct pro *head)
{
int time=0; //記錄當前時間
int newarrive;//新到達進程數
struct pro *runhead=(struct pro*)malloc(sizeof(struct pro));
runhead-next=runhead; //創建新的循環鏈表,存放當前就緒隊列中的進程
struct pro *p,*q;
p=runhead;
q=p-next; //q記錄當前應當運行的進程
while(time=TOTALTIME)
{
newarrive=getCount(head,time);
if(newarrive0)
move(head,runhead,newarrive); //將head後的newarrive個節點移動到runhead隊列中
if(runhead-next==runhead) //就緒隊列中沒有進程
time++;
else if(q==runhead)
{
p=q;
q=q-next;
}
else
{
printf(“進程名:%d\n”,q-num);
printf(“到達時間:%d\n”,q-arriveTime);
if(q-rt==1)
printf(“響應時間:%d\n”,time-q-arriveTime);
else
printf(“第%d次運行開始時間:%d\n”,q-rt,time);
if(q-burst=RR)
{
time+=q-burst;
printf(“第%d次運行結束時間:%d\n”,q-rt,time);
printf(“周轉時間:%d\n”,time-q-arriveTime);
printf(“************************************\n”);
struct pro *tmp=q;
q=q-next;
p-next=q;
free(tmp);
}
else //q-burstRR
{
time+=RR;
printf(“第%d次運行結束時間:%d\n”,q-rt,time);
printf(“************************************\n”);
q-burst-=RR;
q-rt++;
p=q;
q=q-next;
}
}
}
}
void main()
{
struct pro *head=creatList();
printf(“當前時間片大小為:%d\n”,RR);
run(head);
}
C語言問題
將printf(“%s\t%4d\t%4d\t%4d\t%4d\t%4d\t\t%4.2lf\t%4.2lf\n”,pro[s].name,pro[s].arrive,pro[s].service,pro[s].wait,pro[s].begin,pro[s].over,pro[s].cycle,pro[s].right);
中pro[s]的s改為t.
短作業優先演算法用c語言如何寫?
這樣寫應該可以:
#includeiostream.h
#includestdio.h
struct pcb{
char pno;
int come_time; //到達時間
int run_time; //服務時間
};
float fcfs(pcb pro[],int n)
{
struct pcb temp;
int i,j,k; //time為當前時間
float weight_time=0,time=0; //記錄周轉時間的和
//temp=(pcb)malloc(sizeof(pcb));
cout”進程調度情況如下:”endl;
cout”進程號 到達時間 服務時間 周轉時間:”endl;
//選擇排序過程,按到達時間升序排列
for(i=0;in-1;i++)
{
k=i;
for(j=i+1;jn;j++)
if(pro[k].come_timepro[j].come_time)
k=j;
if(k!=i)
{
temp=pro[i];
pro[i]=pro[k];
pro[k]=temp;
}
}
for(i=0;in;i++)
{ time+=pro[i].run_time;
weight_time+=(time-pro[i].come_time)/pro[i].run_time; //(time-pro[i].come_time)/pro[i].run_time為排序後第i個進程的周轉時間
coutpro[i].pno” “pro[i].come_time” “pro[i].run_time” “(time-pro[i].come_time)/pro[i].run_timeendl;
}
return weight_time/=n; //返回平均帶權周轉時間
}
void insert(pcb pro[],pcb pro1,int start,int end)//將一pcb類型的元素插入到有序數組中,最後還保持有序
{
int i=end;
while((i–)start)
if(pro[i].run_timepro1.run_time)pro[i+1]=pro[i];
pro[i]=pro1;
}
float sjp(pcb pro[],int n)
{
int i,first=0,count,flag[20],k,min;
float time=0,weight_time=0;
//調度第一個到達內存的進程
for(i=1;in;i++)
{
if(pro[first].come_timepro[i].come_time) first=i;
flag[i]=0;
}
flag[first]=1;
time=(float)pro[first].run_time;
weight_time=1;
coutpro[first].pno” “pro[first].come_time” “pro[first].run_time” “weight_timeendl;
//pro_temp[0]=pro[first];
count=n-1;
while(count)
{
k=0;
min=32767; //設置一個較大的閾值,
for(i=0;in;i++) //找到一個未被訪問的,作業較短的且已經到達內存的作業調度
if((i!=first)(flag[i]==0)(time=pro[i].come_time)(minpro[i].run_time))
{
k=i;
min=pro[i].run_time;
}
flag[k]=1; //訪問後置標記為訪問
time+=pro[k].run_time;
weight_time+=(time-pro[k].come_time)/pro[k].run_time;
coutpro[k].pno” “pro[k].come_time” “pro[k].run_time” “(time-pro[k].come_time)/pro[k].run_timeendl;
count–; //每調度一個作業,count減1
}
return weight_time/=n;
}
void main()
{
pcb pro[5]={{‘C’,2,5},{‘A’,0,4},{‘B’,1,3},{‘D’,3,2},{‘E’,4,4}};
coutfcfs(pro,5)endl;
coutsjp(pro,5)endl;
}
先來先服務演算法(C語言版)
#includestdio.h
#includestdlib.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;in;i++)
{
flag=0;
for(j=0;jn-i;j++)
{
if(a[j].arrivetimea[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;in;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;in;i++)
{
if(a[i].arrivetimea[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;in;i++)
{
printf(“arrivetime”);
scanf(“%f”,a[i].arrivetime);
printf(“servetime”);
scanf(“%f”,a[i].servetime);
}
Fcfs(a,n);
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/291843.html