⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 005.cpp

📁 计算机操作系统模拟进程管理源代码
💻 CPP
字号:
#include<iostream.h>
#include<fstream>
#include<string.h>
#include "Process.h"
int i=0,t=0;
struct information 
{
	char name[10];
	int Id;
	int Number;
	int num;
};
information Freelist[100];
information Runlist[1];
information Blocklist[100];
void Check()
{
	if(Freelist[0].Id!=0&&Runlist[0].Id==0)
	{
		if(Freelist[1].Id==0)
		{
			strcpy(Runlist[0].name,Freelist[0].name);
			Runlist[0].Id=Freelist[0].Id;
            strcpy(Freelist[0].name,Freelist[1].name);
			Freelist[0].Id=Freelist[1].Id;
			i--;
		}
	else
	{
   	for(int j=0;j<i;j++)
	{
		if(Freelist[j].Id>=Freelist[j+1].Id)
		{
			strcpy(Runlist[0].name,Freelist[j+1].name);
			Runlist[0].Id=Freelist[j+1].Id;
/*			for(int j2=j+1;j2<i;j2++)
			{
               strcpy(Freelist[j2].name,Freelist[j2+1].name);
			   Freelist[j2].Id=Freelist[j2+1].Id;
			}*/
			i--;
		}
		else
		{
			strcpy(Runlist[0].name,Freelist[j].name);
			Runlist[0].Id=Freelist[j].Id;
			for(int j3=j;j3<i;j3++)
			{
               strcpy(Freelist[j3].name,Freelist[j3+1].name);
			   Freelist[j3].Id=Freelist[j3+1].Id;
			}
            i--;
		}
	}
	}
	}
	else 
	{
		cout<<"就绪态进程为空!"<<endl;
	}
}
/*void change()
{
	if(Freelist[0].Id!=0)
	{
		if(Freelist[1].Id==0)
		{
			strcpy(Runlist[0].name,Freelist[0].name);
			Runlist[0].Id=Freelist[0].Id;
			i--;
		}
		else{
   	       for(int j=0;j<i;j++)
		   {
		      if(Freelist[j].Id>=Freelist[j+1].Id)
			  {
			strcpy(Runlist[0].name,Freelist[j+1].name);
			Runlist[0].Id=Freelist[j+1].Id;
/*			for(int j2=j+1;j2<i;j2++)
			{
               strcpy(Freelist[j2].name,Freelist[j2+1].name);
			   Freelist[j2].Id=Freelist[j2+1].Id;
			}
			i--;
		}
		else
		{
			strcpy(Runlist[0].name,Freelist[j].name);
			Runlist[0].Id=Freelist[j].Id;
			for(int j3=j;j3<i;j3++)
			{
               strcpy(Freelist[j3].name,Freelist[j3+1].name);
			   Freelist[j3].Id=Freelist[j3+1].Id;
			}
            i--;
		}
	}
		}
	}
	else 
	{
		cout<<"就绪态进程为空!"<<endl;
	}
}*/
void Create_Process()
{
	Check();
	Process p;
	char Name[10];
	int id;
    cout<<"现在开始创建进程:"<<endl;
	cout<<"新进程名为:"<<endl;
	cin>>Name;
	p.setName(Name);
	cout<<"所需内存为:"<<endl;
	cin>>id;
	p.setID(id);
	if(Runlist[0].Id==0)
		{
			cout<<"该进程已被自动调度"<<endl;
			strcpy(Runlist[0].name,Name);
			Runlist[0].Id=id;
		}
	else 
	{
	strcpy((char*)Freelist[i].name,Name);
	Freelist[i].Id=id;
	Freelist[i].num-=id;
    Freelist[i].Number++;
	cout<<"名为:"<<p.getName()<<"的新进程创建完毕!"<<endl;
	cout<<"此进程为你创建的第"<<Freelist[i].Number<<"个进程。"<<endl;
    cout<<"大小为:"<<p.getID()<<"k"<<endl;
    cout<<"剩余空间为:"<<Freelist[i].num<<"k"<<endl;
	cout<<"请继续选择其他项:"<<endl;
	Freelist[i+1].num=Freelist[i].num;
    Freelist[i+1].Number+=Freelist[i].Number;
	i++;}
}
int Judge1()
{
        int ch;
		while (1) {
		cout << "请选择:(1--阻塞/0--不阻塞) ";
		cin >> ch;
		switch (ch) {
        case 1:
		case 0: 
			return ch;
			break;
		default:
			cout << "您输入错误,请重新输入!" << endl;
			break;
		}
	}
	return -1;
}
void Over_Process()
{
  	char name[10];
   	cout<<"阻塞态列表:"<<endl;
	if(Blocklist[0].Id!=0)
	{
	for(int j=0;j<t;j++)
	{
		cout<<Blocklist[j].name<<"("<<Blocklist[j].Id<<"k)"<<endl;
	}
	cout<<"请输入你要结束的进程名:"<<endl;
	cin>>name;
	for(int j1=0;j1<t;j1++)
	{
		if(strcmp(Blocklist[j1].name,name)==0)
		{
			cout<<"第"<<j1<<"个找到此进程!"<<endl;
			cout<<"正在结束......"<<endl;
			for(int j2=j1;j2<t;j2++)
			{
               strcpy(Blocklist[j2].name,Blocklist[j2+1].name);
			   Blocklist[j2].Id=Blocklist[j2+1].Id;
			}
			t--;
			cout<<"名为:"<<name<<"的进程结束完毕"<<endl;
		}
		else 
		{
			cout<<"第"<<j1<<"个位置没有找到此进程!"<<endl;
		}
	}
	}
	else {cout<<"不存在!"<<endl;}	

}
void Weakup_Process()
{
	char name[10];
   	cout<<"阻塞态列表:"<<endl;
	if(Blocklist[0].Id!=0)
	{
	for(int j=0;j<t;j++)
	{
		cout<<Blocklist[j].name<<"("<<Blocklist[j].Id<<"k)"<<endl;
	}
	cout<<"请输入你要唤醒的进程名:"<<endl;
	cin>>name;
	for(int j1=0;j1<=t;j1++)
	{
		if(strcmp(Blocklist[j1].name,name)==0)
		{
			cout<<"第"<<j1<<"个位置找到此进程!"<<endl;
			cout<<"正在唤醒......"<<endl;
            strcpy(Freelist[i].name,Blocklist[j1].name);
			Freelist[i].Id=Blocklist[j1].Id;
			Check();
			i++;
			cout<<"名为:"<<name<<"的进程唤醒完毕"<<endl;
			for(int j2=j1;j2<t;j2++)
			{
               strcpy(Blocklist[j2].name,Blocklist[j2+1].name);
			   Blocklist[j2].Id=Blocklist[j2+1].Id;
			}
			Check();
			t--;

		}
		else {cout<<"第"<<j1<<"位置没有找到此进程!"<<endl;}
	}
	}
	else {cout<<"不存在!"<<endl;}
}
void TimeOver()
{	
	if(Freelist[0].Id!=0)
	{
		if(Freelist[1].Id==0)
		{
			strcpy(Runlist[0].name,Freelist[0].name);
			Runlist[0].Id=Freelist[0].Id;
			i--;
		}
		else{
   	       for(int j=0;j<i;j++)
		   {
		      if(Freelist[j].Id>=Freelist[j+1].Id)
			  {
			strcpy(Runlist[0].name,Freelist[j+1].name);
			Runlist[0].Id=Freelist[j+1].Id;
/*			for(int j2=j+1;j2<i;j2++)
			{
               strcpy(Freelist[j2].name,Freelist[j2+1].name);
			   Freelist[j2].Id=Freelist[j2+1].Id;
			}*/
			i--;
		}
		else
		{
			strcpy(Runlist[0].name,Freelist[j].name);
			Runlist[0].Id=Freelist[j].Id;
			for(int j3=j;j3<i;j3++)
			{
               strcpy(Freelist[j3].name,Freelist[j3+1].name);
			   Freelist[j3].Id=Freelist[j3+1].Id;
			}
            i--;
		}
		   }
		}cout<<"执行完毕!"<<endl;
	}
	else 
	{
		cout<<"就绪态进程为空!"<<endl;
	}
}
void Block_Process()
{
	if(Runlist[0].Id!=0)
	{
	cout<<"名为:"<<Runlist[0].name<<"的进程正在运行,是否阻塞:"<<endl;
	int choose1=Judge1();
	if(choose1==1){
		         cout<<"正在阻塞进程:"<<endl;
		         strcpy(Blocklist[t].name,Runlist[0].name);
                 Blocklist[t].Id=Runlist[0].Id;
				 Runlist[0].Id=0;
		         t++;
		         Check();
	             cout<<"名为:"<<Blocklist[t-1].name<<"的进程阻塞完毕!"<<endl;}
	if(choose1==0){}
	}
	else {cout<<"无进程处于执行态!"<<endl;}
	cout<<"请继续选择其他项:"<<endl;
}
char Choose()
{
		char ch;
		while (1) {
        cout<<"请输入你的请求:";
		cin >> ch;
		switch (ch) {
        case 'c':
		case 'e':
		case 'b':
		case 'w': 
		case 't':
		case 's': 
		case 'o': 
			return ch;
			break;
		default:
			cout<<"警告:无此项请求!请重新选择!"<<endl;
			break;
		}
	}
	
}
void Smallmenu()
{	
	cout<<"帮助目录:"<<endl;
	cout<<"   ****************";
	cout << "\n\n    C——创建进程";
	cout << "\n    E——结束进程";
	cout << "\n    B——进程阻塞";
	cout << "\n    W——唤醒进程";
	cout << "\n    T——时间片到";
	cout << "\n    S——显示状态";
	cout << "\n    O——退出\n\n";
	cout<<"   ****************"<<endl;
}
void Show()
{
	cout<<"就绪态列表:"<<endl;
	if(Freelist[0].Id!=0)
	{
		for(int j=0;j<i;j++)
	{
		cout<<Freelist[j].name<<"("<<Freelist[j].Id<<"k)"<<endl;
	}
	}
	else {cout<<"不存在!"<<endl;}
	cout<<"执行态列表:"<<endl;
	if(Runlist[0].Id!=0)
	{
	cout<<Runlist[0].name<<"("<<Runlist[0].Id<<"k)"<<endl;
	}
	else {cout<<"不存在!"<<endl;}
	cout<<"阻塞态列表:"<<endl;
	if(Blocklist[0].Id!=0)
	{
	for(int j=0;j<t;j++)
	{
		cout<<Blocklist[j].name<<"("<<Blocklist[j].Id<<"k)"<<endl;
	}
	}
	else {cout<<"不存在!"<<endl;}
}
void JudgeChoose()
{
	Smallmenu();
	char choose=Choose();
	if(choose=='c'){Create_Process();JudgeChoose();}
	if(choose=='e'){Over_Process();JudgeChoose();}
	if(choose=='b'){Block_Process();JudgeChoose();}
	if(choose=='w'){Weakup_Process();JudgeChoose();}
	if(choose=='t'){TimeOver();JudgeChoose();}
	if(choose=='s'){Show();JudgeChoose();}
	if(choose=='o'){cout<<"欢迎下次使用!"<<endl;}
}
void main()
{
	Runlist[0].Id=20;
	strcpy(Runlist[0].name,"123");
	Freelist[i].num=100;
	Freelist[i].Number=0;
	cout<<"**********"<<endl;
    cout<<"模拟进程控制(本系统自动化调度进程,初始化一进程正在执行)"<<endl;
	cout<<"**********"<<endl;
    JudgeChoose();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -