生产者和消费者
nepu的恶臭作业
参考: link. link.
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <vector>
#include <string>
#include <windows.h>
#include <sstream>
#include <random>
#include <Queue>
#define N 3
#define processNum 50
using namespace std;
class PCB;
PCB* wakeup(queue<PCB*>*& list);
PCB* block(queue<PCB*>*& list, PCB* pcb);
int food = 0;
typedef struct semaphore
{
int value;
queue<PCB*>* list;
}semaphore;
queue<PCB*> producerCongestionQueue;
queue<PCB*> consumerCongestionQueue;
queue<PCB*> shareCongestionQueue;
int mutex_c = 1;
int full_c = 0;
int empty_c = N;
semaphore Empty = { N, &producerCongestionQueue };
semaphore Full = { 0, &consumerCongestionQueue };
semaphore Mutex = { 1, &shareCongestionQueue };
class PCB {
public:
string name;
int code;
int pc;
int time;
int state;
char reason;
int roleFlag;
PCB() = default;
static PCB* CreatePcb(char* name) {
PCB* p = (PCB*)malloc(sizeof(PCB));
if (name != NULL) {
p->name = name;
}
else {
p->name = "default";
}
return p;
}
void wait(semaphore* S) {
S->value--;
if (S->value >= 0)
{
if (this->roleFlag == 1) {
if (this->pc == 2)
{
cout << this->name << " 成功,生产者申请获取缓冲池,Empty值满足条件" << " Empty值" << Empty.value << endl;
}
else if (this->pc == 3)
{
cout << this->name << " 成功,生产者申请获取缓冲池,Mutex值满足条件" << " Mutex值" << Mutex.value << endl;
}
}
else if (this->roleFlag == 0)
{
if (this->pc == 1)
{
cout << this->name << " 成功,消费者申请获取缓冲池,Full值满足条件" << " Full值" << Full.value << endl;
}
else if (this->pc == 2)
{
cout << this->name << " 成功,消费者申请获取缓冲池,Mutex值满足条件" << " Mutex值" << Mutex.value << endl;
mutex_c = 0;
}
}
this->pc++;
}
else if (S->value < 0)
{
this->state = 0;
if (this->roleFlag == 1) {
if (this->pc == 2) {
cout << this->name << " 失败,生产者申请获取缓冲池,Empty值不满足条件" << " Empty值" << Empty.value << endl;
}
else if (this->pc == 3) {
cout << this->name << " 失败,生产者申请获取缓冲池,Mutex值不满足条件" << " Mutex值" << Mutex.value << endl;
}
}
else if (this->roleFlag == 0) {
if (this->pc == 1) {
cout << this->name << " 失败,消费者申请获取缓冲池,Full值不满足条件" << " Full值" << Full.value << endl;
}
else if (this->pc == 2) {
cout << this->name << " 失败,消费者申请获取缓冲池,Mutex值不满足条件" << " Mutex值" << Mutex.value << endl;
}
}
block(S->list, this);
}
}
void signal(semaphore* S) {
S->value++;
if (this->roleFlag == 1)
{
if (this->pc == 5)
{
cout << this->name << " 成功,生产者释放缓冲池使用权,signal(Mutex)" << endl;
}
else if (this->pc == 6) {
cout << this->name << " 成功,生产者通知其他进程,signal(Full)" << endl;
}
}
else if (this->roleFlag == 0)
{
if (this->pc == 4)
{
cout << this->name << " 成功,消费者释放缓冲使用权,signal(Mutex)" << endl;
}
else if (this->pc == 5) {
cout << this->name << " 成功,消费者通知其他进程,signal(Full)" << endl;
}
}
if (S->value <= 0)
{
wakeup(S->list);
}
this->pc++;
if ((this->roleFlag == 0 && this->pc == 6) || (this->roleFlag == 1 && this->pc == 7)) {
cout << "已经完成一个进程===================================================================================" << this->name << endl;
cout << "name:" << this->name << "code:" << this->code << "pc:" << this->pc << "state:" << this->state << endl;
this->pc = 1;
}
}
};
PCB process[processNum];
PCB* wakeup(queue<PCB*>*& list) {
PCB* p = (*list).front();
int code = p->code;
process[code].state = 1;
if (process[code].roleFlag == 1) {
if (process[code].pc == 2)
{
cout << process[code].name << " 被唤醒,发现缓冲池不是满的" << " 缓冲池中食物数量" << food << "Full值" << Full.value << endl;
}
if (process[code].pc == 3)
{
cout << process[code].name << " 被唤醒,发现没有其他进程占有缓冲池" << "互斥变量" << Mutex.value << endl;
mutex_c = 1;
}
}
if (process[code].roleFlag == 0)
{
if (process[code].pc == 1)
{
cout << process[code].name << " 被唤醒,发现缓冲池中有食物" << " 食物数量" << food << " Full值" << Full.value << endl;
}
if (process[code].pc == 2)
{
cout << process[code].name << " 被唤醒,发现没有其他进程占有缓冲池" << " 互斥变量" << Mutex.value << endl;
}
}
process[code].pc++;
(*list).pop();
return p;
}
PCB* block(queue<PCB*>*& list, PCB* pcb) {
(*list).push(pcb);
PCB* cur = list->front();
process[pcb->code].state = 0;
return pcb;
}
class PandC {
private:
int in, out;
int count;
int buffer[N];
int mutex;
int empty, full;
public:
PandC() {
in = 0; out = 0; count = 0;
empty = N; full = 0; mutex = 1;
for (int i = 0; i < N; i++)
buffer[i] = 0;
}
void Producer(PCB* pro) {
int tim = rand() % 10 + 1;
switch (pro->pc)
{
case 1:
cout << pro->name << " 生产者正在生产食物" << "所需时间" << tim << "秒" << endl;
Sleep(tim * 100);
pro->pc++;
cout << pro->name << " 生产者生产时间" << tim << "秒" << "缓冲池中食物数量" << food << "个" << endl;
break;
case 2:
Sleep(350);
cout << pro->name << " 生产者查看缓冲池是否满了wait(empty)" << endl;
pro->wait(&Empty);
break;
case 3:
Sleep(350);
cout << pro->name << " 生产者查看是否有人使用缓冲队列wait(mutex)" << endl;
pro->wait(&Mutex);
break;
case 4:
Sleep(350);
food++;
cout << pro->name << " 放入食物" << "缓冲池中有食物" << food << "个" << " Full值为(二值不一致说明由消费者进程堵塞)" << Full.value << endl;
pro->pc++;
break;
case 5:
Sleep(350);
cout << pro->name << " 释放缓冲队列控制权signal(mutex)" << endl;
pro->signal(&Mutex);
break;
case 6:
Sleep(350);
cout << pro->name << " 已经完成生产过程通知消费者signal(full)" << endl;
pro->signal(&Full);
pro->pc = 1;
break;
}
}
void Consumer(PCB* pro) {
switch (pro->pc)
{
case 1:
Sleep(350);
cout << pro->name << " 消费者提出申请拿走食物wait(full)" << " 当前缓冲池中食物数量" << food << "当前full值" << Full.value << " 二者不一致表示有生产者虽然放置了食物但是没有signal操作完毕" << endl;
pro->wait(&Full);
break;
case 2:
Sleep(350);
cout << pro->name << "消费者获得拿走食物的申请wait(mutex)" << endl;
pro->wait(&Mutex);
break;
case 3:
Sleep(350);
food--;
cout << pro->name << " 消费者拿走食物" << endl;
pro->pc++;
break;
case 4:
Sleep(350);
cout << pro->name << " 消费者释放访问权signal(mutex)" << endl;
pro->signal(&Mutex);
break;
case 5:
Sleep(350);
cout << pro->name << " 消费者拿走食物signal(empty)" << endl;;
pro->signal(&Empty);
pro->pc = 1;
break;
}
}
};
void initProcess(int pN, int cN)
{
int i = 0;
for (; i < pN; i++) {
process[i].name = to_string(i + 1) + "号生产者进程";
process[i].roleFlag = 1;
process[i].state = 1;
process[i].pc = 1;
process[i].code = i;
}
for (; i < cN + pN; i++)
{
process[i].name = to_string(i + 1) + "号消费者进程";
process[i].roleFlag = 0;
process[i].state = 1;
process[i].pc = 1;
process[i].code = i;
}
}
int main() {
int i = 0;
int ci = 0;
printf(">>>>>>>>>>>>>>>>>>>>>>>>>(一下所有数字都是正整数)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
printf("请输入时间片执行次数,用于模拟多进程,请输入较大的值如(50):");
cin >> ci;
cout << "请输入生产者个数,消费者个数,空格分隔,输入1 1表示单消费者和单生产者模型";
int pN = 0, cN = 0;
cin >> pN >> cN;
printf("执行时间片%d次\n", ci);
printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n\n");
initProcess(pN, cN);
printf(">>>>>>>>>>>>>>>>>>>>Process begins......>>>>>>>>>>>>>>>>>>>>>>>\n");
PandC factory;
PCB* cur;
int a = 0;
uniform_int_distribution<int> uid{ 0,pN + cN - 1 };
random_device rd;
default_random_engine dre{ rd() };
do {
a = uid(dre);
cur = &process[a];
cout << cur->name << ":" << "full:" << Full.value << " mutex:" << Mutex.value << " empty:" << Empty.value << " code:" << cur->code << " pc:" << cur->pc << " state:" << cur->state << endl;
if (!cur->state) {
continue;
}
if (cur->roleFlag == 1) {
factory.Producer(cur);
}
else {
factory.Consumer(cur);
}
i++;
cout << "时间片执行次数============================" << i << endl;
} while (i != ci);
cout << "时间片执行" << ci << "次" << "最终执行结果:" << endl;
cout << "full:" << Full.value << endl;
cout << "empty:" << Empty.value << endl;
cout << "mutex:" << Mutex.value << endl;
cout << "food:" << food << endl;
return 0;
}
老师:“你们就简单实现以下就行”
|