C 主螺纹,带2个子螺纹,以循环x倍产品&;消费者

C 主螺纹,带2个子螺纹,以循环x倍产品&;消费者,c,pthreads,mutex,semaphore,C,Pthreads,Mutex,Semaphore,我编写了一个程序,其中主线程创建了两个子线程。等待一个随机时间,然后生成一个介于1和6之间的随机值,并将该值放入randomValue变量。另一个线程等待并读取全局变量randomValue并打印该变量,因此我使用了一个信号量来确保读取的线程将始终读取由另一个线程写入的值 我想修改,使每个线程不知道x循环(2,3…),这样它可以生成x乘以一个随机值,并将该值放入randomValue,另一个线程将读取x乘以randomValue变量并打印它。任何修改代码的想法都是受欢迎的。多谢各位 #inclu

我编写了一个程序,其中主线程创建了两个子线程。等待一个随机时间,然后生成一个介于1和6之间的随机值,并将该值放入randomValue变量。另一个线程等待并读取全局变量randomValue并打印该变量,因此我使用了一个信号量来确保读取的线程将始终读取由另一个线程写入的值

我想修改,使每个线程不知道x循环(2,3…),这样它可以生成x乘以一个随机值,并将该值放入randomValue,另一个线程将读取x乘以randomValue变量并打印它。任何修改代码的想法都是受欢迎的。多谢各位

#include <time.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <pthread.h>
#include <semaphore.h>

/* variable shared by producer and consumer 
 (producer writes a value between 1 and 6) */

long randomValue = 0;
/**semaphore  **/
sem_t mex;


// function that create a random sleep-time (to induce unpredictability)
static int duration(int min, int max)
{
  static int first_time = 1;

  // Seed the random number generator with the current time
  // of day if we haven't done so yet.
  if (first_time) {
    first_time = 0;
    srand48((int) time(NULL));
  }
  return (int) (min + drand48()*(max - min));
}

/* producer program */
void *producer(void *arg) {
  char statearray[256];

  // Initialize random generator
  // Note that initstate and random are not threadsafe
  initstate(time(NULL), statearray, 256);

   sleep(duration(1,3));
   printf("prod: producing ...\n");
//random value 1 et 6
   randomValue = random();
   randomValue = ((double) randomValue / RAND_MAX)*6+1;
//put the value
   printf("prod: delivering  %ld\n", randomValue);
   sem_post(&mex); 
  pthread_exit(NULL);
}

/* consumer program */
void *consumer(void *arg) {


  sleep(duration(1,5));
  sem_wait(&mex);
  printf("cons: consuming ...\n");
  // 

  printf("cons: received %ld\n", randomValue);


  pthread_exit(NULL);
}

