#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;
}