C语言编程实现时间片轮转算法,尽量写得简单易懂,谢谢

Python018

C语言编程实现时间片轮转算法,尽量写得简单易懂,谢谢,第1张

#include<stdlib.h>#define MAX 5//进程数量#define RR 2//时间片大小/*时间片轮转算法*/struct pro{int numint arriveTimeint burstint 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* sint iTOTALTIME=0for(i=0i<MAXi++){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的初始值为1s->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->nextp->next=sreturn}struct pro* searchByAT(struct pro *head,int AT)//查找第一个到达时间大于等于AT的节点,返回其前一个指针{struct pro *p,*qp=headq=head->nextwhile(q!=NULL&&q->arriveTime<=AT){p=q q=q->next}return p}void del(struct pro* p)//删除p的下一个节点{struct pro *tmptmp=p->nextp->next=tmp->nextfree(tmp)return}int getCount(struct pro *head,int time)//察看在time之前到达但未移动到运行队列的进程数量{int count=0struct pro *s,*ts=headt=s->nextwhile(t!=NULL&&t->arriveTime<=time){s=t t=t->nextcount++ //count记录当前时刻到达的进程数}return count}struct pro* searchEnd(struct pro *head)//查找并返回循坏队列的尾节点的前一个节点{struct pro *p,*qp=headq=head->nextwhile(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,*ts=headFt=s->nextr=t //r记录要移动的第一个节点while(n>1){t=t->next n--}s->next=t->next //以上完成从原队列中摘除相关节点,r,t分别为第一个和最后一个节点 s=searchEnd(headT)t->next=s->nexts->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,*qp=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=qq=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)}

#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=0i<ni++)

{

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=0j<nj++)

if(a[j].time)

{

flag=1

break

}

else

flag=0

i=(++i)%n

}

}

调试通过...

#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=1i<=Ni++)

{

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=1i<=Ni++)

{

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)

}

}