ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
This is a standard producer consumer problem implemented as stated in book of Galvin in the following order :
1. get command line arguments.
2.Initialize buffer.
3.Create producer threads.
4.create consumer threads.
5.sleep
6.exit
the problem is when the parent thread is going to sleep, no producer or consumer are trying to access the buffer. I am baffled by this..please help me out of this.
Obviously, we're not going to do your homework for you, but perhaps I can help you sort-out the problem.
Producer/Consumer problems involve the use of two mutual-exclusion or signaling devices. A mutex must be used to control the integrity of the data-structure, as is the case with all shared structures: you must be holding the mutex before you can examine or touch the structure. But, in addition, you need a second mechanism to deal with the situation where the queue is full. The producer, upon deducing that there is nowhere to put the value, must sleep ... without deadlocking the consumer. The consumer must signal the producer if the queue had been full and no longer is ... without the producer falling into a sleeps-forever if it happens to be "going to sleep" a millisecond after the wakeup call is fired by the consumer.
You need to study this problem, solve it for yourself, study all that has been written about it. Solve it for yourself. You need to study this problem.
sleep is provided in parent thread to let producer and consumer threads so some activities in the program. but maximum times when ever parent thread goes to sleep nothing happens...no producer or consumer tries to access the critical section. I am pasting here the logs of what I did...for same input the output were quite different.
[vikas@localhost vikas]$ ./a.out 4 1 3
0th producer thread created
0th consumer thread created
1th consumer thread created
2th consumer thread created
going to sleep---------------------------------------------------------------------------------->>> here something is happening after parent thread goes to sleep.
producer produced 1957747793
producer locked it
prod: H-->2 T-->3
producer UNlocked it
producer produced 719885386
producer locked it
prod: H-->2 T-->4
producer UNlocked it
consumer locked it
cons: H-->3 T-->4
consumer UNlocked it
consumer consumed
consumer locked it
cons: H-->4 T-->4
consumer UNlocked it
consumer consumed
I am awake------------------------------------------------------------------------------------->>>>parent thread sleep ends
buffer-->0 buffer-->0 buffer-->1957747793 buffer-->719885386 buffer-->0 buffer-->4
going to sleep------------------------------------------------------------------>>> nothing
happens btw
I am awake----------------------------------------------------------------------->>>these two.
buffer-->0 buffer-->0 buffer-->846930886 buffer-->0 buffer-->0 buffer-->3
I know that there are some rand functions involved which can change the output but still if the parent is sleeping for 4 seconds...its enough time for producers and consumers to have a go.
You need to study this problem, solve it for yourself, study all that has been written about it. Solve it for yourself. You need to study this problem.
Very well put. The problem is not really that complex.
Quote:
Originally Posted by sundialsvcs
Producer/Consumer problems involve the use of two mutual-exclusion or signaling devices. A mutex must be used to control the integrity of the data-structure,
Well, one can use two semaphores (assuming the semaphore has more states than the queue has elements), or atomic counters, so a mutex is not strictly required.
My favorite variant of the problem involves a singly-linked list, and either one producer and multiple consumers, or multiple producers and one consumer, handled locklessly (using compare-and-swap, requiring nonstandard built-ins). Since there is no bound to the queue size, producers never wait. Since a producer always knows if it added an item to an empty list, they can also post a semaphore (or signal a condition variable protected by a mutex) to wake up a waiting consumer.
The real-world case usually involves two such structures: one with empty list items, and one with used items. (The producers are then consumers of the empty items.) That way the queue is naturally synchronized and limited in size, but easily growable/shrinkable when needed. If instead of waiting on an empty list you switch the role (consumer/producer) of a thread, you get a dynamically balancing system, with surprisingly small amount of code. I personally like the robustness of it.
Quote:
Originally Posted by sundialsvcs
The consumer must signal the producer if the queue had been full and no longer is ... without the producer falling into a sleeps-forever if it happens to be "going to sleep" a millisecond after the wakeup call is fired by the consumer.
Exactly. This is why using a semaphore for this second purpose is not recommended.
vikasbansal27, pthreads has a condition variable type, pthread_cond_t, and associated functions pthread_cond_wait() and pthread_cond_signal(), which are designed for exactly this purpose.
When you hold a mutex, but have to wait, you can wait on a condition variable. When you wait on a condition variable, you must hold some mutex. The mutex is unlocked and the thread will wait for a signal or a broadcast on the condition variable atomically: there is no possibility of a signal slipping by. When someone signals (waking up one) or broadcasts (waking up all) on the condition variable, (each of) the woken up thread(s) will automatically atomically re-acquire the lock before it returns from the condition variable wait call.
Let me reiterate: during this, the thread will be holding the mutex, waiting on the condition variable with mutex unlocked, waiting to lock the mutex, or holding the mutex, without any gaps in between. There is no danger of missing a signal or a broadcast.
Furthermore, it is perfectly okay to signal on a condition variable without any threads waiting on it: nothing will happen then. Unlike semaphores, the condition variable itself has no state; if there are no waiters, a signal or broadcast just vanishes, it is not recorded in the condition variable. A wait on a condition variable does not depend on any previous signals or broadcasts done on it; it will always wait until the next one.
Usually, each condition variable is associated with one mutex. The mutex can be locked by any time regardless of the condition variable. (It must always be unlocked by that same thread, either explicitly, or by waiting on a condition variable and specifying that locked mutex.) The condition variable can be signaled or broadcast by any thread at any time; no need to hold the mutex at the same time. If a thread does hold the mutex while signaling on the condition variable, the woken up thread will continue only after the mutex is unlocked first.
There is also a pthread_cond_timedwait() function, which works just like pthread_cond_wait(), except that you also set a wall clock time at which point the thread will wake up just as if the condition variable had been signaled or broadcast on. (There may be a small delay in waking up, since the thread will re-acquire the mutex before it returns from the call even if timeout occurs.)
Last edited by Nominal Animal; 06-01-2012 at 07:37 PM.
Thank you 'sundialsvcs' and 'Nominal Animal' for you replies.
I the point which you two are trying to make. I am using Sem_wait after acquiring the mutex lock which may result in deadlock condition. But If I change the order i.e use Sem_wait before the mutex lock, I can make the threads wait before they acquire any lock. Suppose buffer is full and producer is trying to access the critical section, it will be waiting on sem_wait(empty). Now consumer can acquire the lock and give a sem_post(empty) so that producer can produce again. If I am going in the right direction, then will there be any need for conditional variable now?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.