Implementation of Semaphores and Mutexes

Joined
Sep 8, 2020
Messages
1
Hello everybody. Does anybody have experience or knowledge with the real implemtation of Semaphore within Operating System. Are they at the end variables in Kernel (most likley) which are places somewhere in memory, or their representation is on some special registers? Which makes it very architecture dependable?

Does anybody know?

Thank you

BR Chris
 
Most modern cpu architectures have a test-and-set (lock) instruction the provides the necessary atomic operation for building something like a semaphore.

That said, the implementation is very architectural dependent.

For really old architectures, that might be a bit more complex. 30 years ago, locking instructions were around, but adding increment atomicity was almost always a second if not third instruction. The critical sections required on those old CPUs would definitely generate multiple lines of assembly.

You could always look at something open source, such as any Linux distro, to see how they do it. Without looking myself, I'm guessing Linux uses the test-and-set instruction, when available, then falls back on the next best thing (i.e. they had to get creative).

I've built a semaphore using C++11's conditional_variable, but I never bothered to look at the assembly it generated. I guess I was never really that curious.
 
Last edited:
Back
Top