Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

182
Views
Using mutexes/semaphores with processes

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?
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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!

over 4 years ago · Santiago Trujillo Report

0

Just to be clear, POSIX (and linux) support two separate families of semaphores that have two different interfaces and methods of usage.

There is the older SysV semaphores consisting of semget, semop, semctl and (somewhat optionally) ftok.

The more modern "posix" semaphores consist of sem_open/sem_init, sem_wait, sem_post, sem_close and sem_unlink.

The setup/usage regimes and capabilities are different enough that it is worth familiarizing yourself with both to see what is better for your use case.

You can also use process shared mutexes from the pthreads package.

over 4 years ago · Santiago Trujillo Report

0

Sounds like you're looking for System V IPC You would probably use a semaphore to synchronize between processes.

Here is a nice introduction to Sys V IPC in Linux

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!