模拟生产者消费者问题

本文通过C++代码实现生产者-消费者问题,利用信号量和互斥锁确保多个生产者与消费者之间的同步访问资源。文章展示了如何使用队列来管理等待的生产者和消费者,并通过随机模拟的方式展示系统的运行过程。
// 操作系统实验一.cpp : 定义控制台应用程序的入口点。
//生产者-消费者问题代码实现


#include "stdafx.h"
#include <queue>
#include   <stdlib.h>
#include <time.h>  
#include <iostream>
using namespace std;


int static pv = 1;     //p,v原语信号量.同一时间只能有一个p或v执行
int static mutex = 1;  //source公用信号量.表示能否访问
int static avail = 5;  //生产者信号量.资源初始化为5,可生产5
int static full = 0;   //消费者信号量.资源初始化为5,可消费5
int static source = 5; //临界区资源.初始化为5,最大为10
string Customer[3] = { "c1","c2","c3" }; //3位消费者
string Producer[3] = { "p1","p2","p3" }; //3位生产者
queue <string> q1;     //身份等待队列
queue <int> q2;        //ID等待队列


void CoutCus()
{
cout << "操作者为 消费者 " << endl;
cout << "生产者信号量avail的值为:" << avail << endl;
cout << "顾客信号量full的值为:" << full << endl;
cout << "资源source的值为:" << source << endl;
cout << endl;
}


void CoutPro()
{
cout << "操作者为 生产者 " <<endl;
cout << "生产者信号量avail的值为:" << avail << endl;
cout << "顾客信号量full的值为:" << full << endl;
cout << "资源source的值为:" << source << endl;
cout << endl;
}


void CoutProWait()
{
cout << "操作者为 生产者 "<< endl;
cout << "进入等待队列..." << endl;
cout << endl;
}


void CoutCusWait()
{
cout << "操作者为 消费者 "<< endl;
cout << "进入等待队列..." << endl;
cout << endl;
}


void lock(int sign)
{
sign = sign - 1;
}


void unlock(int sign)
{
sign = sign + 1;
}


int P(int sign)//p原语
{
while(pv = 0)
{

}


lock(pv);
sign = sign - 1;
if (sign < 0)
{
return 1;
}
unlock(pv);

return 0;
}
void V(int sign)//v原语
{
while(pv = 0) 
{

}

lock(pv);
sign = sign + 1;
/*
if (sign <= 0)
{
q.pop();
return;
}
*/
    unlock(pv);

return;
}


void customerdo(string status,int r)//消费者消费产品
{
int p1,p2;
p1=P(full);
if (p1 == 1)
{
q1.push(status);
q2.push(r);
}


p2=P(mutex);
if (p2 == 1)
{
q1.push(status);
q2.push(r);
}


for (int i = 2000; i >= 0; i--)
{

}
source = source - 1;


V(avail);
V(mutex);
CoutCus();
}




void producerdo(string status,int r)//生产者生产产品
{
int p1, p2;


p1=P(avail);
if (p1 == 1)
{
q1.push(status);
q2.push(r);
}
p2=P(mutex);
if (p2 == 1)
{
q1.push(status);
q2.push(r);
}


for (int i = 1000; i >= 0; i--)
{

}
source = source + 1;
V(full);
V(mutex);
CoutPro();
}


int main()
{
int m = 10;//循环次数
while (m != 0)
{
int i = rand();
srand(i);
int n = rand() % 2;//获取0或1的随机数,模拟消费者或生产者随机访问资源
int r = rand() % 3;//获取0到2的随机数,模拟不同的ID随机访问资源
string cus;
string pro;


if (n == 0)
{
cus = Customer[r];
}
else
{
pro = Producer[r];
}


if (n == 0)
{
if (q1.empty()==false)
{
q1.push(cus);//如果等待队列不为空,新来的顾客进入等待队列
q2.push(r);//顾客ID进入等待队列
CoutCusWait();


string pro2=q1.front();
int r2 = q2.front();
cout << "消费者就绪"<<endl;
customerdo(pro2,r2);


q1.pop();
q2.pop();
}
else 
{
customerdo(pro,r);
}
}
else
{
if (q1.empty() == false)
{
q1.push(pro);
q2.push(r);
CoutProWait();


string pro2 = q1.front();
int r2 = q2.front();
cout << "生产者就绪" << endl;
producerdo(pro2, r2);


q1.pop();
q2.pop();
}
else
{
producerdo(pro,r);
}
}
m--;
}

getchar();
getchar();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值