Mutex and Semaphore

Thread Starter

Sparsh45

Joined Dec 6, 2021
143
I am trying to understand what is mutex and semaphore in any operating system? For this I have read some website pages but I am not able to understand clearly.

Both are concerned with restricting access to shared resources in a multiprocessing environment.

For example there are two tasks and both the tasks share the same resource i.e. CPU time. Both tasks cannot use the same resource at the same time in a single core processor.
 

ApacheKid

Joined Jan 12, 2015
1,762
I am trying to understand what is mutex and semaphore in any operating system? For this I have read some website pages but I am not able to understand clearly.

Both are concerned with restricting access to shared resources in a multiprocessing environment.

For example there are two tasks and both the tasks share the same resource i.e. CPU time. Both tasks cannot use the same resource at the same time in a single core processor.
The exact details of behavior are sometimes implementation dependent.

This explains these ideas for Windows with the Win32 "native" API, Mutex and Semaphore.
 

Thread Starter

Sparsh45

Joined Dec 6, 2021
143

drjohsmith

Joined Dec 13, 2021
1,576
https://www.geeksforgeeks.org/mutex-vs-semaphore/amp/

https://www.guru99.com/mutex-vs-semaphore.html

For example there are two tasks and both the tasks share the same resource i.e. CPU time. Both tasks cannot use the same resource at the same time in a single core processor.

What should be used in this situation Mutex or Semaphore
As you see from those excellent descriptions
They both achieve the same thing
Selecting what is running on a single processor.
The difference ,
There is imho no best , it's system responsive.
Sorry no use a or b answer
 

nsaspook

Joined Aug 27, 2009
16,263
The differences can be critical in some OS applications.

https://barrgroup.com/embedded-systems/how-to/rtos-mutex-semaphore
The cause of the widespread modern confusion between mutexes and semaphores is historical, as it dates all the way back to the 1974 invention of the Semaphore (capital "S", in this article) by Djikstra(link is external). Prior to that date, none of the interrupt-safe task synchronization and signaling mechanisms known to computer scientists was efficiently scalable for use by more than two tasks. Dijkstra's revolutionary, safe-and-scalable Semaphore was applied in both critical section protection and signaling. And thus the confusion began.

However, it later became obvious to operating system developers, after the appearance of the priority-based preemptive RTOS (e.g., VRTX, ca. 1980), publication of academic papers establishing RMA and the problems caused by priority inversion, and a paper on priority inheritance protocols in 1990, 3 it became apparent that mutexes must be more than just semaphores with a binary counter.

Unfortunately, many sources of information, including textbooks, user manuals, and wikis, perpetuate the historical confusion and make matters worse by introducing the additional names "binary semaphore" (for mutex) and "counting semaphore."

I hope this article helps you understand, use, and explain mutexes and semaphores as distinct tools.
https://barrgroup.com/embedded-systems/how-to/rtos-priority-inversion
 
Top