Using mutexes / semaphores with processes

Almost all codes and tutorials that I read on the Internet include using mutexes and semaphores to synchronize between threads. Can they be used to synchronize between processes?

I would 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();
}
}
  • Can someone point me to similar code that does this?
  • I understand that different processes have different address spaces, but I think that if the above were different address spaces, would the mutex refer to the same kernel object?
+4
source share
4 answers

, . . , System V IPS . 5 , - .

, , , (. " " , " " ). , IPC , , , " ". , () (, , " " ), , .

, . !

+2

, POSIX ( Linux) , .

SysV, semget, semop, semctl ( ) ftok.

"posix" sem_open/sem_init, sem_wait, sem_post, sem_close sem_unlink.

/ , , .

pthreads.

+2
+1

mutex/semaphore's. fooobar.com/questions/26206/..., . IBM, pthread_mutexattr_setname_np. , mutex Linux 100% (.. Ubuntu, CentOS .. ..), , . , . SysV IPC.

, , - , "-linux-platform" .

+1

Source: https://habr.com/ru/post/1525338/


All Articles