为什么我会得到不同的输出?我使用有界缓冲区,pthreads和信号量用c ++编写代码

时间:2017-11-26 20:35:12

标签: c++ gcc pthreads semaphore

我相信我对信号量的理解是正确的。这是生产者/消费者的问题。生产者生成一个随机字母,将空缓冲区减1,减少互斥量以获得进入其临界区的权限,然后在临界区内将随机字母添加到缓冲区,然后增加互斥,然后增加满。消费者等待直到full大于0,减少互斥量以查看它是否可以进入其临界区,在临界区内随机字母将从缓冲区中取出并进行一些计算,然后它将增加互斥并增加空。

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

#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)。消费者也会将值替换为空,因此我认为这可能是我遇到的同步错误。再次感谢您的投入。

1 个答案:

答案 0 :(得分:1)

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