test code display
process algorithm
#include <stdio.h>
#include <malloc.h>
#include <windows.h>
#define PROCESS_NUM 5
#define PROCESS_TIME 50
enum state
{
ready,
execute,
block,
finish
};
struct pcb
{
char pname[10];
int priority;
int cputime;
int needtime;
int count;
int round;
enum state pstate;
struct pcb *next;
};
struct pcb *createProcess()
{
struct pcb *a,*b,*c;
int i=0;
printf("input name and time\n");
while(i<PROCESS_NUM)
{
a=(struct pcb*)malloc(sizeof(struct pcb));
scanf("%s",a->pname);
scanf("%d",&a->needtime);
a->cputime=0;
a->priority=PROCESS_TIME-a->needtime;
a->pstate=ready;
a->next=NULL;
if(i==0)
b=c=a;
else
b=b->next=a;
i++;
}
return c;
}
void displayPriority(struct pcb* a)
{
printf("%-3c%-12s%-12s%-12s%-12s%12s\n",' ',"进程名","CPU时间","所需时间","进程优先级","进程状态");
for(; a!=NULL; a=a->next)
{
printf("%-5c%-10s",' ',a->pname);
printf("%-2c%-10d",' ',a->cputime);
printf("%-4c%-8d",' ',a->needtime);
printf("%-4c%-8d",' ',a->priority);
switch(a->pstate)
{
case ready:
printf("%12s\n","ready");
break;
case execute:
printf("%12s\n","execute");
break;
case block:
printf("%12s\n","block");
break;
case finish:
printf("%12s\n","finish");
break;
}
}
}
int finishProcess(struct pcb* a)
{
int i=1;
while(i&&a)
{
i=i&&a->needtime==0;
a=a->next;
}
return i;
}
void executeProcess(struct pcb *a)
{
struct pcb*b=a;
int t=b->priority;
b=b->next;
for(;b!=NULL;b=b->next)
if(t>b->priority)
t=b->priority;
b=a;
for(; a!=NULL; a=a->next)
{
if(a->needtime==0)
a->pstate=finish;
if(a->pstate==execute)
a->pstate=ready;
if(t<=a->priority&&a->pstate==ready)
{
t=a->priority;
b=a;
}
}
if(b->needtime!=0)
{
b->priority-=3;
b->needtime--;
b->pstate=execute;
b->cputime++;
}
}
void calculatePriority()
{
int cpu=0;
struct pcb *a;
a=createProcess();
printf("\n");
displayPriority(a);
printf("\n");
while(!finishProcess(a))
{
cpu++;
executeProcess(a);
displayPriority(a);
printf("cputime :%d\n\n",cpu);
Sleep(500);
}
executeProcess(a);
displayPriority(a);
printf("\n所有进程已经处理完毕\n");
}
void showMenu()
{
printf("---------------------------\n");
printf(" 选择进程调度算法 :\n");
printf(" 1. 优先级调度算法\n");
printf(" 2. 循环轮转调度算法\n");
printf(" 3. 退出\n");
printf("---------------------------\n");
}
struct pcb* getProcessRound()
{
struct pcb *a,*b,*c;
int i=0;
printf("input name and time\n");
while(i<PROCESS_NUM)
{
a=(struct pcb*)malloc(sizeof(struct pcb));
scanf("%s",a->pname);
scanf("%d",&a->needtime);
a->cputime=0;
a->round=0;
a->count=0;
a->pstate=ready;
a->next=NULL;
if(i==0)
b=c=a;
else
b=b->next=a;
i++;
}
return c;
}
void addProcessRound(struct pcb*a)
{
if(a->needtime<=0)
return;
a->cputime+=2;
a->needtime-=2;
if(a->needtime<0)
a->needtime=0;
a->count++;
a->round++;
a->pstate=execute;
}
struct pcb* getNextPCB(struct pcb*a,struct pcb*b)
{
struct pcb*c;
c=a;
c=c->next;
for(; c&&c->pstate==finish; c=c->next);
if(c==NULL)
{
c=b;
for(; c!=a&&c->pstate==finish; c=c->next);
}
return c;
}
void setProcessState(struct pcb*a)
{
for(; a!=NULL; a=a->next)
{
if(a->needtime==0)
a->pstate=finish;
if(a->pstate==execute)
a->pstate=ready;
}
}
void displayRound(struct pcb*a)
{
printf("%-3c%-12s%-12s%-12s%-3c%-8s%-12s%12s\n",' ',"进程名","CPU时间","所需时间",' ',"计数","轮回次数","进程状态");
for(; a!=NULL; a=a->next)
{
printf("%-5c%-10s",' ',a->pname);
printf("%-2c%-10d",' ',a->cputime);
printf("%-4c%-8d",' ',a->needtime);
printf("%-4c%-8d",' ',a->count);
printf("%-3c%-9d",' ',a->round);
switch(a->pstate)
{
case ready:
printf("%10s\n","ready");
break;
case execute:
printf("%10s\n","execute");
break;
case finish:
printf("%10s\n","finish");
break;
}
}
}
void calculateRound()
{
int cpu=0;
struct pcb *a,*b;
a=getProcessRound();
b=a;
printf("\n");
displayRound(a);
printf("\n");
while(!finishProcess(a))
{
cpu+=2;
addProcessRound(b);
b=getNextPCB(b,a);
displayRound(a);
printf("cputime :%d\n\n",cpu);
setProcessState(a);
Sleep(500);
}
setProcessState(a);
displayRound(a);
printf("\n所有进程已经处理完毕\n");
}
int main()
{
char i;
while(1)
{
showMenu();
scanf("%d",&i);
switch(i)
{
case 1:
calculatePriority();
break;
case 2:
calculateRound();
break;
case 3:
return 0;
}
}
}
This blog is under a CC BY-NC-SA 3.0 Unported License
Link to this article: https://ts-think.github.io/2018/01/31/test-code-display/