Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9087685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:45:09+00:00 2026-06-16T21:45:09+00:00

I have the following code but the producer and consumer code don’t appear. It

  • 0

I have the following code but the producer and consumer code don’t appear. It just prints “Exit the program”. It’s like the consumer and producer functions are never executed. I don’t understand why this is happening. Can anyone explain why?
The code is as follows:

/* buffer.h */
typedef int buffer_item;
#define BUFFER_SIZE 18

/* main.c */

#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
/* #include "buffer.h" */


#define RAND_DIVISOR 100000000
#define TRUE 1

/* The mutex lock */
pthread_mutex_t mutex;

/* the semaphores */
sem_t full, empty;

/* the buffer */
buffer_item buffer[BUFFER_SIZE];

/* buffer counter */
int counter;

pthread_t tid;       //Thread ID
pthread_attr_t attr; //Set of thread attributes

void *producer(void *param); /* the producer thread */
void *consumer(void *param); /* the consumer thread */

void initializeData()
{

    /* Create the mutex lock */
    pthread_mutex_init(&mutex, NULL);

    /* Create the full semaphore and initialize to 0 */
    sem_init(&full, 0, 0);

    /* Create the empty semaphore and initialize to BUFFER_SIZE */
    sem_init(&empty, 0, BUFFER_SIZE);

    /* Get the default attributes */
    pthread_attr_init(&attr);

    /* init buffer */
    counter = 0;
}

/* Producer Thread */
void *producer(void *param)
{
    buffer_item item;

    while(TRUE)
    {
        /* sleep for a random period of time */
        int rNum = rand() / RAND_DIVISOR;
        sleep(rNum);

        /* generate a random number */
        item = rand();

        /* acquire the empty lock */
        sem_wait(&empty);
        /* acquire the mutex lock */
        pthread_mutex_lock(&mutex);

        if (insert_item(item))
            fprintf(stderr, " Producer report error condition\n");
        else
            printf("producer produced %d\n", item);

        /* release the mutex lock */
        pthread_mutex_unlock(&mutex);
        /* signal full */
        sem_post(&full);
    }
}

/* Consumer Thread */
void *consumer(void *param)
{
    buffer_item item;

    while(TRUE) {
        /* sleep for a random period of time */
        int rNum = rand() / RAND_DIVISOR;
        sleep(rNum);

        /* aquire the full lock */
        sem_wait(&full);
        /* aquire the mutex lock */
        pthread_mutex_lock(&mutex);
        if (remove_item(&item)) {
           fprintf(stderr, "Consumer report error condition\n");
        } else {
            printf("consumer consumed %d\n", item);
        }
        /* release the mutex lock */
        pthread_mutex_unlock(&mutex);
        /* signal empty */
        sem_post(&empty);
    }
}

/* Add an item to the buffer */
int insert_item(buffer_item item)
{
    /* When the buffer is not full add the item
      and increment the counter*/
    if (counter < BUFFER_SIZE) {
        buffer[counter] = item;
        counter++;
        return 0;
    } else { /* Error the buffer is full */
        return -1;
    }
}

/* Remove an item from the buffer */
int remove_item(buffer_item *item)
{
    /* When the buffer is not empty remove the item
      and decrement the counter */
    if (counter > 0) {
        *item = buffer[(counter-1)];
        counter--;
        return 0;
    } else { /* Error buffer empty */
        return -1;
    }
}

