Thread: Adding and removing items from the bounded buffer (producer/consumer problem).

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    12

    Adding and removing items from the bounded buffer (producer/consumer problem).

    I'm trying to write a program in C where I remove, add, and initialize a bounded buffer for the consumer/producer problem. Here's what my bounded buffer program looks like:
    Code:
    #include <pthread.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "bbuffer.h"
    
    int count;
    pthread_mutex_t mutex;
    int in, out;
    int bounded_buffer[BOUNDED_BUFFER_SIZE];
    
    
    void initialize_bounded_buffer() {
        int status;
        count = 0;
        pthread_mutex_lock(&mutex);
        status = pthread_mutex_init(&mutex, NULL);
        if (status != 0) {
            fprintf(stderr, "Error creating buffer_mutex\n");
        }
        pthread_mutex_unlock(&mutex);
    }
    
    
    void add_to_buffer(int value) {
            pthread_mutex_lock(&mutex);
            while (count == BOUNDED_BUFFER_SIZE){
            //make it wait
    }
            if (count < BOUNDED_BUFFER_SIZE) {
                bounded_buffer[count] = value;
                count++;
                printf("added");
          } else {
                printf("buffer full");
            }
            pthread_mutex_unlock(&mutex);
    }
    
    
    int remove_from_buffer() {
        for (;;){
            pthread_mutex_lock(&mutex);
            while (count == 0) {
            //make it wait
    }
            if (count > 0){
                bounded_buffer[count] = bounded_buffer[count-1];
                count--;
                printf("removed");
                return 0;
            } else {
                printf("could not remove");
                return -1;
            }
            pthread_mutex_unlock(&mutex);
        }
    }
    Now one thing that confuses me is that in order to implement this I need to use two condition variables but I'm not sure what the condition variables would be used for and how they would work for adding/removing an item from the bbuffer. I'm also trying to implement a better way for the program to wait as opposed to just having an infinite while loop.

    Also here's my bbuffer.h file:
    Code:
    #ifndef _BBUFFER_H_
    #define _BBUFFER_H_
    
    #define BOUNDED_BUFFER_SIZE 4
    
    void initialize_bounded_buffer();
    void add_to_buffer(int);
    int  remove_from_buffer();
    
    #endif

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You can't lock a mutex, and then initialise it!
    Code:
        pthread_mutex_lock(&mutex);
        status = pthread_mutex_init(&mutex, NULL);
        if (status != 0) {
            fprintf(stderr, "Error creating buffer_mutex\n");
        }
        pthread_mutex_unlock(&mutex);
    You just initialise it, that's all.

    > I'm also trying to implement a better way for the program to wait as opposed to just having an infinite while loop.
    That's what the condition variables are for.
    Using Condition Variables (Multithreaded Programming Guide)
    Scroll down to Example 4-8 Using pthread_cond_wait() and pthread_cond_signal()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You also don't seem to understand how a circular buffer works.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by algorism View Post
    You also don't seem to understand how a circular buffer works.
    Thank you; I was wondering why they seemed to be using a what looks like broken stack buffer; forgot that a circular buffer was the normal solution to this problem.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Feb 2017
    Posts
    12
    One last thing. So for removing and adding values, I'm supposed to add them in the next available "in" position and remove them from the next available "out" position. What does that mean? I also know that adding is like the producer and removing is the consumer.

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by brandon236 View Post
    One last thing. So for removing and adding values, I'm supposed to add them in the next available "in" position and remove them from the next available "out" position. What does that mean? I also know that adding is like the producer and removing is the consumer.
    That sounds more like the first thing, not the last thing. Did you read the wikipedia article about circular buffers? What didn't you understand about it? Did you read about pthread condition variables? Do you understand them?

  7. #7
    Registered User
    Join Date
    Feb 2017
    Posts
    12
    With circular buffers, I can see that it's like a queue where if you remove a value, it'll be the first one in the queue. The problem with my implementation is that I'm using a type of stack which removes the latest one added. The problem I don't know how I can store which one I added first to figure out which value to remove. Also when it comes to removing a value in C I don't think I can just set the position in the buffer to null so how does that work?

    As for pthread condition variables, I'm putting the wait in a while loop but I keep getting an error that says "expected expression before ‘pthread_cond_t' " for the line "pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);" so I don't know what's going on there. I'm also not sure about pthread_cond_signal(pthread_cond_t *cv). I know that if the buffer is either full or empty then I want to block it. I know that pthread_wait waits for the condition to no longer be true but would signal also be important for blocking?

  8. #8
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    I'm not sure what you're saying. Are you saying that you want to use a stack instead of a queue? Go ahead, then. I thought you needed a circular buffer (queue) as is usual in the producer/consumer problem.

    As for your error message, are you actually trying to use the following as a function call?
    Code:
    pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
    That's a function prototype. You obviously don't include the types when you make the call.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Consumer/Producer Problem
    By JacobSmith1996 in forum C Programming
    Replies: 4
    Last Post: 04-14-2016, 12:54 PM
  2. Producer-Consumer problem
    By mistereff in forum C++ Programming
    Replies: 12
    Last Post: 11-29-2012, 04:09 PM
  3. Replies: 3
    Last Post: 11-28-2010, 10:52 AM
  4. Replies: 3
    Last Post: 11-11-2010, 12:05 PM
  5. Producer/Consumer Problem
    By AvengedGoat in forum C++ Programming
    Replies: 5
    Last Post: 03-21-2010, 12:33 PM

Tags for this Thread