Understanding of semaphore

Thread Starter

King2

Joined Jul 17, 2022
155
Hello everyone,

I've been reading about semaphores in operating systems, but I'm struggling to grasp their concept fully. Could someone please explain semaphores in a way that's easy to understand, and perhaps provide some practical examples of how they're used in real-world scenarios within an OS?


Let's say we have a shared resource, like a printer, that multiple threads need to use concurrently. We want to ensure that only one thread can print at a time to avoid conflicts. What would you use mutex or semaphore.

I would use mutex to protect shared resource because I don't understand semaphore

Thank you in advance."
 

Ya’akov

Joined Jan 27, 2019
8,487
A mutex is a simplified and specialized kind of binary semaphore. A mutex is a system-wide resource with a very simple acquire-release interface. Unlike a plain semaphore, the mutex can’t be used by more than one process or thread at a time—hence the name mutually exclusive (semaphore).

A mutex guarantees that a race condition will be avoided, but it doesn’t prevent spinlock. A spinlock on a mutex, though, can be less costly than one on a semaphore because the interface takes fewer cycles.

Using mutex to protect a system resource from race condition failures is the right way to do things and it is very much like using a lock or semaphore except that its simple and specialized interface is made for purpose. It ensures compatibility with other code not written by you that won’t know about some other semaphore you might have implemented for the purpose.
 

FlyingDutch

Joined Mar 16, 2021
82
Hello everyone,

I've been reading about semaphores in operating systems, but I'm struggling to grasp their concept fully. Could someone please explain semaphores in a way that's easy to understand, and perhaps provide some practical examples of how they're used in real-world scenarios within an OS?


Let's say we have a shared resource, like a printer, that multiple threads need to use concurrently. We want to ensure that only one thread can print at a time to avoid conflicts. What would you use mutex or semaphore.

I would use mutex to protect shared resource because I don't understand semaphore

Thank you in advance."
hello,

semaphore is a mutex that can count to more than one (mutex can count to one only). When a thread takes mutex or semaphore it decreases its counter by one. When the thread takes mutex it decreases its counter to 0 - so no one thread can take it until thread does not release the mutex. So only one thread has access to resources protected by mutex. A semaphore is a mutex that has an internal counter greater than one. Let's assume that our semaphore has a counter initially set to 4 - when the thread takes semaphore decreases its counter by one, and the counter has now a value 3 and is still greater than one. Few threads can take semaphore until its counter is greater than 0. So semaphores are used to restrict access to resources to a few threads (for example a pool of memory buffers). But "binary semaphore" is in its properties similar to mutex (there are more properties that are different for mutex and semaphore, but it is more advanced).

Se these two parts of the Youtube tutorial of "FrreRTOS":



Regards
 
Last edited:
Top