回复:共享内存和信号量

发布于 2024-07-20 09:59:02 字数 49 浏览 7 评论 0原文

IPC 机制是使用共享内存和信号量进行同步,是像管道一样单工还是像消息队列一样双工?

Is an IPC mechanism using shared memory and semaphores for synchronization simplex like pipes or duplex like message queues?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我是有多爱你 2024-07-27 09:59:02

如果我对你的问题的理解是正确的,那么它是双重的。

通过共享内存,两个进程可以双向通信,而不仅仅是一个作为读取者,一个作为写入者。 管道只允许读取或写入,但您可以通过使用两个管道来克服这个问题(尽管消息队列是更好的选择)。

If my understanding of your question is correct, it is duplex.

With shared memory, both processes could communicate both ways, not just with one as the reader and one as the writer. Pipes only allow either reading or writing, but you can overcome this by using two pipes (although message queues are a better option).

秋风の叶未落 2024-07-27 09:59:02

信号量的工作原理如下...
proc a:“资源可用吗?” 信号量 = -2 是的。 信号量++
proc b:“资源是否...” semaphore = -1 是的。 信号量++
proc c: "是资源..." semaphore = 0 否。(什么也没有发生)

此时,proc c 可以排队(取决于你的 api,这可能是一个繁忙的循环,也可能是一个回调,或者你可能只需生成一个等待线程并编写自己的回调)

proc a: "im done" semaphore--;

proc c 会注意到信号量可用,通过类似于我之前提到的方式。

我写下所有这些的原因是我可以说,两者兼而有之。 它就像一个消息队列,您可以让线程等待资源(由信号量控制的共享内存),当它们获取资源时,它们会触发某些操作,甚至是实际的系统消息。 或者你可以只是忙着等待直到它完成,这就像管道一样。

A semaphore works like this...
proc a: "Is the resource available?" semaphore = -2 Yes. semaphore++
proc b: "Is the resource..." semaphore = -1 Yes. semaphore++
proc c: "is the resource..." semaphore = 0 No. (nothing happens)

at this point, proc c can queue (depending on your api, this might be a busy loop or it might be a callback, or you might simply spawn a waiting thread & write your own callback)

proc a: "im done" semaphore--;

proc c will notice that semaphore is available, through something probably similar to what I mentioned previously.

the reason I wrote all that out is so I could say, its both. Its like a message queue in that you can have threads waiting on a resource (shared memory controlled by a semaphore) that trigger some action, even an actual system message, when they get the resource. Or you could just busy-wait until it's done, and that would be like piping.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文