c語言周轉時間,c語言時間輸出

本文目錄一覽:

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

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-25 14:07
下一篇 2024-12-25 14:07

相關推薦

  • AES加密解密演算法的C語言實現

    AES(Advanced Encryption Standard)是一種對稱加密演算法,可用於對數據進行加密和解密。在本篇文章中,我們將介紹C語言中如何實現AES演算法,並對實現過程進…

    編程 2025-04-29
  • 學習Python對學習C語言有幫助嗎?

    Python和C語言是兩種非常受歡迎的編程語言,在程序開發中都扮演著非常重要的角色。那麼,學習Python對學習C語言有幫助嗎?答案是肯定的。在本文中,我們將從多個角度探討Pyth…

    編程 2025-04-29
  • Python被稱為膠水語言

    Python作為一種跨平台的解釋性高級語言,最大的特點是被稱為”膠水語言”。 一、簡單易學 Python的語法簡單易學,更加人性化,這使得它成為了初學者的入…

    編程 2025-04-29
  • OpenJudge答案1.6的C語言實現

    本文將從多個方面詳細闡述OpenJudge答案1.6在C語言中的實現方法,幫助初學者更好地學習和理解。 一、需求概述 OpenJudge答案1.6的要求是,輸入兩個整數a和b,輸出…

    編程 2025-04-29
  • 解決docker-compose 容器時間和伺服器時間不同步問題

    docker-compose是一種工具,能夠讓您使用YAML文件來定義和運行多個容器。然而,有時候容器的時間與伺服器時間不同步,導致一些不必要的錯誤和麻煩。以下是解決方法的詳細介紹…

    編程 2025-04-29
  • Python按位運算符和C語言

    本文將從多個方面詳細闡述Python按位運算符和C語言的相關內容,並給出相應的代碼示例。 一、概述 Python是一種動態的、面向對象的編程語言,其按位運算符是用於按位操作的運算符…

    編程 2025-04-29
  • Python語言由荷蘭人為中心的全能編程開發工程師

    Python語言是一種高級語言,很多編程開發工程師都喜歡使用Python語言進行開發。Python語言的創始人是荷蘭人Guido van Rossum,他在1989年聖誕節期間開始…

    編程 2025-04-28
  • Python語言設計基礎第2版PDF

    Python語言設計基礎第2版PDF是一本介紹Python編程語言的經典教材。本篇文章將從多個方面對該教材進行詳細的闡述和介紹。 一、基礎知識 本教材中介紹了Python編程語言的…

    編程 2025-04-28
  • 想把你和時間藏起來

    如果你覺得時間過得太快,每天都過得太匆忙,那麼你是否曾經想過想把時間藏起來,慢慢享受每一個瞬間?在這篇文章中,我們將會從多個方面,詳細地闡述如何想把你和時間藏起來。 一、一些時間管…

    編程 2025-04-28
  • Python語言實現人名最多數統計

    本文將從幾個方面詳細介紹Python語言實現人名最多數統計的方法和應用。 一、Python實現人名最多數統計的基礎 1、首先,我們需要了解Python語言的一些基礎知識,如列表、字…

    編程 2025-04-28

發表回復

登錄後才能評論