C 生产者消费者使用posix
对于有界缓冲区,我有一个标准的生产者-消费者问题。每当我给出不相等的生产者或消费者数量时,程序不会终止。 我已将插入或删除的次数限制为50次 我想知道为什么会出现上述问题以及解决方法C 生产者消费者使用posix,c,linux,posix,producer-consumer,C,Linux,Posix,Producer Consumer,对于有界缓冲区,我有一个标准的生产者-消费者问题。每当我给出不相等的生产者或消费者数量时,程序不会终止。 我已将插入或删除的次数限制为50次 我想知道为什么会出现上述问题以及解决方法 #include<stdio.h> #include<pthread.h> //Header for creating Posix Threads; #include<semaphore.h> #include<stdlib.h> #define MAX
#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;
#包括
#包括//用于创建Posix线程的头;
#包括
#包括
#定义最大值20
typedef结构共享\u缓冲区
{
int-arr[20];
int i;
}缓冲器;
缓冲器b//固定长度缓冲区共享内存
sem_t满,空//计数信号量已满->插槽已满数量->插槽已空数量
pthread_mutex_t mutex//临界截面互斥
int flag=1,cnt=0;
void*producer(void*arg)//producer-threads方法
{
int index;//线程Id
索引=(int)arg;
while(旗帜)
{
sem_wait(&empty);//将检查插槽是否可用并减少空计数
pthread_mutex_lock(&mutex);//获取进程上的锁
b、 arr[b.i]=rand()%100;//标准节
printf(“\n进程%d产生:%d”,索引,b.arr[b.i]);
b、 i++;
cnt++;//临界段结束
pthread_mutex_unlock(&mutex);//解除锁定
sem_post(&full);//递增完整计数
usleep(rand()%100);//随机时间睡眠
}
}
无效*使用者(无效*参数)
{
整数指数;
索引=(int)arg;
while(旗帜)
{
sem_wait(&full);//将检查缓冲区是否为空
pthread_mutex_lock(&mutex);
b、 i--;//临界截面
printf(“\n进程%d消耗:%d”,索引,b.arr[b.i]);
cnt++;//临界段结束
pthread_mutex_unlock(&mutex);//释放锁
sem_post(&empty);//空插槽的增量计数
usleep(兰特()%100);
}
}
int main(int argc,char*argv[])
{
pthread_t Pid,Cid;
int P,C,索引,大小;
b、 i=0;
如果(argc以下是一些线索(不是完整答案):
首先让我感到不安的是,您正在覆盖循环中的pthread\t
引用。但这没关系,因为您以后不会使用它们
第二件事是您将pthread\u exit
放错了位置:您创建的线程末尾应该有:
void * consumer_producer(void * arg)
{
// ...
while(flag)
{
// ...
}
pthread_exit(NULL);
}
我猜你有一个争用条件。当标志设置为0时,可能有一个线程正在等待。其他线程可能会捕获该状态并返回,因此等待的线程将永远等待。这不是问题的答案,而是将定义最大20;
替换为\define MAX 20
,否则你将不会被拒绝能够使用最大常数。你现在不使用它,因此这在你提交的程序中不是问题。请正确缩进你的程序。缩进的意思是??去寻找“编程中的缩进”。是否遵循allman风格的缩进注意,pthread\u t
引用可用于main
中,通过pthread\u join()跟踪线程终止
您在for循环中所说的那些引用。它们不充当单个线程的ID吗主线程中的pthread_exit不导致在终止主线程之前终止所有其他线程吗?但是竞争条件应该发生,即使当生产者和消费者权利的数量相等时,我也可能发生,但如果发生une,则变化更大ven number,因为您有不平衡的调用。那么,如果我自己更改关键部分中的标志值,您想怎么做?该标志是异步设置的。您可以使用本地标志,这些标志设置为关键部分中全局标志的值。但是,它不能保证所有产品都真正消费预计起飞时间。