同一台机器上的进程间通信,信号还是socket,如何决定?
在我看来,signal
和socket
都可以用于这项工作,
你如何决定实际使用哪一个?
It seems to me that both signal
and socket
can be used for this job,
how do you decide which one to use actually?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用信号进行 IPC 有点不方便且原始。您确实应该在 Unix 套接字(而不是 TCP 套接字!)和管道之间进行选择。
管道通常更容易编程,因为它们保证
PIPE_BUF
大小下的单个写入
是原子的。然而它们确实有其局限性。例如,当写入器比读取器快时,当管道缓冲区已满时,写入器开始阻塞。默认情况下,该缓冲区的大小约为 64k,并且在不重新编译内核的情况下无法更改它,至少在 Linux 中是这样。管道也是单向的,这意味着您必须在每个进程中保留一对管道,一个用于读取,一个用于写入。Unix 套接字具有可配置的发送缓冲区大小和更高级的编程接口。
Using signals for IPC is sort of inconvenient and primitive. You should really be choosing between Unix sockets (not TCP ones!) and pipes.
Pipes are generally easier to program with, since they guarantee that a single
write
under the size ofPIPE_BUF
is atomic. They do have their limitations however. For example, when the writer is faster than the reader, the writer starts to block when the pipe buffer gets full. The size of this buffer by default is around 64k, and it cannot be changed without recompiling the kernel, at least in Linux. Pipes are also unidirectional, which means that you'll have to keep a pair of pipes in each process, one for reading and one for writing.Unix sockets have a configurable send buffer size and a more advanced programming interface.