gpt4 book ai didi

c++ - 为什么我得到不同的输出?我正在使用有界缓冲区、pthreads 和信号量使用 c++ 进行编码

转载 作者:行者123 更新时间:2023-11-28 04:54:22 25 4
gpt4 key购买 nike

u根据我对信号量的理解,我相信我的逻辑是正确的。这是生产者/消费者问题。生产者生成一个随机字母,将空缓冲区减 1,减少互斥量以获得进入其临界区的许可,然后在临界区内将随机字母添加到缓冲区,然后递增互斥量,然后递增满。 Consumer等到full大于0,递减mutex看是否可以进入临界区,在临界区内随机字母会从buffer中取出来做一些计算,然后递增mutex和递增empty。

我遇到的问题是我在运行程序时不断得到不同的输出。我得到它们似乎没有顺序。有些输出是正确的,有些是错误的。为什么我得到不同的输出?这是我的代码和输出:

#include <iostream>
#include <pthread.h>
#include <string>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <semaphore.h>
#include <cstring>
using namespace std;

void* consumer(void*);
void* producer(void*);

string buffer[3];
int put = 0;
int get = 0;
sem_t mutex;
sem_t empty;
sem_t full;
int pw = 0;
int cw = 0;
string alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

int main() {

sem_init(&mutex, 0, 1);
sem_init(&empty, 0, 3);
sem_init(&full, 0, 0);

pthread_t consumerThread;
pthread_t producerThread;
pthread_create(&producerThread, NULL, &producer, NULL);
pthread_create(&consumerThread, NULL, &consumer, NULL);
pthread_join(producerThread, NULL);
pthread_join(consumerThread, NULL);

sem_destroy(&mutex);
sem_destroy(&empty);
sem_destroy(&full);

return 0;
}

void* producer(void*) {

while (pw < 6) {
pw++;

//srand(time(NULL));
int a1 = rand() % 25;
cout << "producer: " << alpha[a1] << endl;

sem_wait(&empty);
sem_wait(&mutex);

buffer[put] = alpha[a1];
put = (put + 1) % 3;

sem_post(&mutex);
sem_post(&full);

}
return 0;
}

void* consumer(void*) {

while (cw < 6) {
cw++;

sem_wait(&full);
sem_wait(&mutex);

string c = (1, buffer[get]);
buffer[get] = "";
get = (get + 1) % 3;

int a;
int v1;
int v2;
int d1;
int d2;
string S;
string T;
for (int i = 0; i < alpha.length(); i++) {
if (alpha[i] == c[0]) {
a = i;
}
}

if (a % 2 == 0) {

if (a == 0) {
v1 = 20;
v2 = 4;
} else if (a > 0 && a < 4) {
v1 = 0;
v2 = 4;
} else if (a == 4) {
v1 = 0;
v2 = 8;
} else if (a > 4 && a < 8) {
v1 = 4;
v2 = 8;
} else if (a == 8) {
v1 = 4;
v2 = 14;
} else if (a > 8 && a < 14) {
v1 = 8;
v2 = 14;
} else if (a == 14) {
v1 = 8;
v2 = 20;
} else if (a > 14 && a < 20) {
v1 = 14;
v2 = 20;
} else if (a == 20) {
v1 = 14;
v2 = 0;
} else if (a > 20 && a <= 25) {
v1 = 20;
v2 = 0;
} else {
cout << "error with v1 and v2, incorrect index" << endl;
}

S = alpha[v1] + c + alpha[v2];

if (v1 > a) {
d1 = a - v1 + 26;
} else {
d1 = a - v1;
}

if (a > v2) {
d2 = (25 - v2) % 25;
} else {
d2 = v2 - a;
}

if (d1 > d2) {
T = alpha[v1] + S + alpha[v1];
} else if (d2 > d1) {
T = alpha[v2] + S + alpha[v2];
} else {
//cout << "Both distances are the same." << endl;
}

cout << alpha[a] << alpha[v1] << alpha[v2] << S << " - c to v1: " << d1 << " - c to v2: " << d2 << endl;

if (d1 > d2) {
cout << "The distance from c to v1 is larger: " << d1 << endl;
} else if (d2 > d1) {
cout << "The distance from c to v2 is larger: " << d2 << endl;
} else {
cout << "The distances are the same: " << d1 << endl;
}

//testing
//cout << "v1: " << v1 << endl;
//cout << "v2: " << v2 << endl;
//cout << "d1: " << d1 << endl;
//cout << "d2: " << d2 << endl;

} else {
cout << "consumer: " << alpha[a] << endl;
}
sem_post(&mutex);
sem_post(&empty);
}

return 0;
}

输出:

output 1:

producer: I
producer: L
producer: C
producer: P
IEOEIO - c to v1: 4 - c to v2: 6
The distance from c to v2 is larger: 6
consumer: L
CAEACE - c to v1: 2 - c to v2: 2
The distances are the same: 2
producer: S
consumer: P
producer: K
SOUOSU - c to v1: 4 - c to v2: 2
The distance from c to v1 is larger: 4
KIOIKO - c to v1: 2 - c to v2: 4
The distance from c to v2 is larger: 4

RUN FINISHED; exit value 0; real time: 0ms; user: 0ms; system: 0ms

output 2:

producer: I
producer: L
IEOEIO - c to v1: 4 - c to v2: 6
The distance from c to v2 is larger: 6
producer: C
consumer: L
producer: P
CAEACE - c to v1: 2 - c to v2: 2
The distances are the same: 2
producer: S
consumer: P
producer: K
SOUOSU - c to v1: 4 - c to v2: 2
The distance from c to v1 is larger: 4
KIOIKO - c to v1: 2 - c to v2: 4
The distance from c to v2 is larger: 4

RUN FINISHED; exit value 0; real time: 0ms; user: 0ms; system: 0ms

看起来缓冲区工作正常,因为放入其中的所有内容都出来了,没有被删除或忽略,但不应该连续有 4 个生产者输出,因为这会超过缓冲区大小 (3)。消费者也确实将 value 中的值替换为空,所以我相信这可能是我遇到的同步错误。再次感谢您的意见。

最佳答案

您正在生产者的关键部分之前打印。因此,它可以打印 4 次然后停在信号量上,而缓冲区中只有 3 个元素。只有在消费者消费了这 3 个元素中的一个之后,下一个元素才会放入缓冲区。

关于c++ - 为什么我得到不同的输出?我正在使用有界缓冲区、pthreads 和信号量使用 c++ 进行编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47500768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com