Synchronization 使用posix信号量实现生产者和消费者

Synchronization 使用posix信号量实现生产者和消费者,synchronization,semaphore,producer-consumer,Synchronization,Semaphore,Producer Consumer,我在以下网站上读到了关于使用posix信号量实现生产者和消费者的信息: 但是,从我的角度来看,实施没有正确地澄清问题,我修改如下,我不太确定哪一个是正确的,请某位专家给出一些建议?哪一个是错的? 我修改了Put()和Get()函数,并在main()中创建信号量时更改了参数 原件: void Put(char item) { int value; sem_wait(&empty_sem_mutex); //get the mutex to fill the buffer b

我在以下网站上读到了关于使用posix信号量实现生产者和消费者的信息:

但是,从我的角度来看,实施没有正确地澄清问题,我修改如下,我不太确定哪一个是正确的,请某位专家给出一些建议?哪一个是错的? 我修改了Put()和Get()函数,并在main()中创建信号量时更改了参数 原件:

void Put(char item)
{
  int value;
  sem_wait(&empty_sem_mutex); //get the mutex to fill the buffer

  buffer[nextIn] = item;
  nextIn = (nextIn + 1) % BUFF_SIZE;
  printf("Producing %c ...nextIn %d..Ascii=%d\n",item,nextIn,item);
  if(nextIn==FULL)
    {
      sem_post(&full_sem_mutex);
      sleep(1);
    }
  sem_post(&empty_sem_mutex);

}

void Get()
{
  int item;

  sem_wait(&full_sem_mutex); // gain the mutex to consume from buffer

  item = buffer[nextOut];
  nextOut = (nextOut + 1) % BUFF_SIZE;
  printf("\t...Consuming %c ...nextOut %d..Ascii=%d\n",item,nextOut,item);
  if(nextOut==EMPTY) //its empty
    {
      sleep(1);
    }

  sem_post(&full_sem_mutex);
}

int main()
{
  pthread_t ptid,ctid;
  //initialize the semaphores

  sem_init(&empty_sem_mutex,0,1);
  sem_init(&full_sem_mutex,0,0);

  .....
}
我的实施

#include <pthread.h>
#include <stdio.h>
#include <semaphore.h>

#define BUFF_SIZE 4
#define FULL 0
#define EMPTY 0
char buffer[BUFF_SIZE];
int nextIn = 0;
int nextOut = 0;

sem_t empty_sem_mutex; //producer semaphore
sem_t full_sem_mutex; //consumer semaphore

void Put(char item)
{
  int value;
  sem_wait(&empty_sem_mutex); //get the mutex to fill the buffer

  buffer[nextIn] = item;
  nextIn = (nextIn + 1) % BUFF_SIZE;
  printf("Producing %c ...nextIn %d..Ascii=%d\n",item,nextIn,item);

  sem_post(&full_sem_mutex);
//  sleep(1);

}

void * Producer()
{
  int i;
  for(i = 0; i < 10; i++)
    {
      Put((char)('A'+ i % 26));
    }
}

void Get()
{
  int item;

  sem_wait(&full_sem_mutex); // gain the mutex to consume from buffer

  item = buffer[nextOut];
  nextOut = (nextOut + 1) % BUFF_SIZE;
  printf("\t...Consuming %c ...nextOut %d..Ascii=%d\n",item,nextOut,item);
  sem_post(&empty_sem_mutex);
//  sleep(1);
}

void * Consumer()
{
  int i;
  for(i = 0; i < 10; i++)
    {
      Get();
    }
}

int main()
{
  pthread_t ptid,ctid;
  //initialize the semaphores

  sem_init(&empty_sem_mutex,0,BUFF_SIZE);
  sem_init(&full_sem_mutex,0,0);

  //creating producer and consumer threads

  if(pthread_create(&ptid, NULL,Producer, NULL))
    {
      printf("\n ERROR creating thread 1");
      exit(1);
    }

  if(pthread_create(&ctid, NULL,Consumer, NULL))
    {
      printf("\n ERROR creating thread 2");
      exit(1);
    }

  if(pthread_join(ptid, NULL)) /* wait for the producer to finish */
    {
      printf("\n ERROR joining thread");
      exit(1);
    }

  if(pthread_join(ctid, NULL)) /* wait for consumer to finish */
    {
      printf("\n ERROR joining thread");
      exit(1);
    }

  sem_destroy(&empty_sem_mutex);
  sem_destroy(&full_sem_mutex);

  //exit the main thread

  pthread_exit(NULL);
  return 1;
}
#包括
#包括
#包括
#定义BUFF_大小4
#定义完整的0
#定义空0
字符缓冲区[BUFF_SIZE];
int-nextIn=0;
int nextOut=0;
sem_t empty_sem_mutex//生产者信号量
sem_t full_sem_互斥体//消费者信号量
作废放置(字符项)
{
int值;
sem_wait(&empty_sem_mutex);//获取mutex以填充缓冲区
缓冲区[nextIn]=项目;
nextIn=(nextIn+1)%BUFF\u大小;
printf(“正在生成%c…下一个%d..Ascii=%d\n”,项,下一个,项);
sem_post(和完整sem_互斥体);
//睡眠(1);
}
void*Producer()
{
int i;
对于(i=0;i<10;i++)
{
Put((char)('A'+i%26));
}
}
void Get()
{
国际项目;
sem_wait(&full_sem_mutex);//从缓冲区获取要使用的互斥量
项目=缓冲区[nextOut];
nextOut=(nextOut+1)%BUFF\U大小;
printf(“\t…消耗%c…下一个输出%d..Ascii=%d\n”,项,下一个输出,项);
sem_post(&空的sem_互斥体);
//睡眠(1);
}
void*Consumer()
{
int i;
对于(i=0;i<10;i++)
{
Get();
}
}
int main()
{
pthread_t ptid,ctid;
//初始化信号量
sem_init(&empty_sem_mutex,0,BUFF_SIZE);
sem_init(&full_sem_mutex,0,0);
//创建生产者和消费者线程
if(pthread_create(&ptid,NULL,Producer,NULL))
{
printf(“\n创建线程1时出错”);
出口(1);
}
if(pthread_create(&ctid,NULL,Consumer,NULL))
{
printf(“\n创建线程2时出错”);
出口(1);
}
if(pthread_join(ptid,NULL))/*等待生产者完成*/
{
printf(“\n连接线程时出错”);
出口(1);
}
if(pthread_join(ctid,NULL))/*等待使用者完成*/
{
printf(“\n连接线程时出错”);
出口(1);
}
sem_destroy(&清空sem_互斥体);
sem_destroy(&full_sem_mutex);
//退出主线程
pthread_exit(NULL);
返回1;
}

为什么不告诉我们您更改了什么?好的,我已经添加了修改,我认为我的更好