Thread: Consumer/Producer Problem

  1. #1
    Registered User
    Join Date
    Apr 2016
    Posts
    2

    Consumer/Producer Problem

    Hello everyone,

    I have been assigned a piece of coursework from to implement a solution for the consumer/producer problem that uses a single producer, a single consumer, and a circular buffer. This should be written in C.

    Unfortunately, we have been provided with no learning material and are somehow meant to implement this with no help from the lecturer.

    the requirement is as follows:


    • Requirement: You are asked to implement a solution for the consumer/producer problem that uses asingle producer, a single consumer, and a circular bounded buffer (i.e. of a fixed size containing, e.g.,integer values). Both the producer and consumer generate/consume an unlimited number of items(e.g. integers). I.e., they can be executed in an infinite loop, assuming that the code runs indefinitely.You are free to choose how to implement this approach, however, it may be recommended to usesemaphores. A successful implementation has:
      • A circular buffer of which the size is fixed and configurable (e.g. using a define statement)
      • Counters that keeps track of the number of items that have been produced/consumed so far(you ignore wrap-arounds of ints/longs)
      • An identification number for every item produced, e.g. its index in the sequence of produceditems
      • A simple visualisation that demonstrates how the number of items in the buffer evolves overtime. This can be as simple as printing a number of stars every time an item isproduced/consumed, with each star representing one element currently in the buffer
      • Correct synchronisation of any critical sections that may be present, and only where it is strictlynecessary to do so


    If anyone can help me out with how to approach this requirement it would be extremely grateful

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    The very first step is to put aside the computer and develop a plan. Break down the problem into pieces and organize these pieces in a way to help you "visualize" the problem.

    Then you can create a rough list of "steps" your program will have to follow in order to solve the problem. Additionally, figure out what kinds of data your program will require (these will eventually become your variables and constants).

    Once you have a list of steps, go through them again and break them down further into even smaller steps. This will leave you with pseudo-code, which allows you to easily check the flow of logic for correctness, before going anywhere near real code.

    Now that you have a solid plan, the question of "where to start" should be obvious. Start developing your code, based on the steps you already determined. Go slowly and carefully - write enough code to do one small thing, compile, test, and fix. When you're sure that works, add a little more code to do the next small thing, compile, test, and fix. Build your program up gradually testing along the way. See here: A development process

    If you get stuck anywhere in the process, ask specific questions about the problem you're having, and you will likely get more specific answers.

  3. #3
    Registered User
    Join Date
    Apr 2016
    Posts
    2
    This would work but I don't know where to begin.

    Would you know how to fulfil this requirement?

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by JacobSmith1996 View Post
    This would work but I don't know where to begin.

    Would you know how to fulfil this requirement?
    I told you where to begin, with a plan.

    There is a lot more to programming than simply writing code. Problem-solving is a very important skill you need to cultivate. This includes learning how to break a problem down into components in order to first understand, then solve, it.

    You have showed no apparent effort yet, outside of posting your assignment verbatim, and have asked no specific questions - so there is little help I can offer at this point.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Last edited by Matticus; 04-14-2016 at 01:01 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Producer-Consumer problem
    By mistereff in forum C++ Programming
    Replies: 12
    Last Post: 11-29-2012, 04:09 PM
  2. consumer producer problem with threads
    By nabi in forum C Programming
    Replies: 3
    Last Post: 08-27-2010, 09:51 AM
  3. Producer/Consumer Problem
    By AvengedGoat in forum C++ Programming
    Replies: 5
    Last Post: 03-21-2010, 12:33 PM
  4. pthread producer consumer problem
    By Amit Chikorde in forum C Programming
    Replies: 1
    Last Post: 01-17-2007, 07:39 PM
  5. Producer consumer problem
    By traz in forum C Programming
    Replies: 2
    Last Post: 11-08-2002, 08:04 PM

Tags for this Thread