/* main thread */
int main(int argc, char *argv[]) {
  pthread_t tidprod, tidcons;


    if (sem_init(&mex,0,0)  != 0){
    perror("sem_init");
    exit(EXIT_FAILURE);
   }

  if (pthread_create(&tidprod, NULL, producer, NULL) != 0) {
    perror("pthread_create");
  }
  if (pthread_create(&tidcons, NULL, consumer, NULL) != 0) {
    perror("pthread_create");
  }


  if (pthread_join(tidcons, NULL) != 0) {
    perror("pthread_join prod");
  }

  if (pthread_join(tidprod, NULL) != 0) {
    perror("pthread_join prod");
  }

  fflush(stdout);
  pthread_exit(EXIT_SUCCESS);
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
/*生产者和消费者共享的变量
(生产者写入介于1和6之间的值)*/
长随机值=0;
/**信号量**/
扫描电镜;
//创建随机睡眠时间的函数(导致不可预测性)
静态整数持续时间(整数最小值、整数最大值)
{
静态int首次_时间=1;
//使用当前时间为随机数生成器设定种子
//如果我们还没有这样做的话。
如果(第一次){
第一时间=0;
srand48((int)time(NULL));
}
返回值(int)(最小+拖动48()*(最大-最小));
}
/*制片人节目*/
无效*生产者(无效*参数){
字符状态数组[256];
//初始化随机发生器
//请注意,initstate和random不是线程安全的
initstate(时间(NULL),statearray,256);
睡眠时间(1,3);
printf(“产品:生产…\n”);
//随机值1 et 6
随机值=随机();
随机值=((双)随机值/RAND_MAX)*6+1;
//估价
printf(“产品:交付%ld\n”,随机值);
sem_邮政公司(mex);
pthread_exit(NULL);
}
/*消费者计划*/
无效*使用者(无效*参数){
睡眠时间(1,5);
sem_wait&mex;
printf(“cons:consuming…\n”);
// 
printf(“cons:收到%ld\n”,随机值);
pthread_exit(NULL);
}
/*主线*/
int main(int argc,char*argv[]){
pthread_t tidprod,tidcons;
if(sem_init(&mex,0,0)!=0){
perror(“sem_init”);
退出(退出失败);
}
if(pthread_create(&tidprod,NULL,producer,NULL)!=0){
perror(“pthread_create”);
}
if(pthread_create(&tidcons,NULL,consumer,NULL)!=0){
perror(“pthread_create”);
}
if(pthread_join(tidcons,NULL)!=0){
perror(“pthread_join prod”);
}
if(pthread_join(tidprod,NULL)!=0){
perror(“pthread_join prod”);
}
fflush(stdout);
pthread_exit(exit_SUCCESS);
}

如果只想发送和接收多个值,则不太清楚


要发送和接收多个值,请使用For-loop-in-producer和consumer以及数组作为randomValue。如果要生成一个值并首先使用它,循环将在producer中包含“sem_post(&mex);”并在循环中包含“sem_wait(&mex);”。否则,sem_post()和sem_wait()将在循环外。

如果只想发送和接收多个值,则不太清楚


要发送和接收多个值,请使用For-loop-in-producer和consumer以及数组作为randomValue。如果要生成一个值并首先使用它,循环将在producer中包含“sem_post(&mex);”并在循环中包含“sem_wait(&mex);”。否则,sem_post()和sem_wait()将在循环外。

您只需稍作修改即可完成以下操作:

  • 不要使用一个信号量,而是使用两个:一个信号量来告诉消费者 数字已经准备好,一个用来告诉生产者消费者已经准备好消费
  • 具有一个特殊值,以向消费者表明生产者不会 不再生产了
因此,您可以通过以下方式更改代码:

信号量声明

信号量初始化

生产者线程函数

(我删除了你的评论)


您只需稍作修改即可完成所需操作:

  • 不要使用一个信号量,而是使用两个:一个信号量来告诉消费者 数字已经准备好,一个用来告诉生产者消费者已经准备好消费
  • 具有一个特殊值,以向消费者表明生产者不会 不再生产了
因此,您可以通过以下方式更改代码:

信号量声明

信号量初始化

生产者线程函数

(我删除了你的评论)

/*semaphores  */
/* Set by producer when production is ready */
sem_t mex_prod;
/* Set by consumer when ready to consume */
sem_t mex_cons;
/* by default, nothing produced */
if (sem_init(&mex_prod,0,0)  != 0){
    perror("sem_init");
    exit(EXIT_FAILURE);
}
/* by default, consumer is not ready */
if (sem_init(&mex_cons,0,0)  != 0){
    perror("sem_init");
    exit(EXIT_FAILURE);
}
void *producer(void *arg) {
    char statearray[256];
    initstate(time(NULL), statearray, 256);

    /* choose how much to product  */
    int number_of_productions = 2 + random()%5;

    printf("prod: %d to produce\n", number_of_productions );

    /* this loop can be replaced by some for (i = 0; i< num; ++i) loop */
    while(number_of_productions--)
    {
        sleep(duration(1,3));           

        /* wait for consumer to be ready */
        sem_wait(&mex_cons);

        printf("prod: producing ...\n");

        randomValue = random();
        randomValue = ((double) randomValue / RAND_MAX)*6+1;

        printf("prod: delivering  %ld\n", randomValue);
        sem_post(&mex_prod); 
    }

    sem_wait(&mex_cons);

    /* generate a special value to tell the consumer that no new value
       will be given */
    randomValue  = -1;

    sem_post(&mex_prod); 

    pthread_exit(NULL);
}
void *consumer(void *arg) {

    /* tell producer that consumer is ready */
    sem_post(&mex_cons);

    /* since we don't know how many value will be generated, we have an
       infinite loop */
    while(1)
    {
        sleep(duration(1,5));
        sem_wait(&mex_prod);
        printf("cons: consuming ...\n");

        printf("cons: received %ld\n", randomValue);

        /* value has been consumed, tell producer we are ready for a new one */
        sem_post(&mex_cons); 

        /* if randomValue is -1, we break the loop since no more value will come */
        if (-1 == randomValue)             
            break;

    }
    pthread_exit(NULL);
}