操作系统-进程管理实验C语言

操作系统-进程管理实验C语言


2024年4月21日发(作者:)

#include"stdio.h"

#include"stdlib.h"

#define ready 1

#define run 2

struct pcb

{

char name[10];

int priority; /*进程的优先级*/

int state; /*进程的状态:可以有run、ready、finish(可有可无)*/

int needtime; /*进程需要运行的时间*/

int runtime;

int time; /*进程已运行的时间*/

struct pcb *next; /*指向下一个进程PCB的指针*/

};

typedef struct pcb PCB;

PCB *head=NULL;

/*此函数用于创建进程队列*/

void create(void)

{ PCB *p, *q;

int n,i;

printf("Enter the number of the process:");

scanf("%d",&n); /*输入要创建的进程的数量*/

head=(PCB *)malloc(sizeof(PCB)); /*创建一个表头结点*/

p=head;

for(i=1;i<=n;i++) /*用循环来创建指定个结点*/

{ q=(PCB*)malloc(sizeof(PCB));

p->next=q;

p=q;

printf("nenter the NO.%d name of process:",i);

scanf("%s",&p->name);

printf("enter the priority of process:");

scanf("%d",&p->priority);

printf("enter the time need:");

scanf("%d",&p->needtime);

p->state=ready;

p->runtime=0;

p->next=NULL;

}

}

/*删除执行完毕的进程*/

void delete(PCB *head,PCB *p)

{PCB *q;

q=head;

while(q->next!=p)

q=q->next;

q->next=p->next;

free(p);

}

/*找出执行时间最短的进程*/

PCB *getminneedtime(PCB *head)

{PCB *p,*q;

p=head->next;

q=p->next;

while(q)

{if(p->needtime>q->needtime)

p=q;

q=q->next;

}

return(p);

}

/*找出优先级最高的进程*/

PCB *getpriority(PCB *head)

{PCB *p,*q;

p=head->next;

q=p->next;

while(q)

{if (p->priority>q->priority)

p=q;

}

return (p);

}

/*时间片轮转*/

void RR (void)

{ PCB *p,*q,*r;

int time;

printf("input the time:n ");

scanf("%d",&time);

for(p=head->next;p->next;p=p->next)

{r=p;}

while(head->next)

{p=head->next; /*选出就绪队列的第一个进程*/

p->state=run;

printf("n***Now the running process is:n"); /*输出该进程的信息*/

printf("%st",p->name);

printf("state:runt");

printf("needtime: %dt",p->needtime);

printf("runtime: %dn",p->needtime);

q=head->next;

if(p->needtime-p->runtime<=p->time) /*时间片内,进程运行结束否*/

{p->runtime=p->needtime;

printf("runtime:%dn",p->runtime);

}

else

{p->runtime=p->runtime+p->time;

printf("runtime:%dn",p->runtime);

}

q=p->next;

if(q!=NULL) /*输出就绪队列中的进程信息*/

printf("***Now the ready quenue is:n");

else printf("***Now the ready quenue is NONE!");

while(q)

{

printf("%st",q->name);

printf("state:readyt");

printf("needtime:%dt",q->needtime);

printf("runtime:%dt",q->runtime);

printf("n");

q=q->next;

}

if(p->runtime==p->needtime)

{delete(head,p);

}

else

{head->next=p->next;

r->next=p;

r=p;

r->state=ready;

r->next=NULL;

}}

}

/*优先权调度算法*/

void HPF (void)

{PCB *p,*q;

int flag=1;

while(head->next)

{ if(flag==1)

{p=getpriority(head); /*调用“找出执行时间最短的进程”函数*/

p->state=run;

} /*将选出的进程的状态改为“run”*/

(p->runtime)++;

(p->priority)--;

printf("n***Now the running process is:n"); /*输出该进程的信息*/

printf("%st",p->name);

printf("state:runt");

printf("needtime: %dt",p->needtime);

printf("runtime: %dn",p->runtime);

q=head->next;

if(q->next!=NULL) /*输出就绪队列中的进息*/

printf("***Now the ready quenue is:n");

else printf("***Now the ready quenue is NONE!");

while(q)

{if(q!=p)

{ printf("%st",q->name);

printf("state:readyt");

printf("needtime:%dt",q->needtime);

printf("runtime:%dt",q->runtime);

printf("n");

}

q=q->next;

}

if(p->runtime==p->needtime)

{ delete(head,p);

}

else

{for(q=head->next;q;q=q->next)

if(p->priority>=q->priority)

{flag=0;

}

else { flag=1;

p->state=ready;

}

}

/*将该运行结束的进程删除掉*/

}

}

/*短作业优先*/

void SJF (void)

{PCB *p,*q;

while(head->next)

{ p=getminneedtime(head); /*调用“找出执行时间最短的进程”函数*/

p->state=run; /*将选出的进程的状态改为“run”*/

printf("n***Now the running process is:n"); /*输出该进程的信息*/

printf("%st",p->name);

printf("state:runt");

printf("needtime: %dt",p->needtime);

printf("runtime: %dn",p->needtime);

q=head->next;

if(q->next!=NULL) /*输出就绪队列中的进程信息*/

printf("***Now the ready quenue is:n");

else printf("***Now the ready quenue is NONE!");

while(q)

{if(q!=p)

{ printf("%st",q->name);

printf("state:readyt");

printf("needtime:%dt",q->needtime);

printf("runtime:%dt",q->runtime);

printf("n");

}

q=q->next;

}

delete(head,p);

}

}

/*输出菜单*/

void printfunc(void)

/*将该运行结束的进程删除掉*/

{printf(" *******************n");

printf(" * 1-create *n");

printf(" * 2-SJF *n");

printf(" * 3-HPF *n");

printf(" * 4-RR *n");

printf(" * 0-exit *n");

printf(" *******************n");

}

/*主函数*/

main()

{int choice;

printfunc();

do

{printf("please input the number:");

scanf("%d",&choice);

getchar();

switch (choice)

{case 1:

create();

printfunc();

break;

case 2:

if(head==NULL || head->next==NULL)

printf("就绪队列没有进程,请先创建进程n");

else

{SJF();

printfunc();}

break;

case 3:

if(head==NULL || head->next==NULL)

printf("就绪队列没有进程,请先创建进程n");

else

{HPF();

printfunc();}

break;

case 4 :

if(head==NULL || head->next==NULL)

printf("就绪队列没有进程,请先创建进程n");

else

{RR();

printfunc();}

break;

case 0:

exit(0);

}

}

while(1);

}


发布者:admin,转转请注明出处:http://www.yc00.com/web/1713695308a2300066.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信