Skip to content
Advertisement

Semaphores and Mutex behaviour doubts

Do semaphores and mutexes synchronize both threads and processes, or only threads, or only processes?

Edit: my mistake it’s C, not shell. Since I programmed through the shell I mistook it. And one more thing: are communication and synchronization the same?

Advertisement

Answer

SysV IPC semaphores synchronize between processes.

With POSIX semaphores, you can choose via the pshared argument to sem_init(3) With pshared=1, you can synchronize among all tasks (possibly both threads and processes) that have access to the region of shared memory that you’ve placed the semaphore at.

Pthread mutexes work on threads. (Pthreads also has condition variables which are functionally equivalent to semaphores).

The thread versions can have better performance as it’s the process that can manage the shared counter, whereas with semaphores, the kernel has to do it.

Synchronization is about determining whether a task can go on or has to wait in coordination with other tasks, and it involves communication, e.g., via incrementing and decrementing semaphores, which are shared among tasks.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement