p1=pro=(struct process *)malloc(L); scanf(\"%s %d\t1=i++; /*由程序指定进程的到达时间*/ if(j>=5) {j=1;} /*进程要求运行时间小于5*/ t2=j++; t2+=1;/*由程序指定进程的到达时间*/ pro->name=name;pro->status='R'; pro->t1=t1; pro->t2=t2; pro->num=num;
printf(\"task number:%d,name:%c,status:%c,
arrivaltime:%d,servicetime:%d\\n\pro->name,pro->status,pro->t1,pro->t2); p1=h; /*p1指向第一个节点*/ p0=pro; /*p0指向待插入节点*/ if(counter==1) /*没有结点*/
{h=p0;p0->next=NULL; /*把节点插入队尾*/} else
{while(p0->t1>p1->t1&&p1->next!=NULL) {p2=p1;
p1=p1->next; /*p1指针后移*/} }
if(p0->t1<=p1->t1) {if(h==p1) h=p0; else p2->next=p1;
p0->next=p1; } /*把节点插到p1前面*/ else
{ p1->next=p0;p0->next=NULL; } /*把节点插到p1后面*/} return(h);}
(2)输出进程运行情况函数代码如下: void print(struct process *Q) {int j=1;
struct process *p1; p1=Q; if(Q!=NULL) do{
4、实验结果
{p1->status=’C’;
printf(\"%d %d %c %c%d\\n\
p1->name,,p1->status,p1->t2); /*完成状态为C*/} else
printf(\"%d %d %c %c%d\\n\p1->name,,p1->status,p1->t2); j++;} p1=p1->next;}while(p1!=NULL);}
(3)主函数代码如下: main()/*主函数*/ { int i;
struct process *head,*p,*q; /* 队首指针head ,用来指出最先进入系统中的进程 */
head=(struct process *)malloc(L); /*创建头结点*/ head=NULL;p=NULL;printf(\"\\n\"); printf(\"******************FCFS scheduling arithmetic status*****************\\n\\n\"); printf(\"input the total number of the task:\"); scanf(\"%d\输入总进程数*/printf(\"\\n\"); printf(\"please input the following information:\"); printf(\"process name,process number\\n\\n\"); q=creat(head,p);/*调用creat函数*/ printf(\"\\nAll the task is ready!\"); printf(\"Press Enter to continue.\"); getch();clrscr();printf(\"\\n\");
printf(\"------------------------------------------\\n\"); printf(\"****************************\\n\");
printf(\"* present process process process leaving*\\n\");
printf(\" * running service*\\n\"); printf(\" * ime\\number\\name\\status\\time *\\n\");
printf(\"********************************\\n\"); printf(\" -----------------------------------------------\\n\"); print(q); /*调用print函数*/
printf(\"********************************\\n\"); printf(\"--------------------------------------------------\"); free(head); /*释放内存空间*/ free(p); free(q);getch(); }
2
以系统中有五个进程为例,进程名和进程号如下所示: 进程号 进程名 1 A 2 C 3 E 4 F 5 S
运行环境为Win-TC,运行结果如下:
3