📄 proceeding_dispatch.cpp
字号:
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
struct pcb{ /*进程控制块的结构*/
char name[8];
int runtime,needtime;
int priority;
char status;
struct pcb *next;
};
struct pcb *s;
int count=0; /*运行所需要的总时间*/
/*创建若干进程,当输入进程需要运行的时间为0时停止输入*/
void creatprocess(s)
{char n[8];
int time,prio;
struct pcb *p=s,*q;
while(1) /*输入进程及其运行时间*/
{printf("\n Please input the name of the process:");
scanf("%s",&n);
printf("Please input the time of the process:");
scanf("%d",&time);
printf("please input the priority of the process:");
scanf("%d",& prio);
count+=time;
if(time==0) break; /*当输入进程需要运行的时间为0时停止输入*/
q=(struct pcb *)malloc(sizeof(struct pcb));
strcpy(q->name,n); /*初始化进程控制块*/
q->runtime=0;
q->needtime=time;
q->priority= prio;
q->status='Ready';
p->next=q;
p=p->next;
free(q);
}
}
/*将进程按优先权排序*/
void sort(&s)
{
}
/*打印子程序*/
void printpcbline()
{struct pcb *p;
printf("\nname runtime needtime status\n");
printf("%s\t%d\t%d\t%c\n",s->name,s->runtime,s->needtime,s->status);
p=s->next;
while(p!=s)
{ printf("%s\t%d\t%d\t%c\n",p->name,p->runtime,p->needtime,p->status);
p=p->next;
}
}
void runpcbline()/*模拟调度子程序*/
{ struct pcb *p;
int c;
do{ s->runtime+=1;/*运行一次*/
count=count-1;
if(s->runtime==s->needtime)/*该进程运行完成*/
{ s->status='E'; /*改变其状态*/
printpcbline( );
p=s;
/*删除已完成的进程*/
if(s->next!=s)/*进程队列中不止一个进程*/
{
while(p->next!=s)
p=p->next;
p->next=s->next;
}
else
s=NULL;
if(s!=NULL)
s=s->next;
}
else s=s->next;
if(s!=NULL)
printpcbline();
printf(" Is there a new pcb(1/0)?");
scanf("%d",&c);
printf("%c",c);
if(c==1) {/*有新进程则插入队列*/
void insert();
printpcbline();
}
}while(count>0);
}
/*插入新进程*/
void insert()
{ struct pcb *r,*p;
r=(struct pcb*)malloc(sizeof(struct pcb));
printf("Please input the name of the new pcb:");
scanf("%s",r->name);
printf("\nPlease input the needtime of the new pcb:");
scanf("%d",&r->needtime);
r->runtime=0;
r->status='R';
count+=r->needtime;
if(s!=NULL)
{
p=s;
while(p->next!=s) p=p->next;
r->next=s;
p->next=r;
}
else
{s=r;
s->next=s;
}
}
main()
{s=(struct pcb *)malloc(sizeof(struct pcb));
s=NULL;
creatpcbline();
printpcbline();
runpcbline();
printf("All the process has finished!");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -