本程序是在Turbo C 2.0环境下编写的,预计在其他C程序编译系统中也没问题。
#define null 0
#define M " Menu\n\n1.Creat a process\n2.Destroy a
process\n3.Blockaprocess\n4.Wakeup a process\n5.Suspend a process\n6.Active a
process\n7.Observe the processes\n\nPlease select the item you want to realize:"
struct PCB
{char name;
int ko;
int cha;
}
struct PCB pcb[10];
int i=0;
void creat()
{char a;
int b;
printf("Please input the name of the process:");
scanf("%c",&a);
printf("\nPlease input the ko of the process:");
scanf("%d",&b);
i++;
pcb[i].name=a;
pcb[i].ko=b;
pcb[i].cha=1;
printf(M);
}
void destroy()
{char a;
int j;
printf("Select a process you want to destroy:");
scanf("%c",&a);
printf("\n");
for(j=1;j<=10;j++)
{if(pcb[j].name==a)
{pcb[j].name=0;
pcb[j].ko=0;
pcb[j].cha=0;
printf("Process %c has been destroied.");
}
else printf("There isn't process %c existed!");
}
printf("\n");
printf(M);
}
void block()
{char a;
int j;
printf("Please tell me which process you want to block:");
scanf("%c",&a);
printf("\n");
for(j=1;j<=10;j++)
{if(pcb[j].name==a)
{if(pcb[j].cha==1)
{pcb[j].cha=4;
pcb[j].ko++;
printf("Operation block has been finished!");
}
else if(pcb[j].cha==2)
{pcb[j].cha=3;
pcb[j].ko++;
printf("Operation block has been finished!");
}
else printf("There is no process can be blocked!");
}
}
printf("\n");
printf(M);
}
void wakeup()
{char a;
int j;
printf("Please tell me which process you want to wakeup:");
scanf("%c",&a);
printf("\n");
for(j=1;j<=10;j++)
{if(pcb[j].name==a)
{if(pcb[j].cha==3)
{pcb[j].cha=2;
pcb[j].ko--;
printf("You have wakeup process %c.",a);
}
else if(pcb[j].cha==4)
{pcb[j].cha=1;
pcb[j].ko--;
printf("You have wakeup process %c.",a);
}
else printf("There is no process can be wakedup!");
}
}
printf("\n");
printf(M);
}
void suspend()
{char a;
int j;
printf("Which process do you want to suspend?Select:");
scanf("%c",&a);
printf("\n");
for(j=1;j<=10;j++)
{if(pcb[j].name==a)
{if(pcb[j].cha==2)
{pcb[j].cha=1;
pcb[j].ko++;
printf("You have suspend process %c.",a);
}
else if(pcb[j].cha==3)
{pcb[j].cha=4;
pcb[j].ko++;
printf("You have suspend process %c.",a);
}
else if(pcb[j].cha==5)
{pcb[j].cha=1;
pcb[j].ko++;
printf("You have suspend process %c.",a);
}
else printf("You could suspend none!");
}
}
printf("\n");
printf(M);
}
void active()
{char a;
int j;
printf("Please tell me which process you want to active:");
scanf("%c",&a);
printf("\n");
for(j=1;j<=10;j++)
{if(pcb[j].name==a)
{if(pcb[j].cha==1)
{pcb[j].cha=2;
pcb[j].ko--;
printf("You have active process %c.",a);
}
else if(pcb[j].cha==4)
{pcb[j].cha=3;
pcb[j].ko--;
printf("You have active process %c.",a);
}
else printf("There is no process can be actived!");
}
}
printf("\n");
printf(M);
}
void run()
{struct PCB *p;
char t;
int j,m,k;
printf("The processes will run in the order of ko(lower earlier,higher later):\n");
for(j=1,p=pcb;p!=null;j++,p++)
{if(p[j].ko>p[j+1].ko)
{t=p[j].name;
p[j].name=p[j+1].name;
p[j+1].name=t;
m=p[j].cha;
p[j].cha=p[j+1].cha;
p[j+1].cha=m;
k=p[j].ko;
p[j].ko=p[j+1].ko;
p[j+1].ko=k;
}
}
for(p=pcb,j=1;p!=null;p++)
{printf("Process %c:",p[j].name);
printf("\tko:%d\tcha:%d",p[j].ko,p[j].cha);
printf("\n");
}
printf("\n");
printf(M);
}
observe()
{int j;
for(j=1;j!=null;j++)
{printf("Process %c:",pcb[j].name);
printf("\tko:%d\tcha:%d",pcb[j].ko,pcb[j].cha);
printf("\n");
}
}
exit()
{exit(1);
}
main()
{int a,k;
for(k=1;k<=10;k++)
{pcb[i].name=0;
pcb[i].ko=0;
pcb[i].cha=0;
}
printf(" Menu\n\n");
printf("1.Creat a process\n");
printf("2.Destroy a process\n");
printf("3.Block a process\n");
printf("4.Wakeup a process\n");
printf("5.Suspend a process\n");
printf("6.Active a process\n");
printf("7.Observe the processes\n");
printf("8.Exit\n");
printf("\n\n");
printf("\n\nPlease select the item you want to realize:");
scanf("%d",&a);
printf("\n\n");
if(a!=null)
switch(a)
{case 1:creat();break;
case 2:destroy();break;
case 3:block();break;
case 4:wakeup();break;
case 5:suspend();break;
case 6:active();break;
case 7:observe();break;
case 8:exit();break;
}
getch();
}
评论