#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define IN 1 /* 进入就绪队列 */
#define OUT 0 /* 出就绪队列 */
/*进程是程序在处理机上执行过程,进程存在的标识是进程控制块(PCB),所以应首先设计
进程控制块的结构(struct)*/
typedef struct {
char name[10];/* 进程标识符 */
int prio; /* 进程优先数 */
int round; /* 进程时间轮转片 */
int cputime; /* 进程应用CPU的时间 */
int needtime;/* 进程运行需要的时间 */
int count; /* 计数器 */
char state; /* 进程的状态 */
struct node *next;/* 链指针 */
}PCB;
void firstin(void);
void prt1(char a);
void prt2(char a,PCB *q);
void prt(char algo);
void insert1(PCB *q);
void insert2(PCB *p2);
void creat1(char alg);
void creat2(char alg);
void priority(char alg);/* 优先调度算法的执行函数 */
void roundrun(char alg);/* 时间片轮转算法的执行函数 */
int N; /* 进程数,设为全局变量 */
PCB *finish,*ready,*tail,*run;/* finish,ready,run分别为完成队列指针,就绪队列指针 */
/* tail为循环轮转中就绪队列的指针 */
void 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')
{
creat1(algo);
priority(algo);
}
else
if(algo=='R'||algo=='r')
{
creat2(algo);
roundrun(algo);
}
getch();
}
/*将就绪队列的第一个进程投入运行*/
void 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 roundrun 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%c\n",q->name,q->cputime,
q->needtime,q->count,q->state);
}
void prt(char algo)
{
PCB *p;
prt1(algo);
if(run!=NULL)
prt2(algo,run);/* 输出当前正在运行的PCB */
p=ready; /* 输出就绪队列的指针 */
while(p!=NULL)
{
prt2(algo,p);
p=p->next;
}
p=finish;
while(p!=NULL)
{
prt2(algo,p);
p=p->next;
}
getch(); /* 按任意键继续 */
}
/*优先插入算法*/
void insert1(PCB *q)
{
PCB *p1,*s,*r;
int state;
s=q; /*待插入PCB指针*/
p1=ready; /*就绪队列的头指针*/
r=p1; /*r为p1的前驱指针*/
state=IN;
while((p1!=NULL)&&state)
if(p1->prio>s->prio)
{
r=p1;
p1=p1->next;
}
else
state=OUT;
if(r!=p1)
{
r->next=s;
s->next=p1;
}
else
{
s->next=p1;
ready=s;
}
}
/*轮转法插入函数*/
void insert2(PCB *p2)
{
tail->next=p2; /*将新的PCB插入到当前就绪队列的尾*/
tail=p2;
p2->next=NULL;
}
/*优先创建PCB的信息*/
void creat1(char alg)
{
PCB *p;
int i,time;
char na[10];
ready=finish=run=NULL;
printf("Enter name and time of 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->state='w';
p->prio=50-time;
if(ready!=NULL) /*就绪队列不空,调用插入函数插入*/
insert1(p);
else
{
p->next=ready;
ready=p;
}
}
clrscr();
printf(" Output of priority\n");
printf("***********************\n");
prt(alg); /*输出进程PCB的信息*/
run=ready; /*将就绪队列第一个进程投入使用*/
ready=ready->next;
run->state='R';
}
/*轮转法创建进程*/
void creat2(char alg)
{ PCB *p;
int i,time;
char na[10];
ready=finish=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);
run=ready;
ready=ready->next;
run->state='R';
}
/*优先数调度算法*/
void priority(char alg)
{
while(run!=NULL) /*当运行进程不空时,有进程正在运行*/
{
run->cputime+=1;
run->needtime-=1;
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);
}
}
void roundrun(char alg)
{
while(run!=NULL)
{
run->cputime+=1;
run->needtime-=1;
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;
if(ready!=NULL)
{
run->state='w';
insert2(run);
firstin();
}
}
prt(alg);
}
}
进程调度模仿程序.rar_操作系统 进程调度_进程调度
版权申诉
151 浏览量
2022-09-23
22:38:11
上传
评论
收藏 11KB RAR 举报
林当时
- 粉丝: 100
- 资源: 1万+
评论0