導航:首頁 > 源碼編譯 > c語言時間片演算法

c語言時間片演算法

發布時間:2022-05-01 11:08:00

❶ C語言編程實現時間片輪轉演算法,盡量寫得簡單易懂,謝謝

#include<stdlib.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;i<MAX;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!=NULL&&q->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!=NULL&&t->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(n>1)
{
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(newarrive>0)
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->burst>RR
{
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語言模擬實現

一、目的和要求
進程調度是處理機管理的核心內容。本實驗要求用高級語言編寫模擬進程調度程序,以便加深理解有關進程式控制制快、進程隊列等概念,並體會和了解優先數演算法和時間片輪轉演算法的具體實施辦法。
二、實驗內容
1.設計進程式控制制塊PCB的結構,通常應包括如下信息:
進程名、進程優先數(或輪轉時間片數)、進程已佔用的CPU時間、進程到完成還需要的時間、進程的狀態、當前隊列指針等。
2.編寫兩種調度演算法程序:
優先數調度演算法程序
循環輪轉調度演算法程序
3.按要求輸出結果。
三、提示和說明
分別用兩種調度演算法對伍個進程進行調度。每個進程可有三種狀態;執行狀態(RUN)、就緒狀態(READY,包括等待狀態)和完成狀態(FINISH),並假定初始狀態為就緒狀態。
(一)進程式控制制塊結構如下:
NAME——進程標示符
PRIO/ROUND——進程優先數/進程每次輪轉的時間片數(設為常數2)
CPUTIME——進程累計佔用CPU的時間片數
NEEDTIME——進程到完成還需要的時間片數
STATE——進程狀態
NEXT——鏈指針
註:
1.為了便於處理,程序中進程的的運行時間以時間片為單位進行計算;
2.各進程的優先數或輪轉時間片數,以及進程運行時間片數的初值,均由用戶在程序運行時給定。
(二)進程的就緒態和等待態均為鏈表結構,共有四個指針如下:
RUN——當前運行進程指針
READY——就需隊列頭指針
TAIL——就需隊列尾指針
FINISH——完成隊列頭指針
(三)程序說明
1. 在優先數演算法中,進程優先數的初值設為:
50-NEEDTIME
每執行一次,優先數減1,CPU時間片數加1,進程還需要的時間片數減1。
在輪轉法中,採用固定時間片單位(兩個時間片為一個單位),進程每輪轉一次,CPU時間片數加2,進程還需要的時間片數減2,並退出CPU,排到就緒隊列尾,等待下一次調度。
2. 程序的模塊結構提示如下:
整個程序可由主程序和如下7個過程組成:
(1)INSERT1——在優先數演算法中,將尚未完成的PCB按優先數順序插入到就緒隊列中;
(2)INSERT2——在輪轉法中,將執行了一個時間片單位(為2),但尚未完成的進程的PCB,插到就緒隊列的隊尾;
(3)FIRSTIN——調度就緒隊列的第一個進程投入運行;
(4)PRINT——顯示每執行一次後所有進程的狀態及有關信息。
(5)CREATE——創建新進程,並將它的PCB插入就緒隊列;
(6)PRISCH——按優先數演算法調度進程;
(7)ROUNDSCH——按時間片輪轉法調度進程。
主程序定義PCB結構和其他有關變數。
(四)運行和顯示
程序開始運行後,首先提示:請用戶選擇演算法,輸入進程名和相應的NEEDTIME值。
每次顯示結果均為如下5個欄位:
name cputime needtime priority state
註:
1.在state欄位中,"R"代表執行態,"W"代表就緒(等待)態,"F"代表完成態。
2.應先顯示"R"態的,再顯示"W"態的,再顯示"F"態的。
3.在"W"態中,以優先數高低或輪轉順序排隊;在"F"態中,以完成先後順序排隊。

view plain to clipboardprint?
/*
操作系統實驗之時間片輪轉演算法和優先順序調度演算法
By Visual C++ 6.0
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node
{
char name[20]; /*進程的名字*/
int prio; /*進程的優先順序*/
int round; /*分配CPU的時間片*/
int cputime; /*CPU執行時間*/
int needtime; /*進程執行所需要的時間*/
char state; /*進程的狀態,W——就緒態,R——執行態,F——完成態*/
int count; /*記錄執行的次數*/
struct node *next; /*鏈表指針*/
}PCB;
PCB *ready=NULL,*run=NULL,*finish=NULL; /*定義三個隊列,就緒隊列,執行隊列和完成隊列*/
int num;
void GetFirst(); /*從就緒隊列取得第一個節點*/
void Output(); /*輸出隊列信息*/
void InsertPrio(PCB *in); /*創建優先順序隊列,規定優先數越小,優先順序越高*/
void InsertTime(PCB *in); /*時間片隊列*/
void InsertFinish(PCB *in); /*時間片隊列*/
void PrioCreate(); /*優先順序輸入函數*/
void TimeCreate(); /*時間片輸入函數*/
void Priority(); /*按照優先順序調度*/
void RoundRun(); /*時間片輪轉調度*/
int main(void)
{
char chose;
printf("請輸入要創建的進程數目:\n");
scanf("%d",&num);
getchar();
printf("輸入進程的調度方法:(P/R)\n");
scanf("%c",&chose);
switch(chose)
{
case 'P':
case 'p':
PrioCreate();
Priority();
break;
case 'R':
case 'r':
TimeCreate();
RoundRun();
break;
default:break;
}
Output();
return 0;
}
void GetFirst() /*取得第一個就緒隊列節點*/
{
run = ready;

if(ready!=NULL)
{
run ->state = 'R';
ready = ready ->next;
run ->next = NULL;
}
}
void Output() /*輸出隊列信息*/
{
PCB *p;
p = ready;
printf("進程名\t優先順序\t輪數\tcpu時間\t需要時間\t進程狀態\t計數器\n");
while(p!=NULL)
{
printf("%s\t%d\t%d\t%d\t%d\t\t%c\t\t%d\n",p->name,p->prio,p->round,p->cputime,p->needtime,p->state,p->count);
p = p->next;
}
p = finish;
while(p!=NULL)
{
printf("%s\t%d\t%d\t%d\t%d\t\t%c\t\t%d\n",p->name,p->prio,p->round,p->cputime,p->needtime,p->state,p->count);
p = p->next;
}
p = run;
while(p!=NULL)
{
printf("%s\t%d\t%d\t%d\t%d\t\t%c\t\t%d\n",p->name,p->prio,p->round,p->cputime,p->needtime,p->state,p->count);
p = p->next;
}
}
void InsertPrio(PCB *in) /*創建優先順序隊列,規定優先數越小,優先順序越低*/
{
PCB *fst,*nxt;
fst = nxt = ready;

if(ready == NULL) /*如果隊列為空,則為第一個元素*/
{
in->next = ready;
ready = in;
}
else /*查到合適的位置進行插入*/
{
if(in ->prio >= fst ->prio) /*比第一個還要大,則插入到隊頭*/
{
in->next = ready;
ready = in;
}
else
{
while(fst->next != NULL) /*移動指針查找第一個別它小的元素的位置進行插入*/
{
nxt = fst;
fst = fst->next;
}

if(fst ->next == NULL) /*已經搜索到隊尾,則其優先順序數最小,將其插入到隊尾即可*/
{
in ->next = fst ->next;
fst ->next = in;
}
else /*插入到隊列中*/
{
nxt = in;
in ->next = fst;
}
}
}
}
void InsertTime(PCB *in) /*將進程插入到就緒隊列尾部*/
{
PCB *fst;
fst = ready;

if(ready == NULL)
{
in->next = ready;
ready = in;
}
else
{
while(fst->next != NULL)
{
fst = fst->next;
}
in ->next = fst ->next;
fst ->next = in;
}
}
void InsertFinish(PCB *in) /*將進程插入到完成隊列尾部*/
{
PCB *fst;
fst = finish;

if(finish == NULL)
{
in->next = finish;
finish = in;
}
else
{
while(fst->next != NULL)
{
fst = fst->next;
}
in ->next = fst ->next;
fst ->next = in;
}
}
void PrioCreate() /*優先順序調度輸入函數*/
{
PCB *tmp;
int i;

printf("輸入進程名字和進程所需時間:\n");
for(i = 0;i < num; i++)
{
if((tmp = (PCB *)malloc(sizeof(PCB)))==NULL)
{
perror("malloc");
exit(1);
}
scanf("%s",tmp->name);
getchar(); /*吸收回車符號*/
scanf("%d",&(tmp->needtime));
tmp ->cputime = 0;
tmp ->state ='W';
tmp ->prio = 50 - tmp->needtime; /*設置其優先順序,需要的時間越多,優先順序越低*/
tmp ->round = 0;
tmp ->count = 0;
InsertPrio(tmp); /*按照優先順序從高到低,插入到就緒隊列*/
}
}
void TimeCreate() /*時間片輸入函數*/
{
PCB *tmp;
int i;

printf("輸入進程名字和進程時間片所需時間:\n");
for(i = 0;i < num; i++)
{
if((tmp = (PCB *)malloc(sizeof(PCB)))==NULL)
{
perror("malloc");
exit(1);
}
scanf("%s",tmp->name);
getchar();
scanf("%d",&(tmp->needtime));
tmp ->cputime = 0;
tmp ->state ='W';
tmp ->prio = 0;
tmp ->round = 2; /*假設每個進程所分配的時間片是2*/
tmp ->count = 0;
InsertTime(tmp);
}
}
void Priority() /*按照優先順序調度,每次執行一個時間片*/
{
int flag = 1;

GetFirst();
while(run != NULL) /*當就緒隊列不為空時,則調度進程如執行隊列執行*/
{
Output(); /*輸出每次調度過程中各個節點的狀態*/
while(flag)
{
run->prio -= 3; /*優先順序減去三*/
run->cputime++; /*CPU時間片加一*/
run->needtime--;/*進程執行完成的剩餘時間減一*/
if(run->needtime == 0)/*如果進程執行完畢,將進程狀態置為F,將其插入到完成隊列*/
{
run ->state = 'F';
run->count++; /*進程執行的次數加一*/
InsertFinish(run);
flag = 0;
}
else /*將進程狀態置為W,入就緒隊列*/
{
run->state = 'W';
run->count++; /*進程執行的次數加一*/
InsertTime(run);
flag = 0;
}
}
flag = 1;
GetFirst(); /*繼續取就緒隊列隊頭進程進入執行隊列*/
}
}
void RoundRun() /*時間片輪轉調度演算法*/
{

int flag = 1;

GetFirst();
while(run != NULL)
{
Output();
while(flag)
{
run->count++;
run->cputime++;
run->needtime--;
if(run->needtime == 0) /*進程執行完畢*/
{
run ->state = 'F';
InsertFinish(run);
flag = 0;
}
else if(run->count == run->round)/*時間片用完*/
{
run->state = 'W';
run->count = 0; /*計數器清零,為下次做准備*/
InsertTime(run);
flag = 0;
}
}
flag = 1;
GetFirst();
}

❸ 時間片輪轉調度演算法用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語言編寫並調試一個模擬的進程調度程序,採用「簡單時間片輪轉法」調度演算法對五個進程進行調度。

#include "stdio.h"
#include "stdlib.h"
#include "string.h"

struct PCB {
char NAME[10]; /*進程名*/
int ROUND; /*進程輪轉時間片*/
int REACHTIME; /*進程到達時間*/
int CPUTIME; /*進程佔用CPU時間*/
int COUNT; /*計數器*/
int NEEDTIME; /*進程完成還要的CPU時間*/
char STATE; /*進程的狀態*/
struct PCB *NEXT; /*鏈指針*/
};

struct LINK { /*PCB的鏈結構*/
struct PCB *RUN; /*當前運行進程指針*/
struct PCB *READY; /*就緒隊列頭指針*/
struct PCB *TAIL; /*就緒隊列尾指針*/
struct PCB *FINISH; /*完成隊列頭指針*/
};

void INIT(LINK *); /*對PCB的鏈結構初始化*/
void INSERT(LINK *); /*將執行了一個單位時間片數且還未完成的進程的PCB插到就緒隊列的隊尾*/
void FIRSTIN(LINK *); /*將就緒隊列中的第一個進程投入運行*/
void PRINT(LINK *); /*列印每執行一個時間片後的所有進程的狀態*/
void PR(PCB *); /*列印一個進程的狀態*/
int CREATE(LINK *,int); /*創建新的進程*/
void ROUNDSCH(LINK *); /*按時間片輪轉法調度進程*/

void main() {
LINK pcbs;
int i;
INIT(&pcbs);
i=0;
printf("創建5個進程\n\n");
while(i<5) {
if(CREATE(&pcbs,i+1)==1) {
printf("進程已創建\n\n");
i++;
}
else
printf("進程創建失敗\n\n");
}
FIRSTIN(&pcbs);
ROUNDSCH(&pcbs);
}

void ROUNDSCH(LINK *p) {
PCB *pcb;
while(p->RUN!=NULL) {
pcb=(PCB *)malloc(sizeof(PCB));
strcpy(pcb->NAME,p->RUN->NAME);
pcb->ROUND=p->RUN->ROUND;
pcb->REACHTIME=p->RUN->REACHTIME;
pcb->CPUTIME=p->RUN->CPUTIME;
pcb->COUNT=p->RUN->COUNT;
pcb->NEEDTIME=p->RUN->NEEDTIME;
pcb->STATE=p->RUN->STATE;
pcb->NEXT=p->RUN->NEXT;
pcb->CPUTIME++;
pcb->NEEDTIME--;
pcb->COUNT++;
if(pcb->NEEDTIME==0) {
pcb->NEXT=p->FINISH->NEXT;
p->FINISH->NEXT=pcb;
pcb->STATE='F';
p->RUN=NULL;
if(p->READY!=p->TAIL)
FIRSTIN(p);
}
else {
p->RUN=pcb;
if(pcb->COUNT==pcb->ROUND) {
pcb->COUNT=0;
if(p->READY!=p->TAIL) {
pcb->STATE='W';
INSERT(p);
FIRSTIN(p);
}
}
}
PRINT(p);
}
}

void INIT(LINK *p) {
p->RUN=NULL;
p->TAIL=p->READY=(PCB *)malloc(sizeof(PCB));
p->READY->NEXT=NULL;
p->FINISH=(PCB *)malloc(sizeof(PCB));
p->FINISH->NEXT=NULL;
}

int CREATE(LINK *p,int n) {
PCB *pcb,*q;
pcb=(PCB *)malloc(sizeof(PCB));
flushall();
printf("請輸入第%d個進程的名稱:\n",n);
gets(pcb->NAME);
printf("請輸入第%d個進程的輪轉時間片數:\n",n);
scanf("%d",&(pcb->ROUND));
printf("請輸入第%d個進程的到達時間:\n",n);
scanf("%d",&(pcb->REACHTIME));
pcb->CPUTIME=0;
pcb->COUNT=0;
printf("請輸入第%d個進程需運行的時間片數:\n",n);
scanf("%d",&(pcb->NEEDTIME));
pcb->STATE='W';
pcb->NEXT=NULL;
if(strcmp(pcb->NAME,"")==0||pcb->ROUND<=0||pcb->NEEDTIME<=0) /*輸入錯誤*/
return 0;
q=p->READY;
while(q->NEXT!=NULL&&q->NEXT->REACHTIME<=pcb->REACHTIME)
q=q->NEXT;
pcb->NEXT=q->NEXT;
q->NEXT=pcb;
if(pcb->NEXT==NULL)
p->TAIL=pcb;
return 1;
}

void FIRSTIN(LINK *p) {
PCB *q;
q=p->READY->NEXT;
p->READY->NEXT=q->NEXT;
q->NEXT=NULL;
if(p->READY->NEXT==NULL)
p->TAIL=p->READY;
q->STATE='R';
p->RUN=q;
}

void INSERT(LINK *p) {
PCB *pcb;
pcb=(PCB *)malloc(sizeof(PCB));
strcpy(pcb->NAME,p->RUN->NAME);
pcb->ROUND=p->RUN->ROUND;
pcb->REACHTIME=p->RUN->REACHTIME;
pcb->CPUTIME=p->RUN->CPUTIME;
pcb->COUNT=p->RUN->COUNT;
pcb->NEEDTIME=p->RUN->NEEDTIME;
pcb->STATE=p->RUN->STATE;
pcb->NEXT=p->RUN->NEXT;
p->TAIL->NEXT=pcb;
p->TAIL=pcb;
p->RUN=NULL;
pcb->STATE='W';
}

void PRINT(LINK *p) {
PCB *pcb;
printf("執行一個時間片後的所有進程的狀態:\n\n");
if(p->RUN!=NULL)
PR(p->RUN);
if(p->READY!=p->TAIL) {
pcb=p->READY->NEXT;
while(pcb!=NULL) {
PR(pcb);
pcb=pcb->NEXT;
}
}
pcb=p->FINISH->NEXT;
while(pcb!=NULL) {
PR(pcb);
pcb=pcb->NEXT;
}
}

void PR(PCB *p) {
printf("進程名:%s\n",p->NAME);
printf("進程輪轉時間片:%d\n",p->ROUND);
printf("進程到達時間:%d\n",p->REACHTIME);
printf("進程佔用CPU時間:%d\n",p->CPUTIME);
printf("計數器:%d\n",p->COUNT);
printf("進程完成還要的CPU時間:%d\n",p->NEEDTIME);
printf("進程的狀態:%c\n\n",p->STATE);
}

❺ 程序調度(時間片輪轉演算法)用C語言程序怎麼寫啊謝謝

#include<stdio.h>
struct pcb
{
char name;
int time;
};
void main()
{
int n,i,j,flag=1;
struct pcb a[100];
printf("輸入程序個數:");
scanf("%d",&n);
getchar();/*接收回車*/
for(i=0;i<n;i++)
{
printf("輸入程序的名字:如A B C...\n");
scanf("%c",&a[i].name);
getchar();/*接收回車*/
printf("輸入佔用的時間片:");
scanf("%d",&a[i].time);
getchar();/*接收回車*/
}
i=0;
while(flag && n>0)
{
if(a[i].time!=0)
{
printf("%c",a[i].name);
a[i].time--;
}
for(j=0;j<n;j++)
if(a[j].time)
{
flag=1;
break;
}
else
flag=0;
i=(++i)%n;

}
}

❻ 求一個時間片進程調度演算法!望高手指點!

只幫你寫點開頭,後面你應該會的
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream.h>

typedef struct node
{
char name[10];
int prio;
int round;
int cputime;
int needtime;
int count;
char state;
struct node *next;
}PCB;
PCB *finish,*ready,*tail,*run,; //隊列指針
int N; //進程數

void zhunbei()
{
run=ready; //就緒隊列頭指針賦值給運行頭指針
run->state='G'; //進程狀態變為運行態]
ready=ready->next; //就緒隊列頭指針後移到下一進程
}
//輸出標題函數
void output1(char a)
{
if(toupper(a)=='P') //優先順序法
cout<<" "<<endl;
cout<<"進程名 佔用CPU時間 已運行時間 還要的時間 輪轉時間片 狀態"<<endl;
}

❼ 如何用C語言編寫:設計一個時間片輪轉調度演算法實現處理機調度的程序

實驗三 進程調度

一、實驗目的
在採用多道程序設計的系統中,往往有若干個進程同時處於就緒狀態。當就緒進程個數大於處理機數時,就必須依照某種策略來決定那些進程優先佔用處理機。本實驗模擬在單處理機情況下的處理機調度,幫助學生加深了解處理機調度的工作。

二、實驗內容
設計一個時間片輪轉調度演算法實現處理機調度的程序。

三、實驗指導
1.實驗中使用的數據結構:
1)PCB進程式控制制塊
其中包括參數①進程名name;②要求運行時間runtime;③優先數prior;④狀態state;⑤已運行時間runedtime。
2)為簡單起見,只設運行隊列,就緒鏈表兩種數據結構,進程的調度在這兩個隊列中切換,如圖3.1所示。

