进程调度算法c语言

#include
#include
#include
#include
#include
#define LEN sizeof(struct pcb)
#define null 0
static int x=0,h=1;
struct pcb
{ char name[10];
int artime;//进程到达时间
int netime;//进程需要时间片的个数 随机函数产生
int priority;//进程优先级 随机函数产生
int ustime;//进程已使用时间
char state;//进程状态,包括R=run W=wait F=finish
struct pcb *next;
}pcb,ready[10];
struct pcb *creat(k)//测试通过
{
int a,t=0;
struct pcb *head,*p1,*p2;
srand(time(0));
p1=head=(struct pcb *)malloc(LEN);
p1->netime=1+(int)(20.0*rand()/(RAND_MAX+1.0));
p1->priority=1+(int)(32.0*rand()/(RAND_MAX+1.0));
p1->artime=t++;
p1->ustime=0;
p1->state='W';
printf("进程赋值:\n");
scanf("%s",&(*p1).name);printf("\n");
printf("进程名称:%s\n",&(*p1).name);
printf("进程到达时间:%d\n",p1->artime);
printf("进程需要时间片:%d\n",p1->netime);
printf("进程优先级:%d\n",p1->priority);
printf("进程已用时间:%d\n",p1->ustime);
printf("进程状态:%c\n",'W');
p1->next=NULL;
for(a=1;a{p2=(struct pcb *)malloc(LEN);
p2->netime=1+(int)(20.0*rand()/(RAND_MAX+1.0));
p2->priority=1+(int)(32.0*rand()/(RAND_MAX+1.0));
p2->artime=t++;
p2->ustime=0;
p2->state='W';
scanf("%s",&(*p2).name);printf("\n");
printf("进程名称:%s\n",&(*p2).name);
printf("进程到达时间:%d\n",p2->artime);
printf("进程需要时间片:%d\n",p2->netime);
printf("进程优先级:%d\n",p2->priority);
printf("进程已用时间:%d\n",p2->ustime);
printf("进程状态:%c\n",'W');
p2->next=NULL;
p1->next=p2;
p1=p1->next;
}
return head;
}
struct pcb *join(struct pcb *head,int n,int time)//…………测试通过
{ int i,j;
struct pcb *p;//p表示进程的指针,q表示就绪态的指针
p=head;//将链表的头指针赋给p
for(i=0,j=0;i{ if(p->artime{ ready[j++]=*p;//将进程赋给ready[0],进程数k加1
p=p->next;//p向后移一位
}
else
p=p->next;
}
x=j;
return ready;
}
void swap(struct pcb *m,struct pcb *n)
{
struct pcb tmp;
tmp=*m;
*m=*n;
*n=tmp;
}
void *sort(struct pcb *head,int k)//sort函数是为了实现就绪态进程数组中的进程排序
{ struct pcb *p;
int m,n;
p=head;
if(k>1)
{ for(m=0;m{for(n=0;n{if(p[n].priority>p[n+1].priority)//优先级越小越先执行
{swap(p+n,p+n+1);
}
}
}
head=p;
}
return head;
}
void print(struct pcb *head,int number,int q)//head为ready的头指针,number是就绪态进程的个数……测试通过
{ int i; //printf函数只需输出一次就绪态或执行后的进程队伍就可以了,
struct pcb *p; //不需要执

行所有的时间片,把时间片初和末轮转和时间片轮转都放到main函数里面
p=head;
for(i=0;i{ if(h%2==1)
{printf("第%2d时间片初",q);
}
else
printf("第%2d时间片末",q);

printf("%7s",&(*p).name);
printf("%8d",p->artime);
printf("%11d",p->netime);
printf("%10d",p->priority);
printf("%10d",p->ustime);
printf("%9c",p->state);
p++;
printf("\n");
}
printf(" ---------------------------------------------------------\n");
h=h+1;
}
void run(struct pcb *p,struct pcb *head)//进程执行优先级+1已用时间+1
{ struct pcb *q;
q=head;
p->priority=p->priority+1;
p->ustime=p->ustime+1;
for(;q!=null;)
{ if(strcmp((*p).name,(*q).name)==0)
{q->priority=q->priority+1;
q->ustime=q->ustime+1;
break;
}
else
{q=q->next;
}
}
}
void *delete(struct pcb *n,struct pcb *head,int number)
{ int a;
struct pcb *p;
p=head;
for(a=0;a{ if(strcmp((*p).name,(*n).name)==0)//头结点是要删除的结点
{head=p->next;
break;
}
else
{if(strcmp((p->next)->name,(*n).name)==0)
{p->next=p->next->next;
break;
}
else
{p=p->next;
a=a+1;
}
}
}
return head;
}


main()
{ int i,j;
struct pcb *head,*ready0;
printf("请输入进程个数:");
scanf("i=%d",&i);
printf("%d",i);
head=creat(i);
printf(" 就绪进队伍打印表 \n");
printf(" 时间片 名称 到达时间 需要时间 优先级 已用时间 状态\n");
for(j=1;i>0;j++)
{ready0=join(head,i,j);//认为j代表的是时间片个数,i代表的是进程个数,进程加入就绪态
ready0=sort(ready0,x);//进程排序,优先级小的放在前面
print(ready0,x,j);//打印就绪态队伍
run(ready0,head);//调入执行态
if(ready0->ustime==ready0->netime)
{head=delete(ready0,head,i);//传回来的是进程链表的头指针,其中已经删除了达到条件的结点
i=i-1;//修改进程个数
x=0;//重置x的值
ready0=join(head,i,j);//重新加入重新排序
ready0=sort(ready0,x);
}
else
{ready0=sort(ready0,x);//再次排序就可以了
}
print(ready0,x,j);//打印出执行后的进程队伍
printf("************************************************************************\n");

}
}

相关文档
最新文档