最近在写操作系统的实验报告,顺带写篇博客~
题目:用C++编程来模拟 进程状态的转换
备注都在上面,一看就懂哈~
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct jincheng_type
{
int pid; //进程的ID
int youxian; //进程的优先级
int daxiao; //进程的大小
int zhuangtai;//进程的状态
char info[10];//新进程的内容
};
struct jincheng_type neicun[20];//内存容量,最大20个进程
int shumu = 0, guaqi = 0, pid, flag = 0;
/***********创建新的进程***********/
void create()
{
if(shumu >= 20) printf("\n内存已满,请先唤出或杀死进程\n");
else
{
int i;
for( i = 0; i < 20; i++)
{
if(neicun[i].zhuangtai == 0) break;
}
printf("\n请输入新进程pid\n");
scanf("%d", &(neicun[i].pid));
for(int j = 0; j < i; j++)
if(neicun[i].pid == neicun[j].pid)
{
printf("\n该进程已存在\n");
return;
}
printf("\n请输入进程优先级\n");
scanf("%d", &(neicun[i].youxian));
printf("\n请输入进程大小\n");
scanf("%d", &(neicun[i].daxiao));
printf("\n请输入新进程的内容\n");
scanf("%s", &(neicun[i].info));
neicun[i].zhuangtai = 1;
shumu++;
}
}
/***********查看运行进程***********/
void run()
{
for(int i = 0; i < 20; i++)
{
if(neicun[i].zhuangtai == 1)
{
printf("\n pid = %d", neicun[i].pid);
printf("youxian = %d",neicun[i].youxian);
printf("daxiao = %d",neicun[i].daxiao);
printf("zhuangtai = %d", neicun[i].zhuangtai);
printf("info = %s", neicun[i].info);
flag = 1;
}
}
if(!flag)
{
printf("\n当前没有运行进程\n");
}
}
/***********唤出某个进程***********/
void huancun()
{
if(!shumu)
{
printf("\n当前没有运行进程\n");
return ;
}
printf("\n输入唤出进程的ID值");
scanf("%d", &pid);
for(int i = 0; i < 20; i++)
{
if(pid == neicun[i].pid)
{
if(neicun[i].zhuangtai == 1)
{
neicun[i].zhuangtai = 2;
guaqi++;
printf("\n已成功唤出进程\n");
}
else
if(neicun[i].zhuangtai == 0)
printf("\n要唤出的进程不存在\n");
else printf("\n要唤出的进程已被挂起\n");
flag = 1;
break;
}
}
if(flag == 0)
printf("\n要唤出的进程不存在\n");
}
/***********杀死运行进程***********/
void kill()
{
if(!shumu)
{
printf("\n当前没有运行进程\n");
return;
}
printf("\n输入杀死进程的ID值");
scanf("%d", &pid);
for(int i = 0; i < 20; i++)
{
if(pid == neicun[i].pid)
{
if(neicun[i].zhuangtai == 1)
{
neicun[i].zhuangtai = 0;
shumu --;
printf("\n已成功杀死进程\n");
}
else
if(neicun[i].zhuangtai == 0)
printf("\n要杀死的进程不存在\n");
else
printf("\n要杀死的进程已被挂起\n");
flag = 1;
break;
}
}
if(!flag)
printf("\n要杀死的进程不存在\n");
}
/***********唤醒某个进程***********/
void huanxing()
{
if(!shumu)
{
printf("\n当前没有运行进程\n");
return;
}
printf("\n输入pid\n");
scanf("%d", &pid);
for(int i = 0; i < 20; i++)
{
if(pid == neicun[i].pid)
{
flag = false;
if(neicun[i].zhuangtai == 2)
{
neicun[i].zhuangtai = 1;
guaqi --;
printf("\n已成功唤醒进程\n");
}
else
if(neicun[i].zhuangtai == 0)
printf("\n要唤醒的进程不存在\n");
else printf("\n要唤醒的进程已被挂起\n");
break;
}
}
if(flag)printf("\n要唤醒的进程不存在\n");
}
int main()
{
int n = 1;
int num;
for(int i = 0; i < 20; i++)
neicun[i].zhuangtai = 0;
while(n)
{
printf("\n*********************************************************\n");
printf("\n 进程演示系统 \n");
printf("\n 1创建新的进程 2查看运行进程\n");
printf("\n 3唤出某个进程 4杀死运行进程\n");
printf("\n 5唤醒某个进程 6退出系统 \n");
printf("\n*********************************************************\n");
printf("\n请选择(1~6)\n");
scanf("%d", &num);
switch(num)
{
case 1 : create(); break;
case 2 : run(); break;
case 3 : huancun(); break;
case 4 : kill(); break;
case 5 : huanxing(); break;
case 6 : exit(0);
default: n = 0;
}
flag = 0;
}
}
运行结果就是这样,之后我们再来对其进行各种操作,请自行尝试,这里不做过多介绍~