#include
#include
#include
#include
typedef struct Pcb
{
char name[10];
struct Pcb*next;
}NPCB,*PCB;
bool delet_list(PCB phead);
int add_list(PCB phead, char s[10]);
void traverse_list(PCB phead);
void traverse(PCB phead1,PCB phead2,PCB phead3);
int main(void)
{
char s;
char a[10];
char r[10]="";
int i,i1;
PCB phead=(PCB)malloc(sizeof(PCB));
PCB phead1=(PCB)malloc(sizeof(PCB));
phead1->next=NULL;
PCB phead2=(PCB)malloc(sizeof(PCB));
phead2->next=NULL;
PCB phead3=(PCB)malloc(sizeof(PCB));
phead3->next=NULL;
PCB ptail=phead;
for(i=0;i<10;i++)
{
PCB pnew =(PCB)malloc(sizeof(PCB));
ptail->next=pnew;
strcpy(pnew->name,r);
ptail=pnew;
ptail->next=NULL;
}
// PCB ptail1=phead1;
while(1)
{
printf("请输入指令:(如果不清楚指令请输入h)");
scanf("%s",&s);
switch(s)
{
case 'c':
if(i==0)
{
printf("没有剩余空间");
break;
}
scanf("%s",&a);
add_list(phead1,a);
i--;
delet_list(phead);
printf("还剩%d个空间\n",i);
if (phead2->next==NULL)
{
add_list(phead2,a);
delet_list(phead1);
printf("当前执行态空闲,自动转为执行态\n");
traverse(phead1,phead2,phead3);
break;
}
traverse(phead1,phead2,phead3);
break;
case 'b':
char h[10];
strcpy(phead2->next->name,h);
printf("%s ",phead2->next->name);
add_list(phead3,h);
strcpy(phead1->next->name,h);
strcpy(h,phead2->next->name);
delet_list(phead1);
traverse(phead1,phead2,phead3);
break;
case 'w':
char j[10];
strcpy(phead3->next->name,j);
add_list(phead1,j);
delet_list(phead3);
printf("就绪态:\n");
traverse(phead1,phead2,phead3);
break;
case 't':
char k[10];
strcpy(phead2->next->name,k);
add_list(phead1,k);
strcpy(phead1->next->name,k);
add_list(phead2,k);
break;
case 'h':
printf("加入就绪态:c\n");
printf("由执行态转阻塞态:b\n");
printf("由阻塞态转就绪态:w\n");
printf("由执行态转就绪态:t\n");
break;
case 'e':
break;
}
}
return 0;
}
bool delet_list(PCB phead)
{
int i=0;
PCB p=phead;
PCB q = p->next;
p->next=p->next->next;
free(q);
return true;
}
int add_list(PCB phead,char s[10])
{
PCB l =phead;
PCB ptail;
if (phead->next==NULL)
{
ptail=phead;
}
else
{
while (l->next!=NULL)
{
l=l->next;
}
ptail=l;
}
PCB new1=(PCB)malloc(sizeof(PCB));
ptail->next=new1;
strcpy(new1->name,s);
ptail=new1;
ptail->next=NULL;
return 0;
}
void traverse_list(PCB phead)
{
PCB q=phead;
PCB p=phead->next;
while(p!=NULL)
{
printf("%s ",p->name);
p=p->next;
}
printf("\n");
return ;
}
void traverse(PCB phead1,PCB phead2,PCB phead3)
{
printf("就绪态:\n");
traverse_list(phead1);
printf("执行态:\n");
traverse_list(phead2);
printf("阻塞态:\n");
traverse_list(phead3);
}