模拟进程控制(计算机操作系统实验)

#include
#include
int I=0;
struct ready
{
char pro_name[10];
struct ready *next;
};
struct perform
{
char pro_name[10];
struct perform *next;
};
struct blocking
{
char pro_name[10];
struct blocking *next;
};

void pro_found(struct ready *&r_head,struct perform *&p_head,struct blocking *&b_head)
{
void main1(struct ready *&r_head,struct perform *&p_head,struct blocking *&b_head);
void pro_found1(char name[],struct ready *&r_head,struct perform *&p_head,struct blocking *&b_head);
char name[10];
struct ready *r;
struct perform *p;
struct blocking *b;

if(I<10)
{
printf("\nPlease enter the name of the process:");
scanf("%s",name);
system("cls");
if(I==0)
{
pro_found1(name,r_head,p_head,b_head);
}
if(I>0)
{
r=r_head;p=p_head;b=b_head;
while(r!=NULL)
{
if(strcmp(r->pro_name,name)==0)
{
printf("\nSorry,name already exists,please enter again.\n");
pro_found(r_head,p_head,b_head);
}
r=r->next;
}
while(p!=NULL)
{
if(strcmp(p->pro_name,name)==0)
{
printf("\nSorry,name already exists,please enter again.\n");
pro_found(r_head,p_head,b_head);
}
p=p->next;
}
while(b!=NULL)
{
if(strcmp(b->pro_name,name)==0)
{
printf("\nSorry,name already exists,please enter again.\n");
pro_found(r_head,p_head,b_head);
}
b=b->next;
}
pro_found1(name,r_head,p_head,b_head);
}
}
else
{
printf("\nSorry,there is no blank PCB.\n");
getchar();
getchar();
system("cls");
main1(r_head,p_head,b_head);
}
}
void pro_found1(char name[],struct ready *&r_head,struct perform *&p_head,struct blocking *&b_head)
{
void main1(struct ready *&r_head,struct perform *&p_head,struct blocking *&b_head);
struct ready *z,*p,*l;
struct perform *m;

I++;

z=(struct ready *)malloc(sizeof(struct ready));
strcpy(z->pro_name,name);
if(r_head==NULL)
{
r_head=z;
r_head->next=NULL;
if(p_head==NULL)
{
m=(struct perform *)malloc(sizeof(struct perform));
strcpy(m->pro_name,r_head->pro_name);
p_head=m;
m->next=NULL;

l=r_head;
r_head=r_head->next;
free(l);
printf("\nSuccessfully created a process,and turn to perform.\n");
main1(r_head,p_head,b_head);
}
else
{
printf("\nSuccessfully created a process,and turn to ready.\n");
main1(r_head,p_head,b_head);
}
}
else
{
p=r_head;
while(p->next!=NULL)
{
p=p->next;
}
p->next=z;
z->next=NULL;
printf("\nSuccessfully created a process,and turn to ready.\n");
main1(r_head,p_head,b_head);
}
}
void pro_end(struct ready *&r_head,struct perform *&p_head,struct blocking *&b_head)
{
void main1(struct ready *&r_head,struct perform *&p_head,struct blocking *&b_head);
struct ready *n;
struct perform *p,*l;

if(p_head==NULL)
{
printf("\nSorry,none of the process of performing.\n");
getchar();
getchar();
system("cls");
main1(r_head,p_head,b_head);
}
l=p_head;
p_head=p_head->next;
free(l);
I--;
if(r_head!=NULL)
{
p=(struct perform *)malloc(sizeof(struct perform));
strcpy(p->pro_name,r_head->pro_name);
p_head=p;
p_head->next=NULL;

n=r_head;
r_head=r_head->next;
free(n);
main1(r_head,p_head,b_head);
}
else
main1(r_head,p_head,b_head);
}
void pro_bloceing(struct ready *&r_head,struct perform *&p_head,struct blocking *&b_head)
{
void main1(struct ready *&r_head,struct perform *&p_head,struct blocking *&b_head);
struct blocking *p,*e;
struct ready *l;
struct perform *n;
if(p_head!=NULL)
{
if(b_head==NULL)
{
p=(struct blocking *)malloc(sizeof(struct blocking));
strcpy(p->pro_name,p_head->pro_name);
b_head=p;
b_head->next=NULL;
}
else
{
e=b_head;
while(e->next!=NULL)
{
e=e->next;
}
p=(struct blocking *)malloc(sizeof(struct blocking));
strcpy(p->pro_name,p_head->pro_name);
e->next=p;
p->next=NULL;
}
if(r_head!=NULL)
{
strcpy(p_head->pro_name,r_head->pro_name);
l=r_head;
r_head=r_head->next;
free(l);
main1(r_head,p_head,b_head);
}
else
{
n=p_head;
p_head=p_head->next;
free(n);
main1(r_head,p_head,b_head);
}
}
else
{
printf("\nSorry,none of the process of performing.\n");
getchar();
getchar();
system("cls");
main1(r_head,p_head,b_head);
}
}
void pro_wakeup(struct ready *&r_head,struct perform *&p_head,struct blocking *&b_head)
{
void main1(struct ready *&r_head,struct perform *&p_head,struct blocking *&b_head);
struct ready *p,*m,*n;
struct perform *q;
struct blocking *l;
if(b_head!=NULL)
{
if(r_head!=NULL)
{
p=(struct ready *)malloc(sizeof(struct ready));
strcpy(p->pro_name,b_head->pro_name);
m=r_head;
while(m->next!=NULL)
{
m=m->next;
}
m->next=p;
p->next=NULL;
}
else
{
p=(struct ready *)malloc(sizeof(struct ready));
strcpy(p->pro_name,b_head->pro_name);
r_head=p;
p->next=NULL;
if(p_head==NULL)
{
q=(struct perform *)malloc(sizeof(struct perform));
strcpy(q->pro_name,r_head->pro_name);
p_head=q;
q->next=NULL;

n=r_head;
r_head=r_head->next;
free(n);
}
}
l=b_head;
b_head=b_head->next;
free(l);
main1(r_head,p_head,b_head);
}
else
{
printf("\nSorry,none of the process of blocking.\n");
getchar();
getchar();
system("cls");
main1(r_head,p_head,b_head);
}
}
void timeover(struct ready *&r_head,struct perform *&p_head,struct blocking *&b_head)
{
void main1(struct ready *&r_head,struct perform *&p_head,struct blocking *&b_head);
struct ready *p,*m,*l;
struct perform *q,*n;
if(p_head!=NULL)
{
if(r_head==NULL)
{

p=(struct ready *)malloc(sizeof(struct ready));
strcpy(p->pro_name,p_head->pro_name);
r_head=p;
p->next=NULL;
}
else
{
m=r_head;
while(m->next!=NULL)
{
m=m->next;
}
p=(struct ready *)malloc(sizeof(struct ready));
strcpy(p->pro_name,p_head->pro_name);
m->next=p;
p->next=NULL;
}
n=p_head;
p_head=p_head->next;
free(n);
q=(struct perform *)malloc(sizeof(struct perform));
strcpy(q->pro_name,r_head->pro_name);
p_head=q;
p_head->next=NULL;
l=r_head;
r_head=r_head->next;
free(l);
main1(r_head,p_head,b_head);
}
else
{
printf("\nSorry,none of the process of performing.\n");
getchar();
getchar();
system("cls");
main1(r_head,p_head,b_head);
}
}

