实验说明:FZU的操作系统课程的实验一
实验目的
实验内容
实验要求
流程图
下面是C语言代码(个人所做,不保证完美无漏洞,仅供参考,杜绝无脑照搬)【粉丝专享注释代码】
#include<stdio.h>
#include<stdlib.h>
typedef struct process{
int need;//需要时间 (1+)
int priority;//优先级(0-99)
char name[20];//名字(20以内)
int run;//已运行
}Process;
typedef struct PCB * Link;
typedef struct PCB{
Link next;
Process * pro;
}PCB;
int main(){
int N;//进程数
int NEEDS;//总需要的时间片
Link l;//链表头指针
printf("\n请输入进程总个数?");
scanf("%d",&N);
Process p[N];//创建进程
Link pcbhead =(Link)malloc (sizeof (PCB));
pcbhead->next = NULL;
pcbhead->pro = NULL;
printf("\n已创建进程数为%d信息的空间",N);
int i=0;//循环变量
int j;//循环变量
//输入信息
while(i++<N){
printf("\n\n进程号No.%d:\n\n输入进程名(长度为20的字符内):",i-1);
scanf("%s",&p[i-1].name);
printf("\n输入进程优先数(0~99):");
scanf("%d",&p[i-1].priority);
printf("\n输入进程运行时间(自然数):");
scanf("%d",&p[i-1].need);
p[i-1].run = 0;
NEEDS+=p[i-1].need;
//创建链表
Link t = (Link)malloc (sizeof (PCB));//新增节点
l = pcbhead;
t->next = NULL;
t->pro = &p[i-1];
if(l->pro == NULL){
l->next = t->next;
l->pro = t->pro;
// printf("赋值表头");
}
else{
Link ht = NULL;
int isinsert = 0;
for(l= pcbhead;l;ht=l,l = l->next){
if(l->pro->priority >= p[i-1].priority)continue;
if(ht == NULL){
t->next=pcbhead;
pcbhead=t;
// printf("\n插入表头前");
}
else{
t->next = ht->next;
// printf("\n插入表中");
ht->next=t;
}
isinsert = 1;
break;
}
if(isinsert == 0){
t->next = ht->next;
// printf("\n插入表后");
ht->next=t;
}
}
}
printf("\n");
// printf("\n序列目前如下\n");
// //处理编排就绪队列
// for(l= pcbhead;l;l = l->next){
// printf("%s\n",l->pro->name);
// }
//过程
i = 0;
l=pcbhead;
while(i++<NEEDS){
printf("\n输入任意按键继续...");
getch();
printf("\n *-*-*-*-*-*-*-* The execute number: %d *-*-*-*-*-*-*-\n",i);
printf("\n *-*-*-*-* 当前正在运行的进程是: %s *-*-*-*-*\n",l->pro->name);
printf("p_name\tstate\tsuper\tndtime\truntime\n");
printf("%s\t%s\t%d\t%d\t%d\n",l->pro->name,"R",l->pro->priority,l->pro->need,l->pro->run);
//就绪队列
Link lt = l;
printf("\n *-*-*-*-* 当前就绪队列状态为: *-*-*-*-*\n");
for(lt=lt->next;lt;lt=lt->next){
printf("\np_name\tstate\tsuper\tndtime\truntime\n");
printf("%s\t%s\t%d\t%d\t%d\n",lt->pro->name,"w",lt->pro->priority,lt->pro->need,lt->pro->run);
}
//更新PCB
if(l->pro->priority>0)l->pro->priority--;
l->pro->run++;
//刷新队列
if(l->pro->run == l->pro->need){
//运行完毕释放
printf("\n时间片到期,此时进程 [%s] 已完成.",l->pro->name);
lt=l;
l=l->next;
free(lt);
}
else{
//若不为最后一个则取出插入
if(l->next){
lt=l;
Link ht = NULL;
l=l->next;
Link ll;//循环指针
int isinsert = 0;//是否在循环中插入
for(ll=l;ll;ht=ll,ll = ll->next){
if(ll->pro->priority >= lt->pro->priority)continue;
if(ht == NULL){
lt->next=l;
l=lt;
// printf("\n插入表头前");
}
else{
lt->next = ht->next;
// printf("\n插入表中");
ht->next=lt;
}
isinsert = 1;
break;
}
if(isinsert == 0){
lt->next = ht->next;
// printf("\n插入表后");
ht->next=lt;
}
}
}
}
if(N>0){
printf("\n\n\n进程已全部完成.\n");
getch();
}
return 0;
}
程序流程图