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

📄 test2.cpp

📁 模拟进程管理
💻 CPP
字号:
// test2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream.h>
#include<malloc.h>

typedef struct node
{
	char data;
	struct node *next;

} linklist;

linklist *c,*b;
char n=NULL;

void ShowMenu()
{
	cout<<endl;
	cout<<"●◎★欢迎使用——张皓轶★◎● "<<endl;
	cout<<endl;
	cout<<endl;
	cout<<"◎●◎●◎●◎●◎●◎●◎●◎●◎●◎●\n★s--查询状态\n◎●◎●◎●◎●◎●◎●◎●◎●◎●◎●\n★f--查询命令\n◎●◎●◎●◎●◎●◎●◎●◎●◎●◎●\n★c--创建进程\n◎●◎●◎●◎●◎●◎●◎●◎●◎●◎●\n★e--结束进程\n◎●◎●◎●◎●◎●◎●◎●◎●◎●◎●\n★b--进程阻塞\n◎●◎●◎●◎●◎●◎●◎●◎●◎●◎●\n★w--唤醒进程\n◎●◎●◎●◎●◎●◎●◎●◎●◎●◎●\n★p--调度进程\n◎●◎●◎●◎●◎●◎●◎●◎●◎●◎●\n★t--时间片到\n◎●◎●◎●◎●◎●◎●◎●◎●◎●◎●\n★q----退出登录!\n◎●◎●◎●◎●◎●◎●◎●◎●◎●◎●"<<endl;
	cout<<endl;
}

char Menu()
{   
	char choose;
	while (1) 
	{
		cout << "请输入字母(s/f/c/e/b/w/p/t/q): ";
		cin>>choose;
		cout<<endl;
		switch (choose) 
		{
        case 's':
		case 'f':
		case 'c':
		case 'e':
		case 'b':
		case 'w':
		case 'p':
		case 't':
		case 'q':
			return choose;
			break;
		default:
			cout << "您输入的字符不在字母(s/f/c/e/b/w/p/t/q)之间,请重新输入!" << endl;
			break;
		}
	}
	return -1;

}

linklist *Holdlinklist()
{
	linklist *head;
	head=(linklist*)malloc(sizeof(linklist));
	head->next=NULL;
	return head;
}

linklist *Addlinklist(linklist *head,linklist *p)
{

	linklist *r;
	r=head;	
	for(;r->next!=NULL;)
	{	
		r=r->next;	    	
	}
	r->next=p;
	r=p;
    r->next=NULL;
	return head;
}

void Showlinklist(linklist *head)
{
	void xun();
	void begin();
	linklist *p;
	p=head;
	while(p->next!=NULL)
	{
		cout<<"("<<p->next->data<<")"<<"  ";
		p=p->next;
	}
	cout<<endl;
	xun();
	
}

void Showlinklist1(linklist *head)
{
	void xun();
	void begin();
	linklist *p;
	p=head;
	while(p->next!=NULL)
	{
		cout<<"("<<p->next->data<<")"<<"  ";
		p=p->next;
	}
	cout<<endl;
}

void Create(linklist *head)
{
	char t;
	cout<<"请输入您要使用的进程名:"<<endl;
	linklist *p;
	p=(linklist*)malloc(sizeof(linklist));
	cin>>t;
	p->data=t;
    Addlinklist(head,p);
	cout<<"现在,我们共有如下就绪进程:";
	Showlinklist(head);
}

char Play(linklist *head)
{
	linklist *p;
	p=(linklist*)malloc(sizeof(linklist));
	p=head;
	char n;
	n=p->next->data;
	cout<<"现在运行的进程为:"<<"("<<n<<")"<<endl;
	return n;
}

void Block(linklist *head,char n)
{
	linklist *p;
	p=(linklist*)malloc(sizeof(linklist));
	p->data=n;
	Addlinklist(head,p);
	cout<<"现在,我们共有如下阻塞进程:";
    Showlinklist1(head);
}

void begin()
{	
	ShowMenu();
	void xun();
	c=Holdlinklist(); 
	b=Holdlinklist();
	xun();
}

void xun()
{
	char choice=Menu();
	while(1)
	{
		if (choice=='q')
		{
			cout<<"感谢使用!"<<endl;
			break;
		}
		else if(choice=='f')
		{
			ShowMenu();
			xun();
		}
     	else if(choice=='c')
			Create(c);
		else if(choice=='p')
		{	
			if(n!=NULL)
			{
				cout<<"已经有正在运行的进程了,请停止后再运行新的进程。"<<endl;
				xun();
			}
			if(c->next ==NULL)
			{
				cout<<"没有就绪态进程,请确认。"<<endl;
				xun();
			}
			n=Play(c);
			c=c->next;
			xun();
		}
		else if(choice=='b')
		{
			if(n==NULL)
			{
				cout<<"没有进程执行,请按‘p’运行。"<<endl;
				xun();
			}
			Block(b,n);
			if(c->next ==NULL)
			{
				cout<<"没有就绪态进程,请确认。"<<endl;
				xun();
			}
			n=Play(c);
			c=c->next;
			xun();
		}
		else if(choice=='e')
		{
			if(n==NULL)
			{
				cout<<"没有进程执行,请按‘p’运行。"<<endl;
				xun();
			}
			cout<<"进程"<<"("<<n<<")"<<"已被停止。"<<endl;
			if(c->next ==NULL)
			{
				cout<<"没有就绪态进程,请确认。"<<endl;
				xun();
			}
			n=Play(c);
		    c=c->next;
			xun();
		}
		else if(choice=='w')
		{
/*			if(n==NULL)
			{
				n=Play(b);
			    xun();	
			}*/
	    	if(b->next==NULL)
			{
				cout<<"没有阻塞态的进程,无法唤醒,请确认。"<<endl;
				xun();
			}
			linklist *p;
	        p=(linklist*)malloc(sizeof(linklist));
			p->data=n;
			Addlinklist(c,p);
			n=Play(b);
			b=b->next;
			xun();
		}
		else if(choice=='t')
		{
			if(n==NULL)
			{
				cout<<"没有进程执行,请按‘p’运行。"<<endl;
				xun();
			}
			linklist *p;
			p=(linklist*)malloc(sizeof(linklist));
			p->data=n;
			Addlinklist(c,p);
			if(c->next ==NULL)
			{
				cout<<"没有就绪态进程,请确认。"<<endl;
				xun();
			}
			n=Play(c);
			c=c->next;
			xun();
		}
		else if(choice=='s')
		{
			if(n==NULL)
				cout<<"无运行的进程。"<<endl;
			else
			    cout<<"现在运行的进程为:"<<"("<<n<<")"<<endl;
	    	if(b->next==NULL)
				cout<<"没有阻塞态的进程."<<endl;
			else
			{
				cout<<"现在,我们共有如下阻塞进程:";
                Showlinklist1(b);
			}
			if(c->next ==NULL)
			{
				cout<<"没有就绪态进程。"<<endl;
				xun();
			}
			else
			{
	            cout<<"现在,我们共有如下就绪进程:";
	            Showlinklist(c);
			}
		}
	}
}

int main(int argc, char* argv[])
{
	begin();
	return 0;
}

⌨️ 快捷键说明

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