1
votes

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?

1
What "shell" mutex/semaphore mechanism in particular are you talking about? shells don't provide that functionality in general. Shells do provide file locking which can be used to achieve some of the functionality of mutexes. But it is not clear what you are referring to exactly. - kaylum
My bad.. It's C not shell - Dark Inspiration
Not quite.. I saw that thread before didn't help.. - Dark Inspiration
It didn't help because? It answers exactly: "do they synchronize both threads and processes or only threads, and only processes". If you say more precisely why it didn't help then someone can address the gap. - kaylum

1 Answers

1
votes

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.