进程管理实验

<p>用C语言编程模拟进程管理,至少要有:创建新的进程;查看运行进程;换出某个进程;杀死运行进程以及进程之间通信等功能。
</p><p>
</p><p>#define _CRT_SECURE_NO_DEPRECATE</p>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>

struct  PCB_type
{
	int pid;
	int priority;
	int cputime;
	int state;
};
int shumu = 0;
int pid_1;

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("neicun is full\n");
	}
	else
	{
		shumu++;
		printf("请输入新进程的程序名\n");
		scanf("%d", &neicun[shumu - 1].pid);
		printf("请输入新进程的优先级\n");
		scanf("%d", &neicun[shumu - 1].priority);
		printf("请输入新进程的运行时间\n");
		scanf("%d", &neicun[shumu - 1].cputime);
		printf("创建进程时令其状态为就绪\n");
		neicun[shumu - 1].state = 2;
		printf("\n创建进程成功!\n");
	}
		

}
	/*          查看当前运行进程         */
void run()
{
	int max = 0;
	for (int i = 0;i<shumu;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<shumu;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 < number;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)
			temp = 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<shumu;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 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();
			system("pause");
			break;
		case 2:
			run();
			printf("\npress anykey to go on~");
			system("pause");
				//getch();
			break;
		case 3:
			huanchu();
			printf("\npress anykey to go on~");
			system("pause");
				//getch();
			break;
		case 4:
			kill();
			printf("\npress anykey to go on~");
				//getch();
			system("pause");
			break;
		case 5:
			exit(0);
			default:
			n = 0;
			break;
		}
	}
}
很久不用VS,中途出现了几个小问题
1.warning C4996: 'getch': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _getch. See online help for details.

这种警告是因为VS的C函数库有更新的安全版本,
如果源码只打算用VS编译可以改为使用安全版本
比如scanf改为scanf_s
如果需要使用其他编译器编译,就无视吧


如果你getch() 是为了暂停程序以便观察结果,并且你的源码并不打算用其他编译器编译
可以使用system("pause");替换getch();效果就是暂停程序并显示 "请按任意键继续. . ." 

2.error C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

原因是Visual C++ 2012 使用了更加安全的 run-time library routines 。新的Security CRT functions(就是那些带有“_s”后缀的函数),请参见:


《CRT函数的安全增强的版本》
下面给出这个问题的解决方案:


方法一:将原来的旧函数替换成新的 Security CRT functions。


方法二:用以下方法屏蔽这个警告:


    1. 在预编译头文件stdafx.h里(注意:一定要在没有include任何头文件之前)定义下面的宏:


       #define _CRT_SECURE_NO_DEPRECATE


    2. 或声明 #param warning(disable:4996)


    3. 更改预处理定义:


        项目->属性->配置属性->C/C++ -> 预处理器 -> 预处理器定义,增加:


            _CRT_SECURE_NO_DEPRECATE


方法三:方法二没有使用更加安全的 CRT 函数,显然不是一个值得推荐的好方法,但我们又不想一个一个地改函数名,这里还有一个更简便的方法:


在预编译头文件 stdafx.h 里(同样要在没有include任何头文件之前)定义下面的宏:


#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1


在链接的时候便会自动将旧函数替换成 Security CRT functions 。


注意:这个方法虽然使用了新的函数,但是不能消除警告(原因见红字),你还得同时使用方法二(-_-)。即实际应在预编译头文件 stdafx.h 里加入下面两句:


#define _CRT_SECURE_NO_DEPRECATE


#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1

  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值