POSIX生产者 - 消费者

问题描述:

我有一个与生产者和消费者有关的问题。我有1个生产者和3个消费者。制作人出示我放在队列中的信件,消费者拿着这封信。据认为,当两个消费者接受信件时,信件已经从队列中移除,但是有一个选项,消费者A和消费者C不能接受同一封信(可能A会先写信,那么con。B将首先采用第二或con。B,然后con C(或A)秒,但当A和C在一起时不可能)。POSIX生产者 - 消费者

我写了一个解决这个问题的代码,但是现在我不能从函数中打印任何东西。

#include <stdio.h> 
#include <stdlib.h> 
#include <pthread.h> 
#include <semaphore.h> 
#include <unistd.h> 
#include "FIFO.h" 

#define semaphore sem_t 
#define MAX_SIZE _SIZE 
#define SLEEP 1 
#define true 1 

FIFO buff; 
semaphore full, empty, a_read, b_read, c_read, mutex; 

void randSleep(int max) { 
    double x = rand()/(double)RAND_MAX; 
    usleep((int)floor(x*max)); 
} 


void* producer_func() 
{ 
    char c='A'; 
    while(GetAmount(&buff)<MAX_SIZE) 
    { 
     sem_wait(&empty); 
     sem_wait(&mutex); 

     Push(&buff, c); 

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

     randSleep(SLEEP); 
    } 
    pthread_exit(NULL); 
} 

void* consumer_a_func() 
{ 
    char c; 
    long int first_met; 

    while(true) 
    { 
     sem_wait(&a_read); 
     sem_wait(&full); 

     if (sem_getvalue(&b_read,&first_met)) printf("Error with returning b_read value"); 
     sem_wait(&mutex); 

     if (first_met) 
     { 
      c = First(&buff); 
      sem_post(&mutex); 
     } 
     else 
     { 
      c = Pop(&buff); 
      sem_post(&mutex); 
      sem_post(&a_read); 
      sem_post(&b_read); 
      sem_post(&empty); 
     } 
     //printf("c value %s\n", c); 
     randSleep(SLEEP); 
    } 

    pthread_exit(NULL); 
} 

void* consumer_b_func() 
{ 
    char c; 
    long int first_met_a, first_met_c ; 

    while(true) 
    { 
     sem_wait(&b_read); 
     sem_wait(&full); 

     if (sem_getvalue(&a_read,&first_met_a)) printf("Error with returning a_read value"); 
     if (sem_getvalue(&c_read,&first_met_c)) printf("Error with returning c_read value"); 

     sem_wait(&mutex); 
     if (first_met_a && first_met_c) 
     { 
      c = First(&buff); 
      sem_post(&mutex); 
     } 
     else 
     { 
      c = Pop(&buff); 
      sem_post(&mutex); 
      sem_post(&b_read); 
      if (first_met_a) 
       sem_post(&c_read); 
      else 
       sem_post(&a_read); 
      sem_post(&empty); 
     } 
     //printf("c value %s\n", c); 
     randSleep(SLEEP); 
    } 

    pthread_exit(NULL); 
} 

void* consumer_c_func() 
{ 
    char c; 
    long int first_met; 

    while(true) 
    { 

     sem_wait(&c_read); 
     sem_wait(&full); 

     if (sem_getvalue(&b_read,&first_met)) printf("Error with returning b_read value"); 

     sem_wait(&mutex); 

     if (first_met) 
     { 
      c = First(&buff); 
      sem_post(&mutex); 
     } 
     else 
     { 
      c = Pop(&buff); 
      sem_post(&mutex); 
      sem_post(&c_read); 
      sem_post(&b_read); 
      sem_post(&empty); 
     } 
     printf("c value %s\n", c); 
     randSleep(SLEEP); 
    } 

    pthread_exit(NULL); 
} 

int main() 
{ 
    Init(&buff); 
    sem_init(&empty, 0, MAX_SIZE); 
    sem_init(&full, 0, 0); 
    sem_init(&a_read, 0, 1); 
    sem_init(&b_read, 0, 1); 
    sem_init(&c_read, 0, 1); 
    sem_init(&mutex, 0, 1); 

    pthread_t producer, consumer_a, consumer_b, consumer_c; 

    pthread_create(&producer, NULL, producer_func, NULL); 
    printf("All right\n"); 
    pthread_create(&consumer_a, NULL, consumer_a_func, NULL); 
    printf("All right\n"); 
    pthread_create(&consumer_b, NULL, consumer_b_func, NULL); 
    printf("All right\n"); 
    pthread_create(&consumer_c, NULL, consumer_c_func, NULL); 
    printf("All right\n"); 

    pthread_join(&producer, (void**)NULL); 
    pthread_join(&consumer_a, (void**)NULL); 
    pthread_join(&consumer_b, (void**)NULL); 
    pthread_join(&consumer_c, (void**)NULL); 

} 

文件FIFO.hFIFO.c

误差看起来像

在_IO_vfprintf_internal(S = 0x7ffff78ac620 < _IO_2_1_stdout_>,格式=,AP = AP @条目= 0x7ffff6acfe58)在vfprintf.c: 1632()

或分段错误(核心转储)

但有时这种代码运行正常,但没有任何的printf的我在生产者 - 消费者功能写道

+0

伴侣,如果你想得到任何答案,你必须将你的问题减少到简单,简短和教学的代码。阅读堆栈溢出的推荐做法。 –

+0

对不起,我的“小”代码,但我不知道哪个部分工作错了=( – koshachok

+0

请问,如果你的问题只是关于printf,请为未来的用户更改你的问题的标题 –

//printf("c value %s\n", c); 

c是一个字符,打印它,你应该使用

printf("c value %c\n", c); 

你可以试试使用-Wformat进行编译,所以如果在printf调用中对变量使用无效格式,则会发生警告。

+0

一般来说,我会建议使用['-Wall'](https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wall-323)(gcc链接),其中包括你的' (具有讽刺意味的是,'-Wall'并不是所有可能的警告。) – e0k

+0

或者强迫自己真正学习C,用'-Wall -Wextra -pedantic'进行编译,并且不接受任何代码,直到它编译时没有任何警告。(除了'-Wall'外加'-Wextra -pedantic'给你几乎所有可能的警告) –

+0

@ DavidC.Rankin:至少加上'-Wconversion'一般来说,最好是静音警告(例如* *好理解的**演员)比之后想知道发生了什么 – Olaf