编写并调试一个模拟的进程调度程序,采用“轮转法”调度算法对五个进程进行调度。
//最终成功版
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
//#define getpch(type) (type*)malloc(sizeof(type))
#define NULL 0
struct pcb /* 定义进程控制块PCB */
{
char name[10];
char state;
int super;
int ntime;
int rtime;
struct pcb* link;
};
struct pcb *head,*foverhead;
int num;
int flag=0;
int creat();
void check();
void show();
void inShow();
void flush();
int main(){
char ch;
head=(struct pcb*)malloc(sizeof(struct pcb));
foverhead=(struct pcb*)malloc(sizeof(struct pcb));
foverhead=head;
num=creat();
show(num);
printf("\n 下面开始自己运行\n\n============================================================");
int runtime=0;
head=foverhead->link;
while((num!=0)&&foverhead!=NULL){
check();
if(flag==0)
{
runtime++;
}
printf("\n目前的运行时间:%d \n ",runtime);
printf("\n 按任一键继续......\n");
head->state='w';
head=head->link;
ch=getchar();
}
printf("\n 所有进程都已经完成喽!!!byebye\n");
return 0;
}
int creat(){
int num;
printf("\n 请输入进程块数:");
scanf("%d",&num);
int pr=num;
for(int i=0; i<num; i++)
{
printf("\n 进程号No.%d:\n",i+1);
struct pcb *p=(struct pcb*)malloc(sizeof(struct pcb));
printf("\n 输入进程名:");
scanf("%s",p->name);
printf("\n 输入进程运行时间:");
scanf("%d",&p->ntime);
p->super=pr--;
printf("\n");
p->rtime=0;
p->state='w';
p->link=NULL;
head->link=p;
// printf("\n 输入进程名:");
head=p;
}
head->link=foverhead->link;
return num;
}
void check(){
if(head->ntime==head->rtime){
printf("\n %s 已经运行完成\n--------------------------------------------",head->name);
struct pcb *p=head;
head->link=p->link;
head=head->link;
num--;
flag=1;
}else{
head->rtime++;
head->state='R';
inShow();
flag=0;
}
}
void show(){
head=foverhead;
head=head->link;
printf("\n进程名字 状态 优先级 需要时长 已运行时长\n");
for(int i=0;i<num;i++){
printf("|%s\t",head->name);
printf("|%c\t",head->state);
printf("|%d\t",head->super);
printf("|%d\t",head->ntime);
printf("|%d\t",head->rtime);
printf("\n");
head=head->link;
}
}
void inShow(){
flush();
printf("\n进程名字 状态 优先级 需要时长 已运行时长\n");
for(int i=0;i<num;i++){
printf("|%s\t",head->name);
printf("|%c\t",head->state);
printf("|%d\t",head->super);
printf("|%d\t",head->ntime);
printf("|%d\t",head->rtime);
printf("\n");
head=head->link;
}
}
void flush(){
struct pcb *temp=head;
for(int i=0;i<num;i++){
head->super=num-i;
head=head->link;
}
head=temp;
}
哈哈哈 谁知道C语言咋写啊啊啊啊啊啊 (已经疯癫)
不得不说,在一些输出,有一些粗制滥造!(好吧,是很粗制滥造)
一些运行结果