Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Producer-Consumer model - binary semaphore or mutex?

This is mainly about the understanding of the concept, which confuses me.

Mutex means that one thread takes the control of the access of shared resource, performs operations and unlocks it, then only other thread can gain access to lock

while binary semaphore is like a thread can gain access to the shared resource but gaining access to the lock, while another thread can unlock it

In the last question that I saw over here, it was mentioned that "semaphores are more suitable for some synchronization problems like producer-consumer".

My question is, for example, if producer thread has gained lock and filling up some queue with data, and consumer at the same time unlocks it, isn't there any concurrency issue??

like image 203
daydreamer Avatar asked Oct 22 '10 04:10

daydreamer


People also ask

Which semaphore is used in producer consumer?

Semaphore S: This semaphore variable is used to achieve mutual exclusion between processes. By using this variable, either Producer or Consumer will be allowed to use or access the shared buffer at a particular time.

Which is better binary semaphore or mutex?

If you have number of instances for resource it is better to use Binary semaphore. If you have single instance for resource it is better to use mutex.

Is binary semaphore and mutex same?

A Mutex is different than a semaphore as it is a locking mechanism while a semaphore is a signalling mechanism. A binary semaphore can be used as a Mutex but a Mutex can never be used as a semaphore.

What is mutex in producer consumer?

The mutex semaphore ensures mutual exclusion. The empty and full semaphores count the number of empty and full spaces in the buffer. After the item is produced, wait operation is carried out on empty. This indicates that the empty space in the buffer has decreased by 1.


1 Answers

I'll try to sort out the confusion the best I can. I will explain the concepts the way they are traditionally defined. The problem is that people start to mix the meaning of many of these concepts and a lot of confusion arise from that.

Whenever we have a piece of code that modifies a bit of memory (say a variable) that is shared between different processes or threads we have a critical section. If we don't take care to synchronize this bit of code properly then we will get bugs. One example of a critical section is a producer adding an element to a shared container of some sort.

One way to synchronize critical sections is to enforce mutual exclusion. Mutual exclusion means that only one process or thread at a time can execute the critical section and gain access to the shared piece of memory. Note that mutual exclusion is not a mechanism in it self, it is a principle that we can enforce by different means. Some people talk about locks and binary semaphores as mutexes but that mixes the concepts in a way that will lead to confusion.

A binary semaphore is a way to enforce mutual exclusion. Whenever a process wants to get access to the mutex it can aquire the semaphore. This operation will block if there is another process holding the semaphore at that moment. Hence we have mutual exclusion. Once a process is done with the mutex then we releases the semaphore letting other processes into the mutex. In this way we can achieve mutual exclusion with a binary semaphore, but it is by no means the only possible application of a binary semaphore.

Semaphores are nice for producer-consumer problems because they can take on an arbitrary natural number, not just 0 and 1 in the case of binary semaphores. This is very useful when synchronizing producer-consumer problems because you can let the value of a semaphore contain the number of available elements. If the number of elements go down to zero then the semaphore operations will automatically block.

I realize the explanation of the producer-consumer problem is a bit brief and I encourage you to look at solutions which uses semaphores and also compare these solutions to other solutions which uses other synchronization constructs such as monitors or message passing. I've found it to be very illuminating.

like image 56
svenningsson Avatar answered Sep 20 '22 10:09

svenningsson