Using mutexes/semaphores with processes

ipc, linux, mutex, process

Solution

Yes, it is possible. There are many ways to synchronize different processes. Perhaps the most popular solutions for mutual exclusion in this field are System V IPC semaphores and atomic operations on shared memory. I recommend you read chapter 5 of David A Ruslin's book called Interprocess Communication Mechanisms, or better yet - the whole book.

As for your second question, most modern operating systems on commodity hardware would place processes in different address spaces, though it is also possible for processes to share the same address space (see Virtual Memory, Memory Protection). Either way, if IPC mechanism is handled by the kernel, then two processes would refer to the same "kernel object", as you said. In cases where mutual exclusion is implemented (almost) without the kernel (like spin locks of some sort that use "shared memory"), both processes would refer to the same physical memory even though their virtual addresses for that memory might be different.

Hope it helps. Good Luck!

Problem

Almost all the code and tutorials that I have read online so far involve using mutexes and semaphores for synchronisation amongst threads. Can they be used to synchronise amongst processes? I'd like to write code that looks like this: ``` void compute_and_print() { // acquire mutex // critical section // release mutex } void main() { int pid = fork(); if ( pid == 0 ) { // do something compute_and_print(); } else { // do something compute_and_print(); } } ``` - Could someone point me towards similar code that does this? - I understand that different processes have different address spaces, but I wonder if the above would be different address spaces, however, wouldn't the mutex refer to the same kernel object?

Original source

Related problems