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

📄 pro1.cpp

📁 这是上操作系统课的时候上实验学习生产者那一部分,这个源代码是模拟的生产者那部分.
💻 CPP
字号:
// pro1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "iostream.h"
#include "stdlib.h"
#define OK 1
#define ERROR 0
#define BUFFSIZE 2
struct PCB
{
	int flag;//1:生产者;0:消费者
	int state;//1:就绪;0:等待
	char name[10];//名字
};
typedef struct QNode
{//定义队列的结点的结构
	PCB   data;
	struct QNode  *next;
}QNode, *QueuePtr;
typedef struct 
{//定义队列的结构
	QueuePtr  front;            //队头指针   
	QueuePtr  rear;             //队尾指针
}LinkQueue; 
typedef struct 
{//定义链表的结构
	QueuePtr  head;            //链表指针   
	int length;             //队尾指针
}Linklist;   
int Initlist (Linklist &L)
{//初始化链表
	L.head=(QueuePtr) malloc(sizeof(QNode)); 
    if(!L.head) exit(0);
    L.head->next=NULL;
	L.length=0;
    return OK;
}
int EnList(Linklist &L,QueuePtr p)
{//入队
	QueuePtr q;
    q=(QueuePtr)malloc(sizeof(QNode));
    if(!q) 
		exit(0);
	q=L.head->next;
	L.head->next=p;
	p->next=q;
    return OK;
}
int InitQueue (LinkQueue &Q)
{//初始化队列
	Q.front=Q.rear=(QueuePtr) malloc(sizeof(QNode)); 
    if(!Q.front) exit(0);
    Q.front->next=NULL;
    return OK;
}
int EnQueue(LinkQueue &Q,PCB e)
{//入队
	QueuePtr p;
    p=(QueuePtr)malloc(sizeof(QNode));
    if(!p) exit(0);
    p->data=e; p->next=NULL;
    Q.rear->next=p;
    Q.rear=p;
    return OK;
}
int DeQueue (LinkQueue &Q,PCB &e)
{//出队
	QueuePtr p;
    p=(QueuePtr)malloc(sizeof(QNode));
	if(Q.front==Q.rear) return ERROR;
	p=Q.front->next;
	e=p->data;
	Q.front->next=p->next;
	if(Q.rear==p) Q.rear=Q.front;
	free(p);
	return OK;
}
int create(LinkQueue &Q)
{
	int a,i;
	system("cls");
	cout<<endl<<endl<<endl<<endl;
	cout<<"      **************************************************"<<endl;
	cout<<"      *              ^-^ 欢迎使用此系统    ^-^         *    "<<endl;
	cout<<"      *                                                *    "<<endl;
	cout<<"      **************************************************"<<endl;
	cout<<"          请输入您需要的进程数(请不要输入过大):";
	cin>>i;
	for(a=0;a<i;a++)
	{
		PCB e;
		cout<<"请输入第"<<a+1<<"个进程的名字:"<<endl;
		cin>>e.name;
		cout<<endl<<"请输入这个进程是生产者还是消费者(1为生产者,0为消费者):"<<endl;
		cin>>e.flag;
		e.state=1;//新产生的是就绪队列,所以开始状态是1(就绪)
		EnQueue(Q,e);
	}
	cout<<"就绪队列创建成功,它是:"<<endl;
	QueuePtr p;
	p=(QueuePtr)malloc(sizeof(QNode));
	p=Q.front->next;
	while(p!=NULL)
	{
		cout<<p->data.name<<"    ";
		if(p->data.flag==1)
			cout<<"生产者;"<<endl;
		else
			cout<<"消费者;"<<endl;
		p=p->next;
		
	}
	return OK;
}
void display(LinkQueue &Q)
{
	QueuePtr p;
	p=(QueuePtr)malloc(sizeof(QNode));
	p=Q.front->next;
	while(p!=NULL)
	{
		cout<<p->data.name<<"    ";
		p=p->next;
		
	}
}
int main(int argc, char* argv[])
{
	char c;
	do
	{
		int productnum=0;//已经生产的产品的数目
		int full=0;//缓存是否满的信号,若为0,则未满,若为1,则已满
		int empty=0;//缓存是否为空的信号,若为0,则空,若为BUFFSIZE,则已满.开始时无资源,所以将其置为空
		char buff[BUFFSIZE];
		int buffpoint=0;//缓存区的指针
		PCB e;
		QueuePtr p;
		p=(QueuePtr)malloc(sizeof(QNode));
		LinkQueue ready,waitcustomer,waitproducer;//产生就绪队列,生产者队列,消费者队列
		InitQueue (ready);
		InitQueue (waitcustomer);
		InitQueue (waitproducer);
		Linklist over;//产生执行链表
		Initlist (over);
		create(ready);//生成就绪队列
		p=ready.front->next;
		while(p!=NULL)
		{
			cout<<endl<<p->data.name<<"申请运行,它是一个";
			if(p->data.flag==1)
			{
				cout<<"生产者";
				if(full==0)
				{//若缓存不为满,则让生产者生产物品放入缓存
					cout<<endl<<"批准申请";
					buff[buffpoint]='b';//置为b,意为放入了资源
					buffpoint++;
					empty++;//生产了一个资源,所以 empty+1
					if(buffpoint==BUFFSIZE)
						full=1;//若buff已经满,则full=1
					DeQueue (ready,e);//从就绪队列中取出;
					QueuePtr q;
					q=(QueuePtr)malloc(sizeof(QNode));
					q->data=e;
					EnList(over,q);//放入已执行链表
				}
				else
				{
					cout<<endl<<"缓存已满,进入生产等待队列等待;";
					DeQueue (ready,e);//从就绪队列中取出;
					e.state=0;//等待
					EnQueue(waitproducer,e);//进入生产等待队列
				}
			}
			else
			{
				cout<<"消费者";
				if(empty!=0)
				{//若缓存不为空,则让消费者取资源
					cout<<endl<<"批准申请";
					buff[buffpoint]='a';//把b置为a,意为取出资源
					buffpoint--;
					empty--;//使用了一个资源,所以 empty+1
					if(buffpoint<BUFFSIZE)
						full=0;//buff未满
					DeQueue (ready,e);//从就绪队列中取出;
					QueuePtr q;
					q=(QueuePtr)malloc(sizeof(QNode));
					q->data=e;
					EnList(over,q);//放入已执行链表
				}
				else
				{
					cout<<endl<<"缓存为空,进入消费等待队列等待;";
					DeQueue (ready,e);//从就绪队列中取出;
					e.state=0;//等待
					EnQueue(waitcustomer,e);//进入消费等待队列
				}
			}
			if(full!=1&&waitproducer.front!=waitproducer.rear)
			{//缓存不为满且生产者等待队列不为空时让生产者等待队列队头的进程进入到就绪队列中
				DeQueue (waitproducer,e);//从生产者等待队列中取出;
				cout<<endl<<"由于有了空间"<<e.name<<"进入就绪队列";
				e.state=1;//再次就绪
				EnQueue(ready,e);//进入就绪队列
			}
			if(empty!=0&&waitcustomer.front!=waitcustomer.rear)
			{//缓存不为空且消费者等待队列不为空时让消费者等待队列队头的进程进入到就绪队列中
				DeQueue (waitcustomer,e);//从消费者等待队列中取出;
				cout<<endl<<"由于有了资源"<<e.name<<"进入就绪队列";
				e.state=1;//再次就绪
				EnQueue(ready,e);//进入生产等待队列
			}
			p=ready.front->next;//再让p指向队头进程
		}
		cout<<endl<<"就绪队列没有进程";
		if(waitcustomer.front!=waitcustomer.rear)
		{
			cout<<endl<<"消费者等待队列中有进程:"<<endl;
			display(waitcustomer);
		}
		else
			cout<<endl<<"消费者等待队列中没有进程:"<<endl;
		if(waitproducer.front!=waitproducer.rear)
		{
			cout<<endl<<"生产者等待队列中有进程:"<<endl;
			display(waitproducer);
		}
		else
			cout<<endl<<"生产者等待队列中没有进程:"<<endl;
		cout<<endl<<endl<<"      还要继续再试一次么?(Y/N)";
		cin>>c;
	}while(c=='Y'||c=='y');
	system("cls");
	cout<<endl<<endl<<endl<<endl;
	cout<<"      **************************************************"<<endl;
	cout<<"      *                ^-^   谢谢使用    ^-^           *    "<<endl;
	cout<<"      *                                                *    "<<endl;
	cout<<"      *                                                *    "<<endl;
	cout<<"      *                                                *    "<<endl;
	cout<<"      *            信息05-2   伊冰静   050814226       *"<<endl;
	cout<<"      **************************************************"<<endl;
	return 0;
}

⌨️ 快捷键说明

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