#include "stdafx.h"
#include "myhead.h"
#include "iostream"
#include <vector>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
using namespace std;
CWinApp theApp;
#define MAX 10
char buff[MAX]={0};
int in = 0;
int out = 0;
int full = 0;
int empty = MAX;
char PRODECT[MAX] = {'A','B','C','D','E','F','G','H','I'};
vector< int > block_queue; //生产阻塞队列
vector< int > block_queue2; //消费阻塞队列
void ShowBuff()
{
cout << endl;
for(int i =0; i< MAX; i++)
{
cout<<"buff["<<i<<"]: "<<buff[i] <<endl;
}
}
int Producter(int n)
{
if( empty >= 0 ) //wait(empty)
{
cout<<"I am Producter "<< " ! ...";
empty--;
buff[in] = PRODECT[ rand() % MAX ];
cout<<"Producting..."<<buff[in]<<" at "<<in<<endl;
in= ( in+1 ) % 10;
full++; //=singal(full)]
if( block_queue2.size() > 0 ) block_queue2.pop_back() ;
return -1;
}
else
{
cout<<"Buff is full..."<<"Producter "<<" Waiting...! "<<endl;
block_queue.push_back(n); //
return n;
}
}
int Comsumer(int n)
{
if( full>0 ) //what(full)
{
cout<<"I am Consumer"<<" ! ... ";
full--;
cout<<"Consuming..."<<buff[out]<<" at "<<out<< endl; ;
buff[ out ] = 0 ;
out= ( out + 1 ) % MAX;
empty++; //=singal(empty)
if( block_queue.size() > 0 ) block_queue.pop_back() ;
return -1;
}
else
{
cout<<"Buff is empty..."<<"Consumer"<<" Waiting..." << endl;
block_queue2.push_back(n);
return n;
}
}
int main(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nCount = 0;
while(nCount < 0x1f) // f为循环次数
{
int n= rand() % 7; //生成随机数值
switch(n)
{
case 0: Producter(n) ;
break;
case 1: Comsumer(n) ;
break;
case 2: Producter(n);
break;
case 3: Producter(n);
break;
case 4: Comsumer(n) ;
break;
case 5:Producter(n);
case 6:
Producter(n) ;
default:
break;
}
Sleep(10); //调整显屏速度,执行挂起一段时间
nCount++;
ShowBuff();
cout<<"生产阻塞队列:";
for(int i=0;i< block_queue.size(); i++ )
{
cout<<block_queue[i]<<"\t";
}
cout<<endl<<"消费阻塞队列:";
for(int j=0;j< block_queue2.size(); j++ )
{
cout<<block_queue2[j ]<<"\t";
}
cout<<endl;
}
system("pause");
return 0;
}
四散
- 粉丝: 65
- 资源: 1万+
最新资源
- LCD1602电子时钟程序
- 西北太平洋热带气旋【灾害风险统计】及【登陆我国次数评估】数据集-1980-2023
- 全球干旱数据集【自校准帕尔默干旱程度指数scPDSI】-190101-202312-0.5x0.5
- 基于Python实现的VAE(变分自编码器)训练算法源代码+使用说明
- 全球干旱数据集【标准化降水蒸发指数SPEI-12】-190101-202312-0.5x0.5
- C语言小游戏-五子棋-详细代码可运行
- 全球干旱数据集【标准化降水蒸发指数SPEI-03】-190101-202312-0.5x0.5
- spring boot aop记录修改前后的值demo
- 全球干旱数据集【标准化降水蒸发指数SPEI-01】-190101-202312-0.5x0.5
- ActiveReports
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