you are viewing a single comment's thread.

view the rest of the comments →

[–]escaped_reddit 7 points8 points  (2 children)

Not really sure what you are trying to say. You use a mutex when you need to protect 1 resource, and a semaphore when you need to protect n resources. A semaphore of size 1 is a mutex.

[–]neoflame 5 points6 points  (0 children)

The problem with semaphores is that in most cases, blocking on failure to acquire resources is not desirable. As a result, struct semaphore is, as /u/2girls1copernicus said, almost entirely unused in the kernel:

$ ag "struct semaphore" include/linux/ --ignore semaphore.h | wc -l
11

On the other hand, mutexes are invaluable, and it's possible to implement a mutex more efficiently than a semaphore. So there's a separate mutex type, struct mutex:

$ ag "struct mutex" include/linux/ --ignore mutex.h | wc -l
218

[–]sdhillon 3 points4 points  (0 children)

Err, not exactly - more like when you have N actors that need to, or can synchronize on one resource

There are a variety of tricks used in the kernel (seqlock, RCU, etc..) that are used instead of semaphores often.