📄 lunhuan.cpp
字号:
#include "stdio.h"
#include <stdlib.h>
#include <conio.h>
#define getlzh(type) (type*)malloc(sizeof(type))
#define NULL 0
struct lunzhuan
{
char name[10];
char state;
int ntime;
int rtime;
struct lunzhuan *link;
}*head=NULL,*p;
typedef struct lunzhuan LZH;
void init()
{
head=getlzh(LZH);
printf("请输入进程名称:");
scanf("%s",head->name);
printf("请输入进程运行所需要的时间:");
scanf("%d",&head->ntime);
head->rtime=0;
head->state='w';
head->link=head;
p=head;
}
void input()
{
int count;
printf("请输入进程数量:");
scanf("%d",&count);
if(count<1)
return;
else
{
init();
count--;
while(count)
{
LZH *temp=getlzh(LZH);
printf("请输入进程名称:");
scanf("%s",temp->name);
printf("请输入进程运行所需要的时间:");
scanf("%d",&temp->ntime);
temp->link=head;
temp->state='w';
temp->rtime=0;
p->link=temp;
p=temp;
count--;
}
}
}
void disp(LZH *pr) /*建立进程显示函数,用于显示当前进程*/
{
printf("|%s\t",pr->name);
printf("|%c\t",pr->state);
printf("|%d\t",pr->ntime);
printf("|%d\t",pr->rtime);
printf("\n");
printf("\n");
}
void run(LZH *lp1)
{
lp1->state='r';
disp(lp1);
lp1->rtime++;
lp1->state='w';
}
LZH* destroy(LZH *lp2)
{
printf("进程%s已经完成!\n",lp2->name);
LZH *lp3=NULL;
if(lp2->link==lp2)
{
free(lp2);
return 0;
}
else
{
lp3=lp2;
while(lp3->link!=lp2)
{
lp3=lp3->link;
}
lp3->link=lp2->link;
free(lp2);
return lp3;
}
}
void main()
{
input();
system("cls");
LZH *lp4=head,*lp5;
while(lp4)
{
printf("请按任意键继续!");
getchar();
if((lp4->rtime<lp4->ntime)&&lp4)
{
printf("当前运行的进程是: %s\n",lp4->name);
printf("\nqname\tstate\tndtime\truntime\n");
run(lp4);
}
if(lp4&&(lp4->link!=lp4))
{
lp5=lp4;
printf("当前守护进程有:");
printf("\nqname\tstate\tndtime\truntime\n");
while((lp5->link!=lp4))
{
disp(lp5->link);
lp5=lp5->link;
}
}
if(lp4->rtime==lp4->ntime)
{
lp4=destroy(lp4);
}
if(lp4)
lp4=lp4->link;
else
{printf("所有进程已经完成!\n"); break;}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -