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语言时间片优先算法

求进程调度先来先服务算法,短进程优先算法完整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;iMAXPCB;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;

cinfname;

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”进程流文件中的进程总数=”quantiryendl;

cout”进程名 所需时间 优先数”endl;

for (i=0;iquantiry;i++)

{

cout” “pcbs[i].name” “pcbs[i].time” “pcbs[i].privilegeendl;

}

return 1;

}

return 0;

}

//重置数据,以供另一个算法使用

void init()

{

int i;

for (i=0;iMAXPCB;i++)

{

pcbs[i].finished=0;

pcbs[i].wait_time=0;

}

}

void FIFO()

{

int i,j;

int total;

//输出FIFO算法执行流

coutendl”—————————————————————“endl;

cout”FIFO算法执行流:”endl;

cout”进程名 等待时间”endl;

for (i=0;iquantiry;i++)

{

cout” “pcbs[i].name” “pcbs[i].wait_timeendl;

for (j=i+1;jquantiry;j++)

{

pcbs[j].wait_time+=pcbs[i].time;

}

}

total=0;

for (i=0;iquantiry;i++)

{

total+=pcbs[i].wait_time;

}

cout”总等待时间:”total” “”平均等待时间:”total/quantiryendl;

}

//优先度调度算法

void privilege()

{

int i,j,p;

int passed_time=0;

int total;

int queue[MAXPCB];

int current_privielege=1000;

for (i=0;iquantiry;i++)

{

current_privielege=1000;

for (j=0;jquantiry;j++)

{

if ((pcbs[j].finished==0)(pcbs[j].privilegecurrent_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;

}

//输出优先数调度执行流

coutendl”—————————————–“endl;

cout”优先数调度执行流:”endl;

cout”进程名 等待时间”endl;

for (i=0;iquantiry;i++)

{

cout” “pcbs[queue[i]].name” “pcbs[queue[i]].wait_time”–“queue[i]endl;

}

total=0;

for (i=0;iquantiry;i++)

{

total+=pcbs[i].wait_time;

}

cout”总等待时间:”total” 平均等待时间:”total/quantiryendl;

}

//时间片轮转调度算法

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;iquantiry;i++)

{

if (pcbs[i].finished==0)

{

flag=1;

queue[total]=i;

total++;

if (pcbs[i].time=Quatum*(round+1))

pcbs[i].finished=1;

}

}

round++;

}

coutendl”—————————————————————“endl;

cout”时间片轮转调度执行流:”;

for(i=0;itotal;i++)

{

coutpcbs[queue[i]].name” “;

}

coutendl;

cout”进程名 结束时间 运行时间 等待时间”endl;

sum=0;

for (i=0;iquantiry;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” “;

coutpcbs[i].time” “(j+1)*Quatum-pcbs[i].timeendl;

sum+=(j+1)*Quatum-pcbs[i].time;

break;

}

}

}

cout”总等待时间:”sum” “”平均等待时间:”sum/quantiryendl;

}

//显示版权信息函数

void version()

{

coutendlendl;

cout” ┏━━━━━━━━━━━━━━━━━━━━━━━┓”endl;

cout” ┃ 进程调度模拟系统   ┃”endl;

cout” ┠───────────────────────┨”endl;

cout” ┃   version 2011 ┃”endl;

cout” ┗━━━━━━━━━━━━━━━━━━━━━━━┛”endl;

coutendlendl;

}

//主函数

int main()

{

int flag;

version();

initial();

flag=readData();

if(flag==1){

FIFO();

init();

privilege();

init();

timer();

}

coutendl;

system(“pause”);

return 0;

}

如何用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”

#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-prioready-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);

}

}

本文来自投稿,不代表【】观点,发布者:【

本文地址: ,如若转载,请注明出处!

举报投诉邮箱:253000106@qq.com

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2024年3月25日 19:03:35
下一篇 2024年3月25日 19:12:02

