Pthread Queue System
Posted
by Wallace
on Stack Overflow
See other posts from Stack Overflow
or by Wallace
Published on 2010-05-23T22:16:09Z
Indexed on
2010/05/23
22:20 UTC
Read the original article
Hit count: 359
Hi. I'm working on my assignment on pthreads. I'm new and never touched on pthreads before. Is there any sample codes or resources out there that anyone of you have, that might aid me in my assignment?
Here are my assignment details. A pthread program about queue system:
Write a C/C++ Pthread program for a Dental clinic’s queuing system that declares an
array of integers of size N, where N is the maximum number of queue for the day. The
pthread program uses two threads. Whenever there is a new dental appointment, the first
thread (the creator) puts the queue numbers in the array, one after the other. The
second thread (the remover) removes the queue numbers from the array whenever the
dentist has seen the patient. This is done in a FIFO fashion (First In First Out).
The algorithm of the creator is as follows:
• If the array is not full then put a new number in it (the numbers start at 1
and are incremented by one each time, so the creator create queue number 1, 2, 3 etc.)
• sleep for 1 to 10 seconds, randomly
• repeat
The algorithm of the remover is as follows:
• If the array is not empty then remove its smallest queue number
• sleep for 1 to 10 seconds, randomly
• repeat
You should use mutex locks to protect things that must be protected. Each thread
should print on the screen what it is doing (eg: "number 13 is added into the queue",
"number 7 is removed from the queue", etc.). The program should run forever.
Any help will be appreciated. Thanks.
© Stack Overflow or respective owner