资源描述:
《操作系统-进程管理实验c语言》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、#include"stdio.h"#include"stdlib.h"#defineready1#definerun2structpcb{charname[10];intpriority;/*进程的优先级*/intstate;/*进程的状态:可以有run、ready、finish(可有可无)*/intneedtime;/*进程需要运行的时间*/intruntime;inttime;/*进程已运行的时间*/structpcb*next;/*指向下一个进程PCB的指针*/};typedefstructpcbPCB;PCB*h
2、ead=NULL;/*此函数用于创建进程队列*/voidcreate(void){PCB*p,*q;intn,i;printf("Enterthenumberoftheprocess:");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("ent
3、ertheNO.%dnameofprocess:",i);scanf("%s",&p->name);printf("enterthepriorityofprocess:");scanf("%d",&p->priority);printf("enterthetimeneed:");scanf("%d",&p->needtime);p->state=ready;p->runtime=0;p->next=NULL;}}/*删除执行完毕的进程*/voiddelete(PCB*head,PCB*p){PCB*q;q=head;wh
4、ile(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;whi
5、le(q){if(p->priority>q->priority)p=q;}return(p);}/*时间片轮转*/voidRR(void){PCB*p,*q,*r;inttime;printf("inputthetime:");scanf("%d",&time);for(p=head->next;p->next;p=p->next){r=p;}while(head->next){p=head->next;/*选出就绪队列的第一个进程*/p->state=run;printf("***Nowtherunningp
6、rocessis:");/*输出该进程的信息*/printf("%st",p->name);printf("state:runt");printf("needtime:%dt",p->needtime);printf("runtime:%d",p->needtime);q=head->next;if(p->needtime-p->runtime<=p->time)/*时间片内,进程运行结束否*/{p->runtime=p->needtime;printf("runtime:%d",p->runtime)
7、;}else{p->runtime=p->runtime+p->time;printf("runtime:%d",p->runtime);}q=p->next;if(q!=NULL)/*输出就绪队列中的进程信息*/printf("***Nowthereadyquenueis:");elseprintf("***NowthereadyquenueisNONE!");while(q){printf("%st",q->name);printf("state:readyt");printf("needtime:%d
8、t",q->needtime);printf("runtime:%dt",q->runtime);printf("");q=q->next;}if(p->runtime==p->needtime){delete(head,p);}else{head->next=p->next;r->next=p;r=p;r->