相关推荐

  • c语言改写模式,c语言实现修改功能

    c语言程序修改? 1、这个程序有4个错误,我都加粗了,第一个是m没有赋初值,第二个是while表达式中的ch=getchar()需要括号括起来,第三个是m=m*10+ch-0中的0也需要用单引号括起来,第四个是第2个while中为m!=0。 2、define容易造成误会,因为不符合一般的编程习惯,false 0, true 1;scanf放在你的那个地方是达…

    2024年5月23日
    3900
  • c语言控制代码的换码序列,c语言交换代码

    求C语言编程大神解答一下下面这个编程代码? k==5,用5去除125余0,所以r=125%5中r为0。由于!0为1,所以执行while循环体:先打印出5(k的值),再n=n/k==125/5=25;由于251则再打印出*号。这一循环结果输出是5*。 下面是我的代码,三个函数分别对应三个问题。 在实现基本要求的前提下,拓展了可以从键盘输入的功能,以下为各题代码…

    2024年5月23日
    5600
  • java对数组计算,java数组计算时间复杂度

    Java中数组运算 1、很简单,因为你的数组是局部变量,只在那个声明它的方法里能用,解决办法是: 把它设置为属性; 作为方法的返回值。 2、将它们相加,并将结果存储到result数组中相应的位置。- 最后返回result数组即可。需要注意的是,上面的代码中我们使用了三元运算符 ? : 来判断数组的下标是否越界,如果越界则将值设为0。 3、public sta…

    2024年5月23日
    5000
  • c语言扫描io脚状态,c语言端口扫描

    求51单片机的上升沿和下降沿C语言检测程序列子,端口就是普通IO口。 上升沿触发是当信号有上升沿时的开关动作,当电位由低变高而触发输出变化的就叫上升沿触发。也就是当测到的信号电位是从低到高也就是上升时就触发,叫做上升沿触发。 单片机怎么计算1s内下降沿的个数的C语言程序或者计算两个下降沿的时间(检测脉冲频率)计算1s内下降沿的个数方法是,一个定时器设置定时1…

    2024年5月23日
    4400
  • c语言mallloc使用的简单介绍

    C语言中使用malloc必须加#includemallo.h? 1、在C语言中使用malloc函数进行动态内存分配。malloc的全称是memory allocation,中文叫动态内存分配。原型:extern void malloc(unsigned int num_bytes);功能:分配长度为num_bytes字节的内存块。 2、你可以看一下C语言那本…

    2024年5月23日
    4400
  • c语言三位小数,C语言三位小数

    怎样用C++语言输出精确到小数点后三位的数? 1、用C++语言输出精确到小数点后三位的数,可以参考下面给出的代码:coutsetiosflags(ios:fixed)setprecision(3)。其中 setiosflags中set是设置的意思。ios是iostream的缩写,即输入输出流。flags是标志的意思。 2、要精确到小数点后若干位,则数据类型为…

    2024年5月23日
    7200
  • c语言21点游戏,二十一点游戏代码c语言

    如何使用C语言编写简单小游戏? 1、数学知识:长方形的面积S=a*b 长方形周长L=2*(a+b)其中a b分别为长方形的宽和高。算法分析:长方形面积及周长均依赖于宽和高,所以先要输入宽高值,然后根据公式计算,输出结果即可。 2、/*也不知道你是什么级别的,我是一个新手,刚接触编程语言,以下是我自己变得一个小程序,在所有c语言的编译器(vc++0、turbo…

    2024年5月23日
    6300
  • c语言当中的null,C语言当中的符号

    C/C++中,NULL和null的区别是什么? nul 和 null要看编译器,不同的编译器有所区别。 所以C或者C++中都使用一个特殊定义NULL表示无效值,其本质就是未定义具体数据类型的0值。 null是是什么都没有的意思。在java中表示空对象。 本意是“空的;元素只有零的”意思。计算机中通常表示空值,无结果,或是空集合。\x0d\x0a在ASCII码…

    2024年5月23日
    4500
  • 包含c语言对txt文件命名的词条

    如何在C语言编程里面修改源文件名字 如果你是在WINDOWS的话,简单了,随便用个编辑器,比如记事本,然后写c源程序,保存到你想要保存的位置。如果你在DOS下,可以用edit,写好以后,按alt键,选择文件菜单,然后保存。 用open打开文件,注意操作模式使用“修改”或者“添加” 用write或者fprintf向文件中写入你的内容。 用close关闭文件。 …

    2024年5月23日
    4800
  • 学c语言编程,学c语言编程用什么软件

    编程开发必须要学C语言吗? 1、要学习。编程开发的学习内容主要包括c语言、python和c+语言。C语言作为一种简单灵活的高级编程语言,它是一个面向过程的语言,一般是作为计算机专业的基础入门语言课程。 2、C语言。对于刚接触编程的人来说,先学习C语言是非常重要的。C语言可以说是是计算机编程语言的鼻祖,其他的编程语言几乎全是由C语言变化衍生出来的。 3、不需要…

    2024年5月23日
    3400

发表回复

登录后才能评论



关注微信