任何人都可以解释“如何使用未命名信号量在两个进程之间提供同步?”。使用信号量时需要哪些函数调用以及信号量如何共享共享内存区域。 mmap()函数调用在同步中有什么用。
最佳答案
来自 sem_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_init 在该地址创建未命名信号量。使用 fork()
系统调用创建的子进程继承父进程的内存映射,因此您也可以在子进程中访问未命名的信号量。
关于c - 如何使用未命名信号量在两个进程之间提供同步?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29209341/