我有一個標準生產者消費者問題的有界緩衝區。每當我給不等數量的生產者或消費者程序不終止。 我有限的插入或刪除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;
}
這不是你問題的答案,而是用#define MAX 20替換'#define MAX 20;',否則你將無法使用MAX常量。您現在不使用它,因此這在您提交的程序中不是問題。 –
請正確縮進ayou程序。 –
好吧,生病吧 – user3347303