⾮抢占式优先算法例题_⾮抢占短作业优先算法源代码(C语
⾔)
#include
#include
#define MAX 5 //进程数
/*短作业优先算法*/
struct pro
{
int num; //进程名
int arriveTime; //到达时间
int burst; //运⾏时间;
struct pro *next;
};
//函数声明
struct pro* creatList();
void insert(struct pro *head,struct pro *s);
struct pro* searchByAT(struct pro *head,int AT);
void run(struct pro *head);
void del(struct pro* p);
int getCount(struct pro *head,int time);
struct pro* creatList() //创建链表,按照进程的到达时间排列
{
struct pro* head=(struct pro*)malloc(sizeof(struct pro));
head->next=NULL;
struct pro* s;
int i;
for(i=0;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));
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;
c语言listinsert函数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) //察看当前就绪队列中的进程数
{
int count=0;
struct pro *p,*q;
p=head;
q=p->next;
while(q!=NULL&&q->arriveTime<=time)
{
count++;
p=q;
q=q->next;
}
return count;
}
struct pro* SJF(struct pro *head,int count) //在头节点后的count个节点中选择burst最⼩的,返回其前⼀个节点的指针{
int min;
struct pro *p,*q,*flag;
p=head;
q=p->next;
min=q->burst;
flag=p; //flag记录返回指针
while(count>0)
{
if(q->burst
{
min=q->burst;
flag=p;
}
count--;
p=q;
q=q->next;
}
return flag;
}
void run(struct pro *head) //按短作业优先算法调度进程,并输出其运⾏情况{
int time=0,count;
struct pro *s,*t;
while(head->next!=NULL)
{
count=getCount(head,time);
if(count==0) //如果当前就绪队列中没有进程,时间⾃增
time++;
else if(count==1) //如果就绪队列中只有1个进程,则必定是第⼀个节点
{
t=head;
s=t->next;
printf("进程名:%d\n",s->num);
printf("到达时间:%d\n",s->arriveTime);
printf("运⾏开始时间:%d\n",time);
printf("响应时间:%d\n",time-s->arriveTime);
time+=s->burst;
printf("