圖3.1PCB鏈表

2.運行結果,包括各個進程的運行順序,每次佔用處理機的運行時間

3.每個進程運行時間隨機產生,為1~20之間的整數。
4.時間片的大小由實驗者自己定義,可為3或5。

四、實驗要求
1.在上機前寫出全部源程序;
2.能在機器上正確運行程序。
五、程序清單
六、運行結果
七、調試分析及實驗心得
我的回答和這位老兄的差不多

❽ c語言,單處理機進程調度,時間片輪轉


//參考一下
#include<stdio.h>
#include<stdlib.h>

#defineCPU_TIME50//CPU時間片

structmission//單個任務的結構體
{
charname[20];//任務名稱
intfinished;//任務是否已完成,完成為1,未完成為0
intneed_time;//任務總共需要的CPU時間
intfinished_time;//任務已經執行的CPU時間
};

intwork_mission=5;//未完成任務計數

structmissions[5]={{"one",0,100,0},//假設有5個任務
{"two",0,380,0},
{"three",0,200,0},
{"four",0,440,0},
{"five",0,230,0}
};

intmove_mission(intflag)//任務排序
{
if(work_mission==0)
{
return1;
}
structmissiontemp;
temp=s[0];
inti;

if(flag==1)
{
for(i=1;i<work_mission+1;i++)
{
s[i-1]=s[i];
}
s[work_mission]=temp;
}
else
{
for(i=1;i<work_mission;i++)
{
s[i-1]=s[i];
}
s[work_mission-1]=temp;
}


return0;
}
intmain(intargc,char*argv[]){
structmissiontemp;
intfinished=0;
intcpu_time_count=0;
while(finished==0)
{
printf(" 第%d個CPU時間片即將執行 ",cpu_time_count/CPU_TIME+1);
printf(" 當前任務隊列(即將執行下面第一行任務,執行後將任務移到隊列末尾):CPU已用時:%d ",cpu_time_count);
inti;
for(i=0;i<5;i++)
{
printf("任務名稱:%5s是否已完成:%d需時間:%03d已執行時間:%03d已完成:%03.2f%% ",s[i].name,s[i].finished,s[i].need_time,s[i].finished_time,100*((float)s[i].finished_time/(float)s[i].need_time));
}
if(s[0].finished==1)
{
finished=1;
break;
}
if((s[0].need_time-s[0].finished_time)>CPU_TIME)
{
s[0].finished_time+=CPU_TIME;
cpu_time_count+=CPU_TIME;
finished=move_mission(0);
}
else
{
cpu_time_count+=(s[0].need_time-s[0].finished_time);
s[0].finished_time=s[0].need_time;
s[0].finished=1;
work_mission-=1;
finished=move_mission(1);
}
}
printf(" 當前任務隊列(全部執行完畢):CPU用時:%d ",cpu_time_count);
inti;
for(i=0;i<5;i++)
{
printf("任務名稱:%5s是否已完成:%d需時間:%03d已執行時間:%03d已完成:%03.2f%% ",s[i].name,s[i].finished,s[i].need_time,s[i].finished_time,100*((float)s[i].finished_time/(float)s[i].need_time));
}
printf(" 總共用了CPU時間:%d",cpu_time_count);
return0;
}

閱讀全文

與c語言時間片演算法相關的資料

熱點內容
如何連接到伺服器上的資料庫 瀏覽:869
文件夾文字轉文本代碼 瀏覽:104
python代碼字典統計排序1 瀏覽:505
氣場pdf 瀏覽:580
電腦cmd和python的差別 瀏覽:828
小程序源碼價格 瀏覽:132
伺服器合並後迴流會怎麼樣 瀏覽:825
程序員如何調試代碼 瀏覽:725
PHP實例方法 瀏覽:822
南昌朝陽解壓娛樂 瀏覽:847
linux所有命令無效 瀏覽:280
如何更新伺服器2016密碼 瀏覽:291
guidjava生成 瀏覽:904
vs編譯出來的文件在哪裡 瀏覽:525
類似程序員一樣思維的人 瀏覽:606
解壓縮與命令行解壓縮 瀏覽:189
電腦常用的命令有哪幾個 瀏覽:554
古典針灸入門pdf 瀏覽:885
構建高可用linux伺服器第2版 瀏覽:344
單片機控制機械 瀏覽:443