void xianshi(struct ready *&r_head,struct perform *&p_head,struct blocking *&b_head)
{
struct ready *r;
struct perform *p;
struct blocking *b;
r=r_head;p=p_head;b=b_head;
printf("\nReady: ");
while(r!=NULL)
{
printf("%s ",r->pro_name);
r=r->next;
}
printf("\nPerform: ");
while(p!=NULL)
{
printf("%s ",p->pro_name);
p=p->next;
}
printf("\nBloceing: ");
while(b!=NULL)
{
printf("%s ",b->pro_name);
b=b->next;
}
printf("\n");
}
void main1(struct ready *&r_head,struct perform *&p_head,struct blocking *&b_head)
{
int n;
printf("\n空白PCB个数:%d\n",10-I);
xianshi(r_head,p_head,b_head);
printf("\n");
printf("1c: -------------------------- 创建进程\n");
printf("2e: -------------------------- 结束进程\n");
printf("3b: -------------------------- 进程阻塞\n");
printf("4w: -------------------------- 激活进程\n");
printf("5t: -------------------------- 时间片到\n");
printf(" 0: -------------------------- 退 出\n");
printf("请选择: (0----5):");
scanf("%d",&n);
switch(n)
{
case 1:system("cls");pro_found(r_head,p_head,b_head);
case 2:system("cls");pro_end(r_head,p_head,b_head);
case 3:system("cls");pro_bloceing(r_head,p_head,b_head);
case 4:system("cls");pro_wakeup(r_head,p_head,b_head);
case 5:system("cls");timeover(r_head,p_head,b_head);
case 0:exit(0);
}
}
void main()
{
struct ready *r_head=NULL;
struct perform *p_head=NULL;
struct blocking *b_head=NULL;
system("color 0A");
main1(r_head,p_head,b_head);
}

相关文档
最新文档