Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

184
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda