2014-02-24 39 views
0

我有一个标准生产者消费者问题的有界缓冲区。每当我给不等数量的生产者或消费者程序不终止。 我有限的插入或删除50 我想的数量就知道为什么会出现上述问题,以及如何解决它生产者消费者使用POSIX

#include<stdio.h> 
#include<pthread.h> //Header for creating Posix Threads; 
#include<semaphore.h> 
#include<stdlib.h> 
#define MAX 20 

typedef struct shared_buffer 
    { 
    int arr[20]; 
    int i; 
    }buffer; 

    buffer b;   //Fixed Length buffer shared memory 

sem_t full,empty; //Counting semaphores full->no of slots filled empty ->no of slots  empty 
pthread_mutex_t mutex; //mutual exclusion for critcal section 
int flag=1,cnt=0;  
void * producer(void * arg) //Producer threads method 
{ 
    int index; //thread Id 
    index=(int)arg; 

    while(flag) 
    { 

    sem_wait(&empty);  //will check if slot available and decrement empty count 
    pthread_mutex_lock(&mutex); //acquiring lock on process 
    b.arr[b.i]=rand()%100;  //critcal section 
    printf("\n Process %d Produced :%d",index,b.arr[b.i]); 
    b.i++; 
     cnt++;    //critcal section ends 
    pthread_mutex_unlock(&mutex); //realeasing lock 
    sem_post(&full);    //increment full count 
     usleep(rand()%100);   //sleep for random time 

    } 
} 

void * consumer(void * arg) 
{ 
    int index; 
    index=(int)arg; 
    while(flag) 
    { 
    sem_wait(&full);     //will check if buffer is not empty 
    pthread_mutex_lock(&mutex); 
    b.i--;       //critical section 
    printf("\n Process %d consumed :%d",index,b.arr[b.i]); 
    cnt++;      //critical section ends 
    pthread_mutex_unlock(&mutex); //release lock 
    sem_post(&empty);    //increment count of empty slots 
    usleep(rand()%100); 
    } 
} 


int main(int argc,char * argv[]) 
{ 
    pthread_t Pid,Cid; 
    int P,C,index,size; 
    b.i=0; 
if(argc<4) 
{ 
    printf("Error.Usage : ./filename.out <no of producer> <no of consumer> <Buffer  Size(<=15)> \n"); 
    exit(0); 
} 
    P=atoi(argv[1]); 
    C=atoi(argv[2]); 
    size=atoi(argv[3]); 
    sem_init(&full,0,0);    //number of slots filled is 0 
    sem_init(&empty,0,size);   //number of empty slots is buffer size 
    pthread_mutex_init(&mutex,NULL); 

    for (index=0;index<C;index++)  //creating C number of consumer 
    { 
    pthread_create(&Cid,NULL,consumer,index); 
    } 
    for (index=0;index<P;index++) //creating P number of producer 
    { 
    pthread_create(&Pid,NULL,producer,index); 
    } 
while(cnt<=50)     //maximum 50 operations allowed 
    usleep(200); 
    flag=0; 
    printf("phew!Successful"); 
    pthread_exit(NULL); 
    return 1; 

}

+0

这不是你问题的答案,而是用#define MAX 20替换'#define MAX 20;',否则你将无法使用MAX常量。您现在不使用它,因此这在您提交的程序中不是问题。 –

+0

请正确缩进ayou程序。 –

+0

好吧,生病吧 – user3347303

回答

0

这里有一些线索(不完整的答案):

让我感到困扰的第一件事是您在您的循环中覆盖了pthread_t引用。但没关系,因为你之后不使用它们。

第二件事情是你弄丢了pthread_exit:应该有您所创建的线程结束:

void * consumer_producer(void * arg) 
{ 
    // ... 
    while(flag) 
    { 
     // ... 
    } 
    pthread_exit(NULL); 
} 
+0

请注意''pthread_t'引用可以用于'main'通过'pthread_join()跟踪线程终止' – Coconop

+0

您在for循环中说的那些引用。不要他们作为单个线程的ID不主要在主要导致所有其他线程被终止之前终止主本身 – user3347303

0

我猜你有一个竞争条件。当线程正在等待flag设置为0时,其他线程可能会捕获该状态并返回,因此等待线程将永远等待。

+0

但即使我有相同数量的生产者和消费者权利,我也许会发生竞争条件 – user3347303

+0

我可能,但更改是如果数量不均匀,则会更高,因为此时您的calles不平衡。 – Matthias

+0

s/changes/chances/g – Matthias