#include "conio.h"
#include "stdio.h"
#include "stdlib.h"
struct jincheng_type
{
int pid;
int youxian;
int daxiao;
int msg;
int live;
};
struct jincheng_type neicun[20],waicun[20];
int shumu=0,pid_l;
int create();
void viewrun( );
void huanchu();
void kill( );
void tongxun( );
main()
{
int n,m,i;
char 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.进程之间通信 6.退出 ");
printf("\n********************************************");
printf("\n请选择(1~6) ");
a=getch();
switch(a)
{
case'1':
create( );
break;
case'2':
viewrun( );
break;
case'3':
huanchu();
break;
case'4':
kill( );
break;
case'5':
tongxun( );
break;
case'6': exit(0);
default: n=0;
}
}
}
int create( ) /* 创建一个进程的示例(不完整的程序) */
{
if(shumu>=20)
{
printf("\n内存已满,请先结束或换出进程\n");
}
else
{
printf("\n请输入新进程的pid\n");
scanf("%d",&neicun[shumu+1].pid);
printf("\n请输入新进程的优先级\n");
scanf("%d",&neicun[shumu+1].youxian);
printf("\n请输入新进程的大小\n");
scanf("%d",&neicun[shumu+1].daxiao);
printf("\n请输入新进程的消息\n");
scanf("%d",&neicun[shumu+1].msg);
neicun[shumu+1].live=1;
shumu++;
}
return neicun[shumu-1].live;
}
void viewrun( )/*查看运行进程*/
{
int vpid;
printf("\n请输入进程的pid\n");
scanf("%d",&vpid);
if(vpid>0&&vpid<=20&&neicun[vpid].live==1)
{
printf("\n进程的pid是: %d\n",neicun[vpid].pid);
printf("进程的优先级是: %d\n",neicun[vpid].youxian);
printf("进程的大小是:%d\n",neicun[vpid].daxiao);
printf("进程的消息是:%d\n",neicun[vpid].msg);
}
else
{
printf("\n所查看运行进程不存在\n");
}
printf("请按回车退出查看\n");
vpid=getch();
}
void huanchu()/*换出某个进程*/
{
int pid1,pid2;
char c;
printf("\n请输入第一个替换进程的pid\n");
scanf("%d",&pid1);
printf("\n请输入第二个替换进程的pid\n");
scanf("%d",&pid2);
if(pid1>0&&pid1<=20&&neicun[pid1].live==1)
{
if(neicun[pid1].youxian>neicun[pid2].youxian)
{
waicun[20].pid=neicun[pid1].pid;
waicun[20].youxian=neicun[pid1].youxian;
waicun[20].daxiao=neicun[pid1].daxiao;
waicun[20].msg=neicun[pid1].msg;
neicun[pid1].pid=neicun[pid2].pid;
neicun[pid1].youxian=neicun[pid2].youxian;
neicun[pid1].daxiao=neicun[pid2].daxiao;
neicun[pid1].msg=neicun[pid2].msg;
neicun[pid2].pid=waicun[20].pid;
neicun[pid2].youxian=waicun[20].youxian;
neicun[pid2].daxiao=waicun[20].daxiao;
neicun[pid2].msg=waicun[20].msg;
printf("\n替换完成\n");
printf("\n被替换进程的pid是: %d\n",waicun[20].pid);
printf("被替换进程的优先级是: %d\n",waicun[20].youxian);
printf("被替换进程的大小是:%d\n",waicun[20].daxiao);
printf("被替换进程的消息是:%d\n",waicun[20].msg);
}
else
{
printf("\n进程优先级不够大");
}
}
else
{
printf("所查看运行进程不存在");
}
printf("请按回车退出换出进程\n");
c=getche();
return;
}
void kill()/*杀死运行进程*/
{
int kpid;
printf("\n请输入进程的pid\n");
scanf("%d",&kpid);
if(kpid>0&&kpid<20&&neicun[kpid].live==1)
{
neicun[kpid].live=0;
}
return;
}
void tongxun( )/*进程之间通信*/
{
int tpid1,tpid2;
int buffer;
char d;
printf("\n请输入通信源进程pid\n");
scanf("%d",&tpid1);
printf("\n请输入通信目的进程pid\n");
scanf("%d",&tpid2);
if(tpid1>0&&tpid1<20&&neicun[tpid1].live==1)
{
buffer=neicun[tpid1].msg;
neicun[tpid1].msg=neicun[tpid2].msg;
neicun[tpid2].msg=buffer;
printf("\n源进程的消息是: %d\n",neicun[tpid1].msg);
printf("\n目的进程的消息是: %d\n",neicun[tpid2].msg);
}
else
{
printf("\n所查看运行进程不存在\n");
}
printf("\n请按回车退出进程通信\n");
d=getch();
return;
}