Understanding the posix interprocess semaphore

 ̄綄美尐妖づ 提交于 2019-12-01 22:14:40

The wording in the manual page is kind of ambiguous.

If pshared is nonzero, then the semaphore is shared between processes, and should be located in a region of shared memory.

Since a child created by fork(2) inherits its parent's memory mappings, it can also access the semaphore.

Yes, but it still has to be in a shared region. Otherwise the memory simply gets copied with the usual CoW and that's that.

You can solve this in at least two ways:

  • Use sem_open("my_sem", ...)
  • Use shm_open and mmap to create a shared region

An excellent article on this topic, for future passers-by:

http://blog.superpat.com/2010/07/14/semaphores-on-linux-sem_init-vs-sem_open/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!