int main(int argc, char *argv[])
{
    /* Loop counter */
    int i;

     /* Verify the correct number of arguments were passed in */
    if(argc != 4) {
        fprintf(stderr, "USAGE:./main.out <INT> <INT> <INT>\n");
    }

    int mainSleepTime = atoi(argv[1]); /* Time in seconds for main to sleep */
    int numProd = atoi(argv[2]); /* Number of producer threads */
    int numCons = atoi(argv[3]); /* Number of consumer threads */

    /* Initialize the app */
    initializeData();

    /* Create the producer threads */
    for(i = 0; i < numProd; i++) {
        /* Create the thread */
        pthread_create(&tid,&attr,producer,NULL);
    }

    /* Create the consumer threads */
    for(i = 0; i < numCons; i++) {
        /* Create the thread */
        pthread_create(&tid,&attr,consumer,NULL);
    }

    /* Sleep for the specified amount of time in milliseconds */
    sleep(mainSleepTime);

    /* Exit the program */
    printf("Exit the program\n");
    exit(0);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T21:45:10+00:00Added an answer on June 16, 2026 at 9:45 pm

    First you need to apply different tid’s for your threads.

    pthread_t tid1, tid2;

    Then use these tid’s:

    pthread_create(&tid1, &attr, producer, NULL);

    pthread_create(&tid2, &attr, consumer, NULL);

    Lastly, skip the sleep and join the threads.

    pthread_join(tid1, NULL);

    pthread_join(tid2, NULL);

    I’ve tested it, and works on my system. Below is the working code in full.

    #include <stdlib.h>
    #include <stdio.h>
    #include <pthread.h>
    #include <semaphore.h>
    #include "buffer.h"
    
    
    #define RAND_DIVISOR 100000000
    #define TRUE 1
    
    /* The mutex lock */
    pthread_mutex_t mutex;
    
    /* the semaphores */
    sem_t full, empty;
    
    /* the buffer */
    buffer_item buffer[BUFFER_SIZE];
    
    /* buffer counter */
    int counter;
    
    pthread_t tid1, tid2;       //Thread ID
    pthread_attr_t attr; //Set of thread attributes
    
    void *producer(void *param); /* the producer thread */
    void *consumer(void *param); /* the consumer thread */
    
    void initializeData() {
    
       /* Create the mutex lock */
       pthread_mutex_init(&mutex, NULL);
    
       /* Create the full semaphore and initialize to 0 */
       sem_init(&full, 0, 0);
    
       /* Create the empty semaphore and initialize to BUFFER_SIZE */
       sem_init(&empty, 0, BUFFER_SIZE);
    
       /* Get the default attributes */
       pthread_attr_init(&attr);
    
       /* init buffer */
       counter = 0;
    }
    
    /* Producer Thread */
    void *producer(void *param) {
       buffer_item item;
    
       while(TRUE) {
          /* sleep for a random period of time */
          int rNum = rand() / RAND_DIVISOR;
          sleep(rNum);
    
          /* generate a random number */
          item = rand();
    
          /* acquire the empty lock */
          sem_wait(&empty);
          /* acquire the mutex lock */
          pthread_mutex_lock(&mutex);
    
          if(insert_item(item)) {
             fprintf(stderr, " Producer report error condition\n");
          }
          else {
             printf("producer produced %d\n", item);
          }
          /* release the mutex lock */
          pthread_mutex_unlock(&mutex);
          /* signal full */
          sem_post(&full);
       }
    }
    
    /* Consumer Thread */
    void *consumer(void *param) {
       buffer_item item;
    
       while(TRUE) {
          /* sleep for a random period of time */
          int rNum = rand() / RAND_DIVISOR;
          sleep(rNum);
    
          /* aquire the full lock */
          sem_wait(&full);
          /* aquire the mutex lock */
          pthread_mutex_lock(&mutex);
          if(remove_item(&item)) {
             fprintf(stderr, "Consumer report error condition\n");
          }
          else {
             printf("consumer consumed %d\n", item);
          }
          /* release the mutex lock */
          pthread_mutex_unlock(&mutex);
          /* signal empty */
          sem_post(&empty);
       }
    }
    
    /* Add an item to the buffer */
    int insert_item(buffer_item item) {
       /* When the buffer is not full add the item
          and increment the counter*/
       if(counter < BUFFER_SIZE) {
          buffer[counter] = item;
          counter++;
          return 0;
       }
       else { /* Error the buffer is full */
          return -1;
       }
    }
    
    /* Remove an item from the buffer */
    int remove_item(buffer_item *item) {
       /* When the buffer is not empty remove the item
          and decrement the counter */
       if(counter > 0) {
          *item = buffer[(counter-1)];
          counter--;
          return 0;
       }
       else { /* Error buffer empty */
          return -1;
       }
    }
    
    int main(int argc, char *argv[]) {
       /* Loop counter */
       int i;
    
       /* Verify the correct number of arguments were passed in */
       if(argc != 4) {
          fprintf(stderr, "USAGE:./main.out <INT> <INT> <INT>\n");
       }
    
       int mainSleepTime = atoi(argv[1]); /* Time in seconds for main to sleep */
       int numProd = atoi(argv[2]); /* Number of producer threads */
       int numCons = atoi(argv[3]); /* Number of consumer threads */
    
       /* Initialize the app */
       initializeData();
    
       /* Create the producer threads */
       for(i = 0; i < numProd; i++) {
          /* Create the thread */
          pthread_create(&tid1,&attr,producer,NULL);
        }
    
       /* Create the consumer threads */
       for(i = 0; i < numCons; i++) {
          /* Create the thread */
          pthread_create(&tid2,&attr,consumer,NULL);
       }
    
       /* Sleep for the specified amount of time in milliseconds */
       //sleep(mainSleepTime);
    
       pthread_join(tid1, NULL);
       pthread_join(tid2, NULL);
    
       /* Exit the program */
       printf("Exit the program\n");
       exit(0);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a producer/consumer queue as following but I am getting ArgumentWException . Following
I have the following code but it doesnt seem to work: <td align=center style=padding-bottom:52.5px>
I have the following code but the lon/lat seems to be returning null; package
I have the following code but I am unsure how I would adjust it
I have following asp.net code but it gives error when I change dropdown selected
I have tried the following code but has no effect: Imports system.Runtime.InteropServices <DllImport(UxTheme.DLL, BestFitMapping:=False,
I have following code which works for radio buttons but need to be changed
i have following code for moving 4 picture around circular way,but it can rotate
I have following code written to run NSTimer . But the NSTimer selector is
I have the following code (in this case for two images - but in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.