named and unnamed posix semaphores

我只是一个虾纸丫 提交于 2019-12-07 05:26:56

问题


Planning to use a posix semaphore to sync 2 processes. Not quite sure which to use - named or unnamed.

What are the advantages and disadvantages of each? How do I decide which to use? On which situations, is one preferable over the other?

Thanks.


回答1:


If the two processes are unrelated you should use a named semaphore. If the two process are related (i.e. forked) or if you are just using the semaphore between threads you should use unnamed.

The advantages of unnamed are that you don't have to keep track of the names and any permissions nor unlink them. And unnamed semaphores can be use as a simple global variable (or on the heap) in the case where they are being shared between threads of the same process, or put in shared memory which will be inherited by the children in the case of a forked process.



来源:https://stackoverflow.com/questions/11788605/named-and-unnamed-posix-semaphores

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