1.目的和要求
通过实验理解进程的概念,进程的组成(PCB结构),进程的并发执行和操作系统进行进程管理的相关原语(主要是进程的创建、执行、撤消)。
2.实验内容
用C语言编程模拟进程管理,至少要有:创建新的进程;查看运行进程;换出某个进程;杀死运行进程以及进程之间通信等功能。
3.主体程序
#include
#include
#include
struct PCB_type
{ int pid;
int priority;
int cputime;
int state;
int shumu=0,pid_l;
struct PCB_type neicun[20];
struct PCB_type hc[10];
int max=0;int number=0;
void create();
void run();
void huanchu();
void kill();
/* 创建新进程*/
void create()
{
if(shumu>=20)
{
printf("\n内存已满,请先结束或换出进程\n");
}
else
{
shumu++;
printf("\n请输入新进程的程序名\n");
scanf("%d",&neicun[shumu-1].pid);
printf("\n请输入新进程的优先级\n");
scanf("%d",&neicun[shumu-1].priority);
printf("\n请输入新进程的运行时间\n");
scanf("%d",&neicun[shumu-1].cputime);
printf("\n创建进程时令其状态为就绪\n");
neicun[shumu-1].state=2;
}
printf("\n创建进程成功!\n");
}
/* 查看当前运行进程*/
void run()
{
int max=0;
for(int i=0;i
{
if((neicun[i].state==1)&&(neicun[i].priority>=neicun[max].priority))
max=i;
}
neicun[max].state=3;
printf("当前运行进程程序名:\n%d",neicun[max].pid);
printf("\n该进程的优先级:\n%d",neicun[max].priority);
printf("\n该进程的运行时间:\n%d",neicun[max].cputime);
printf("\n该进程的状态:\n%d",neicun[max].state);
}
/* 换出*/
void huanchu()
{
int k;
printf("请输入要换出程序的程序名:");
scanf("%d",&k);
for(int j=0;j
{
if(neicun[j].state==1)
{
hc[number].pid=neicun[j].pid;
hc[number].state=neicun[j].state;
hc[number].priority=neicun[j].priority;
hc[number].cputime=neicun[j].cputime;
number++;
neicun[j].pid=0;
neicun[j].state=0;
neicun[j].priority=0;
neicun[j].cputime=0;
pid_1++;
}
else printf("进程%d无法换出的pid:%d\n",j.neicun[j].pid);
if(number!=0)
for(int i=0;i
{
printf("当前运行进程程序名:\n%d",hc[i].pid);
printf("\n该进程的优先级:\n%d",hc[i].priority);
printf("\n该进程的运行时间:\n%d",hc[i].cputime);
printf("\n该进程的状态:\n%d",hc[i].state);
}
}
PCB_type temp=neicun[0];
for(k=0;k<=shumu;k++)
{
if(neicun[k].priority>temp.priority)
tmpe=neicun[k];
}
neicun[k].state=1;
}
/* 杀死进程*/
void kill()
{
neicun[max].pid=0;
neicun[max].priority=0;
neicun[max].cputime=0;
neicun[max].state=0;
if(max==(shumu-1))
shumu--;
else{
for(int j=max+1;j
{
neicun[j-1].pid=neicun[j].pid;
neicun[j-1].priority=neicun[j].priority;
neicun[j-1].cputime=neicun[j].cputime;
neicun[j-1].state=neicun[j].state;
}
shumu--;
}
max=0;
run();
}
/* int k=0;
printf("请输入要杀死程序的进程名:");
scanf("%d",&k);
if(neicun[k].state=1)
neicun[k].state=2;
neicun[k].cputime=0;
neicun[k].pid=0;
neicun[k].priority=0;
neicun[k].state=0;
if(k==(shumu-1))
shumu--;
else{
for(int j=k+1;j
{
neicun[j-1].pid=neicun[j].pid;
neicun[j-1].priority=neicun[j].priority;
neicun[j-1].cputime=neicun[j].cputime;
neicun[j-1].state=neicun[j].state;
}
shumu--;
}
printf("进程%d已被杀死!,k");
}*/
int main()
{
int n,a;
n=1;
while(n==1)
{
system("cls");
printf("\n**********************************************");
printf("\n* 进程演示系统*");
printf("\n**********************************************");
printf("\n 1.创建新的进程2.查看运行进程");
printf("\n 3.换出某个进程4.杀死运行进程");
printf("\n 5.退出系统");
printf("\n**********************************************");
printf("\n请选择(1~5):");
scanf("%d",&a);
switch(a)
{ case 1:
create( );
printf("\npress anykey to go on~");
getch();
break;
case 2 :
run();
printf("\npress anykey to go on~");
getch();
break;
case 3 :
huanchu();
printf("\npress anykey to go on~");
getch();
break;
case 4 :
kill();
printf("\npress anykey to go on~");
getch();
break;
case 5 :
exit(0);
default:
n=0;
break;
}
}
}