?? 進程調(diào)度實驗.txt
字號:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct pcb/*定義結(jié)構(gòu)*/
{ char name[5];
struct pcb *next;
int needtime;
int priority;
char state[5];
}NODE;
NODE *create_process(int n)/*創(chuàng)建隊列*/
{ NODE *head,*s,*t;
int time,i,j;
char pname[5];
head=(NODE *)malloc(sizeof(NODE));
printf("please input process name:");
scanf("%s",pname);
strcpy(head->name,pname);
printf("please input need time:");
scanf("%d",&time);
head->needtime=time;
printf("please input priority:");
scanf("%d",&j);
head->priority=j;
strcpy(head->state,"ready");
head->next=NULL;
t=head;
for(i=1;i<n;i++)
{ s=(NODE *)malloc(sizeof(NODE));
printf("please input process name:");
getchar();
gets(pname);
strcpy(s->name,pname);
printf("please input need time:");
scanf("%d",&time);
s->needtime=time;
printf("please input priority:");
scanf("%d",&j);
s->priority=j;
strcpy(s->state,"ready");
s->next=NULL;
t->next=s;
t=s;
}
return head;
}
void pri_process(NODE *p)/*輸出進程隊列*/
{
NODE *q;
q=p->next;
printf("\n name \t needtime \t priority \t state\n");
while(q!=NULL)
{ printf(" %5s\t %2d \t %2d \t %5s \n",
q->name,q->needtime,q->priority,q->state); /* \t為水平制表*/
q=q->next;
}
}
NODE *order(NODE *head_sort)/*對進程的優(yōu)先級進行排序*/
{
NODE *p,*q,*head,*r,*t;
int m,pr;
char name[5];
head=head_sort;
p=head->next;
r=p;
t=p;
q=p->next;
while(r!=NULL)
{
while(q!=NULL)
{
if(p->priority<q->priority)
{
m=p->priority;
p->priority=q->priority;
q->priority=m;
strcpy(name,p->name);
strcpy(p->name,q->name);
strcpy(q->name,name);
pr=p->needtime;
p->needtime=q->needtime;
q->needtime=pr;
}
p=q;
q=q->next;
}
r=r->next;
p=t;
q=p->next;
}
return(head_sort);
}
void main()/*主程序*/
{ NODE *p,*head,*m;
int x;
printf("please input process number:");
scanf("%d",&x);
p=create_process(x);
head=(NODE *)malloc(sizeof(NODE));
head->next=p;
pri_process(head);
while(x>0)
{
order(head);
m=head->next;
strcpy(m->state,"run");
m->priority--;
m->needtime--;
if(head->next!=NULL)
pri_process(head);
if(m->needtime==0)
{
head->next=m->next;
printf("%s has finished\n",m->name);
free(m);
x--;
}
}
printf("over! \n");
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -