2014-02-24 38 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