Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何使用未命名的信号量在两个进程之间提供同步?_C_Multithreading_Semaphore - Fatal编程技术网

C 如何使用未命名的信号量在两个进程之间提供同步?

C 如何使用未命名的信号量在两个进程之间提供同步?,c,multithreading,semaphore,C,Multithreading,Semaphore,谁能解释一下“如何使用未命名的信号量在两个进程之间提供同步?”。使用信号量时,需要哪些函数调用以及信号量如何共享共享内存区域。mmap()函数调用在同步中有什么用途。来自sem\u init手册页面: If pshared is non-zero, then the semaphore is shared between processes, and should be located in a region of shared memory (see shm_open(3),

谁能解释一下“如何使用未命名的信号量在两个进程之间提供同步?”。使用信号量时,需要哪些函数调用以及信号量如何共享共享内存区域。mmap()函数调用在同步中有什么用途。

来自
sem\u init
手册页面:

   If pshared is non-zero, then the semaphore is shared between processes,
   and should be located in a region of shared  memory  (see  shm_open(3),
   mmap(2),  and  shmget(2)).   (Since a child created by fork(2) inherits
   its parent's memory mappings, it can also access the  semaphore.)   Any
   process  that  can  access  the shared memory region can operate on the
   semaphore using sem_post(3), sem_wait(3), etc.
因此,您应该使用
shm_open
+
mmap
shmget
+
shmat
创建和连接共享内存。然后使用
sem\u init
在地址处创建未命名的信号量。通过使用
fork()
系统调用创建的子进程继承父进程的内存映射,因此您也可以在子进程中访问未命名的信号量