操作系统----进程管理(C语言)

一、进程管理
#include “conio.h”
#include “stdio.h”
#include “stdlib.h”

struct PCB_type
{
int pid; //进程名-----------------轮转时间片,当前队列指针
int priority; //进程优先数
int cputime; //进程所占用的CPU时间
int state; //进程的状态
};
struct PCB_type neicun[20],waicun[20];
int shumu=0,pid_l;

int create();
void run( );
void huanchu();
void kill( );
void clrscr();

void main()
{
int n;
char a;
n=1;
while(n==1)
{
clrscr();
printf("\n********************************************");
printf("\n* 进程演示系统 “);
printf(”\n
*******************************************");
printf("\n 1.创建新的进程 2.查看运行进程 “);
printf(”\n 3.换出某个进程 4.杀死运行进程 “);
printf(”\n 5.退出系统 “);
printf(”\n********************************************");
printf("\n请选择(1~5)");
a=getch();
switch(a)
{ case’1’:
create( ); /自定义过程/
break;
case’2’:
run( ); /* 自定义过程 */
break;
case’3’:
huanchu(); /自定义过程/
break;
case’4’:
kill( ); /自定义过程/
break;
case’5’:
exit(0);
default: n=0;
}
}
}
void clrscr()
{
system(“cls”);
}

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].priority);
printf("\n请输入新进程的大小\n");
scanf("%d",&neicun[shumu+1].cputime);
neicun[shumu+1].state=1;
shumu++;
}
return neicun[shumu].state;
}

void run( )/查看运行进程/
{
int rpid;
printf("\n请输入进程的pid\n");
scanf("%d",&rpid);
if(rpid>0&&rpid<=20&&neicun[rpid].state1)
{
printf("\n进程的pid是: %d\n",neicun[rpid].pid);
printf(“进程的优先级是: %d\n”,neicun[rpid].priority);
printf(“进程的大小是:%d\n”,neicun[rpid].cputime);
}
else
{
printf("\n所查看运行进程不存在\n");
}
printf(“请按回车退出查看\n”);
rpid=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].state
1)
{
if(neicun[pid1].priority>neicun[pid2].priority)
{
waicun[20].pid=neicun[pid1].pid;
waicun[20].priority=neicun[pid1].priority;
waicun[20].cputime=neicun[pid1].cputime;

		neicun[pid1].pid=neicun[pid2].pid;
		neicun[pid1].priority=neicun[pid2].priority;
		neicun[pid1].cputime=neicun[pid2].cputime;
		
		neicun[pid2].pid=waicun[20].pid;
		neicun[pid2].priority=waicun[20].priority;
		neicun[pid2].cputime=waicun[20].cputime;
		
		
		printf("\n替换完成\n");
		printf("\n被替换进程的pid是: %d\n",waicun[20].pid);
		printf("被替换进程的优先级是: %d\n",waicun[20].priority);
		printf("被替换进程的大小是:%d\n",waicun[20].cputime);
	}
	else
	{
		printf("\n进程优先级较小");
	}
}
else
{
	printf("所查看运行进程不存在\n");
}
printf("请按回车退出换出进程\n");
c=getch();
return;

}
void kill()/杀死运行进程/
{

int kpid;
printf("\n请输入进程的pid\n");
scanf("%d",&kpid);
if(kpid>0&&kpid<20&&neicun[kpid].state==1)
{
	neicun[kpid].state=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;

}
*/

你可能感兴趣的:(操作系统)