标题:
时间片轮转法与优先调度算法,进程调度,C代码
[打印本页]
作者:
yuyang911220
时间:
2015-5-28 08:49
标题:
时间片轮转法与优先调度算法,进程调度,C代码
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "ctype.h"
#include "conio.h"
#include "malloc.h"
typedef struct node
{
charname[10];
intprio;
intround;
int cputime;
int needtime;
intcount;
char state;
struct node *next;
}PCB;
PCB *finish,*ready,*tail,*run;
int N;
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 count round state\n");
}
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);
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 b;
s=q;
p1=ready;
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->next=s;
s->next=p1;
}
else
{
s->next=p1;
ready=s;
}
}
void insert2(PCB *p2)
{
tail->next=p2;
tail=p2;
p2->next=NULL;
}
void create1(char alg)
{
PCB *p;
int i,time;
char na[10];
ready=NULL;
finish=NULL;
run=NULL;
printf("Enter name and time ofprocess\n");
for(i=1;i<=N;i++)
{
p=(PCB *)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;
}
}
//clr11();
printf(" output of priority:\n");
printf("************************************************\n");
prt(alg);
run=ready;
ready=ready->next;
run->state='R';
}
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/)
Powered by Discuz! 7.0.0