📄 fcfs.c
字号:
#include "conio.h"
#include "stdlib.h"
#include "stdio.h"
#define getPCB(type) (type*)malloc(sizeof(type))
#define NULL 0
/*void running(PCB *p,int m)*/
int n=0,time=0;float eti,ewi;
struct PCB{ char name[10]; /* 进程名 */
char state; /* 进程状态 */
int ts; /* 提交时间 */
float super; /* 优先权 */
int tb; /* 开始运行时间 */
int tc; /* 完成时间 */
float ti; /* 周转时间 */
float wi; /* 带权周转时间 */
int ntime; /* 进程所需运行时间 */
char resource[10]; /* 所需资源 */
struct PCB *link; /* 结构体指针 */
} *p,*q,*head=NULL;
typedef struct PCB PCB;
void inital(){
int i;
printf("\nInput PCB num\n");
scanf("%d",&n);
printf("Input\nname\tts\tntime\tresource\n");
for(i=0;i<n;i++){
p=getPCB(PCB);
scanf("%s\t%d\t%d\t%s",&p->name,&p->ts,&p->ntime,&p->resource);
p->state='W';
p->link=NULL;
if(head==NULL) head=q=p;
else{
q->link=p;
q=p;
}
}
}
void print(PCB *pr,int m){
PCB *p;
printf("\ntime=%d",time);
printf("\nts--SubmitTime;tr--RunTime;tb--BeginTime;tf--FinishTime;tt--TotalTime;nt--nTotalTime");
if(m==3){
printf("\nname\tstate\tts\ttr\tsuper\tsource\ttb\ttf\ttt\tnt\n");
printf("%s\t%c\t%d\t%d\t%4.2f\t%s\t%d\t%d\t%4.2f\t%4.2f\n",
pr->name,pr->state,pr->ts,pr->ntime,pr->super,pr->resource,pr->tb,pr->tc,pr->ti,pr->wi);
}
else {
printf("\nname\tstate\tts\ttr\tsource\ttb\ttf\ttt\tnt\n");
printf("%s\t%c\t%d\t%d\t%s\t%d\t%d\t%4.2f\t%4.2f\n",
pr->name,pr->state,pr->ts,pr->ntime,pr->resource,pr->tb,pr->tc,pr->ti,pr->wi);
}
p=head;
do{
if(p->state=='W')
if(m==3){
printf("%s\t%c\t%d\t%d\t%4.2f\t%s\n",
p->name,p->state,p->ts,p->ntime,p->super,p->resource);
}
else{
printf("%s\t%c\t%d\t%d\t%s\n",
p->name,p->state,p->ts,p->ntime,p->resource);
}
p=p->link;
}while(p!=NULL);
p=head;
do{
if(p->state=='F')
if(m==3){
printf("%s\t%c\t%d\t%d\t%4.2f\t%s\t%d\t%d\t%4.2f\t%4.2f\n",
p->name,p->state,p->ts,p->ntime,p->super,p->resource,p->tb,p->tc,p->ti,p->wi);
}
else{
printf("%s\t%c\t%d\t%d\t%s\t%d\t%d\t%4.2f\t%4.2f\n",
p->name,p->state,p->ts,p->ntime,p->resource,p->tb,p->tc,p->ti,p->wi);
}
p=p->link;
}while(p!=NULL);
}
void last(){
eti/=n;ewi/=n;
printf("\naverTotalTime=%7.3f\tnaverTotalTime=%7.3f\n",eti,ewi);
}
void running(PCB *p,int m){
p->tb=time;p->state='R';
p->tc=p->tb+p->ntime;
p->ti=(float)(p->tc-p->ts);
p->wi=(float)(p->ti/p->ntime);
eti+=p->ti;
ewi+=p->wi;
print(p,m);
time+=p->ntime;
p->state='F';
printf("\n%s has been finished!\npress any key to continue...\n",p->name);
getch();
}
void fcfs(int m){
int i,iden;
printf("\n\nthe PCB is runing...");
for(i=0;i<n;i++){
p=head;iden=1;
do{
if(p->state=='W'&&p->ts<=time) iden=0;
if(iden)p=p->link;
}while(p!=NULL&&iden) ;
if(iden) {
i--;printf("\ntime=%d:\tno PCB submib...wait...",time);time++;
if(time>100){printf("\nruntime is too long...error");getch();}
}
else{
running(p,m);
}
}
}
void start(){
int m=1;
char str[100]="Welcome to FCFS algrithem!\n";
printf("%s",str);
inital();
fcfs(m);
last();
}
void main(){
start();
printf("\nfinished!");
getch();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -