Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Linux - Kernel This forum is for all discussion relating to the Linux kernel.


Closed Thread
  Search this Thread
Old 02-26-2012, 11:14 PM   #1
LQ Newbie
Registered: Feb 2012
Posts: 5
Blog Entries: 1

Rep: Reputation: Disabled
Post Producer and Consumer problem

6. Write a multi-threaded program to solve producer and consumer problem.
1 There are two types of worker thread: producer and consumer. A producer thread simply randomly generate an integer between 0-100 (You can use random()/srandom() or drand48()/srand48() to generate random number in C), and put it into the buffer. A consumer thread simple take the first integer from the buffer. A circular array should be used for the buffer. You do not need loop for both types of thread. Each thread only does once and exit.
2 Assume total number of worker thread is 10, and buffer size is also 10. Your program should randomly generate the number of producers and consumers. Their relationship should be : num_producers + num_consumers = num_threads, num_producer - num_consumer <= buffer_size, num_consumer <= num_producer, so that no thread will be blocked forever.
3 Synchronize the producer and consumer.
4 You can choose any type of language (e.g. C/C++/Java) to implement this program. Below is a sample of output. However, the number and the execution order in your program might be different.
Number of producer: 7 Number of consumer: 3
Thread 0 produce 63 in buffer 0, current number of items is 1
Thread 1 consume item 63 in buffer 0, current number of items is 0
Thread 3 produce 45 in buffer 1, current number of items is 1
Thread 4 consume item 45 in buffer 1, current number of items is 0
Thread 5 produce 88 in buffer 2, current number of items is 1
Thread 6 produce 96 in buffer 3, current number of items is 2
Thread 7 produce 26 in buffer 4, current number of items is 3
Thread 8 produce 52 in buffer 5, current number of items is 4
Thread 9 produce 81 in buffer 6, current number of items is 5
Thread 2 consume item 88 in buffer 2, current number of items is 4
There are 4 items left in the buffer: 96,26,52,81
Old 02-26-2012, 11:58 PM   #2
Registered: May 2001
Posts: 29,353
Blog Entries: 55

Rep: Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541
0) Per the LQ Rules, please do not post homework assignments verbatim. We're happy to assist if you have specific questions or have hit a stumbling point, however. Let us know what you've already tried and what references you have used (including class notes, books, and Google searches) and we'll do our best to help. Also, keep in mind that your instructor might also be an LQ member.
1) Please post your thread once and in only one forum. Posting a single thread in the most relevant forum will make it easier for members to help you and will keep the discussion in one place. This thread should be closed because it is a duplicate.

Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Producer / Consumer Shared Data: semaphores, kernel threads? jhwilliams Linux - Kernel 1 12-04-2011 12:59 PM
script to sync producer consumer application. ufmale Programming 2 06-08-2010 09:55 AM
How to implement a POSIX threaded program to solve producer/consumer problem cy163 Programming 3 09-13-2008 09:38 AM
Producer & Consumer issue with php on mysql ufmale Programming 3 03-13-2008 02:08 AM
The old producer-consumer problem still a problem for me!!!!!!!!!!!!! skie_knite007 Programming 1 12-14-2005 07:39 PM

All times are GMT -5. The time now is 04:08 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration