命名和未命名的posix信号量 [英] named and unnamed posix semaphores

查看:110
本文介绍了命名和未命名的posix信号量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

计划使用posix信号量来同步2个进程.不确定使用哪个-命名或未命名.

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?

谢谢.

推荐答案

如果两个进程不相关,则应使用命名信号量.如果两个进程是相关的(即分叉的),或者您只是在线程之间使用信号灯,则应使用未命名.

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.

这篇关于命名和未命名的posix信号量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