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

📄 实验4源程序.txt

📁 操作系统中进程之间的通信
💻 TXT
字号:
#include<iostream>
#include<string>
//#include<time.h>
using namespace std;

class node
{
	friend class tongb;
private:
	node *next;
public:
	string name;
	string state;
	string cause;
	int bpoint;
	node(node *next=NULL){};
    node(string na,string st,string ca,int bp)
	{name=na;state=st;cause=ca;bpoint=bp;next=NULL;}
    ~node(){};
};


class tongb
{
private:
	node *head,*pcurrent;//头指针和当前指针
	string str;//用户输入的字符串,生产者每次从字符串中读取一个字符
	int k;//计数输入的字符
	int s1,s2;//信号量
	int pa[5];//生产者程序入口地址
	int sa[5];//消费者程序入口地址
	char b[10];//缓冲区
	int in,out;
	int pc;//指令计数器
	char product;//存放生产者读入的字符
	char X;//存放消费者读出的字符
public:
	tongb(){head=new node();head->next=NULL;in=out=k=0;}
	~tongb(){};
	void insert(string na,string st,string ca,int bp)
	{
		if(head->next==NULL)
		{
			head->next=new node(na,st,ca,bp);
		}
		else
		{
			node *q=new node(na,st,ca,bp);
			node *p=head->next;
			while(p->next)
			{
				p=p->next;
			}
			p->next=q;
		}
	}
	void print()
	{
		cout<<"------PCB------\n";
		cout<<"进程名\t"<<"状态\t"<<"等待原因  "<<"断点\n";
		node *p=head->next;
		while(p)
		{
			cout<<p->name<<"\t"<<p->state<<"\t"<<p->cause<<"\t  "<<p->bpoint<<endl;
			p=p->next;
		}
	}

	void p(int s)
	{
		
		s--;
		if(s<0)
		{
			pcurrent->state="等待态";
			if(pcurrent->name=="生产者"){pcurrent->cause="s1";}
			else { pcurrent->cause="s2";}
		
		}
		else
		{
			pcurrent->state="就绪态";
			
		}
		if(pcurrent->name=="生产者"){cout<<"正执行p(s1)操作...\n";}
		else {cout<<"正执行p(s2)操作...\n";}
	}
	void v(int s)
	{
		s++;
		if(s>=0)
		{
			node *p;
			if(pcurrent->name=="生产者")
			{
				p=head->next->next;
				if(p->cause=="s2"){p->state="就绪态";p->cause="无";}
			}
			else
			{
				p=head->next;
				if(p->cause=="s1"){p->state="就绪态";p->cause="无";}
			}
		}
		
		pcurrent->state="就绪态";
		if(pcurrent->name=="生产者"){cout<<"正执行v(s2)操作...\n";}
		else {cout<<"正执行v(s1)操作...\n";}
		
	}
	void put(){cout<<"正执行put()操作...\n"; b[in]=product;in=(in+1)%10;}
	void get(){cout<<"正执行get()操作...\n";X=b[out];out=(out+1)%10;}
	void produce(){cout<<"正执行produce()操作...\n";product=str[k];k++;cout<<"生产者读入字符:"<<product<<endl;}
	void consume(){cout<<"正执行consume()操作...\n";cout<<"消费者读出字符:"<<X<<endl;}
	void Goto(int L){cout<<"正执行Goto()操作...\n";pc=L;}
	void nop(){}
	void moning()//模拟处理器指令执行程序
	{
		int j=pc;
		if(pcurrent->name=="生产者")
		{
			j=pa[pc];
		}
		else
		{
			j=sa[pc];
		}
		pc++;
		switch(j){
		case 0:produce();pcurrent->state="就绪态";break;
		case 1:p(s1);break;
		case 2:put();pcurrent->state="就绪态";break;
		case 3:v(s2);break;
		case 4:Goto(0);pcurrent->state="就绪态";break;
	    case 10:p(s2);break;
		case 11:get();pcurrent->state="就绪态";break;
        case 12:v(s1);break;
		case 13:consume();pcurrent->state="就绪态";break;
		case 14:Goto(0);pcurrent->state="就绪态";break;
		default:cout<<"error\n";
		}
		print();
		cout<<"\n\n生产者运行结束?是按y或Y,否按任意键继续:";
		char a;
		cin>>a;
		if(a=='y'||a=='Y')
		{
			head->next->state="完成态";
		}
		
	}
	void cpu()//处理器调度程序
	{
		while(1)
		{
		pcurrent->bpoint=pc;
		
		node *p=head->next;
		node *q=p->next;
		if(p->state=="就绪态"&&q->state=="就绪态")
		{
			//srand(time(NULL));
		    //int n=rand()%2;
			int n;
			cout<<"输入1运行生产者进程,选择0运行消费者进程:";
			cin>>n;
			if(n==1)
			{
				p->state="运行态";pcurrent=p;pc=p->bpoint;
			    cout<<"\ncpu选择生产者运行\n";
				moning();
			}
			else
			{
				q->state="运行态";pcurrent=q;pc=q->bpoint;
				
				cout<<"\ncpu选择消费者运行\n";
				moning();
			}
		}
		else if(p->state=="就绪态"&&q->state!="就绪态")
		{
				p->state="运行态";pcurrent=p;pc=p->bpoint;
			    cout<<"\ncpu选择生产者运行\n";
				moning();
		}
		else if(p->state!="就绪态"&&q->state=="就绪态")
		{
			    q->state="运行态";pcurrent=q;pc=q->bpoint;
				
				cout<<"\ncpu选择消费者运行\n";
				moning();

		}
		else return;
		}

	}
	void start()//初始化程序
	{
		
		s1=10;s2=0;
		insert("生产者","就绪态","无",0);
		insert("消费者","就绪态","无",0);
		print();
		string s;
		cout<<"\n输入一组字符串:";
		cin>>s;
		str=s;
		pcurrent=head->next;
		pc=0;
		for(int i=0;i<5;i++)
		{
			pa[i]=i;
		}
		for(int j=0;j<5;j++)
		{
			sa[j]=j+10;
		}
		
		cpu();
	}
	};
	void main()
	{
		tongb p;
		p.start();
	}



⌨️ 快捷键说明

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