Linux unix 域套接字 VS 命名管道?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9475442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
unix domain socket VS named pipes?
提问by
After looking at a unix named socket and i thought they were named pipes. I looked at name pipes and didnt see much of a difference. I saw they were initialized differently but thats the only thing i notice. Both use the C write/read function and work alike AFAIK.
在查看了一个名为 socket 的 unix 之后,我认为它们是命名管道。我查看了名称管道,并没有发现太大的不同。我看到它们的初始化方式不同,但这是我唯一注意到的。两者都使用 C 写/读函数并且工作方式与 AFAIK 相似。
Whats the difference between unix domain sockets and named pipes? When would i pick one over the other? Which should i use by default (like how i use use vector by default in C++ than use deque, list or whatever else if i have needs)?
unix 域套接字和命名管道有什么区别?我什么时候会选择一个?我应该默认使用哪个(比如我如何在 C++ 中默认使用 vector 而不是使用 deque、list 或其他任何我需要的东西)?
回答by jtoberon
One difference is that named pipes are one-way, so you'll need to use two of them in order to do two-way communication. Sockets of course are two way. It seems slightly more complicated to use two variables instead of one (that is, two pipes instead of one socket).
一个区别是命名管道是单向的,因此您需要使用其中的两个来进行双向通信。套接字当然有两种方式。使用两个变量而不是一个变量(即两个管道而不是一个套接字)似乎稍微复杂一些。
Also, the wikipedia article is pretty clear on the following point: "Unix domain sockets may be created as byte streams or as datagram sequences, while pipes are byte streams only."
此外,维基百科文章对以下几点非常清楚:“Unix 域套接字可以创建为字节流或数据报序列,而管道只是字节流。”
Named pipes are, in fact, bi-directional but half-duplex. This means that communication may go either from end A to end B, or B to A, but never both at the same time.
命名管道实际上是双向但半双工的。这意味着通信可能从 A 端到 B 端,或 B 到 A,但绝不会同时进行。
回答by caf
UNIX-domain sockets are generally more flexible than named pipes. Some of their advantages are:
UNIX 域套接字通常比命名管道更灵活。它们的一些优点是:
- You can use them for more than two processes communicating (eg. a server process with potentially multiple client processes connecting);
- They are bidirectional;
- They support passing kernel-verified UID / GID credentials between processes;
- They support passing file descriptors between processes;
- They support packet and sequenced packet modes.
- 您可以将它们用于两个以上的进程通信(例如,一个服务器进程可能连接多个客户端进程);
- 它们是双向的;
- 它们支持在进程之间传递内核验证的 UID/GID 凭据;
- 它们支持在进程之间传递文件描述符;
- 它们支持分组和顺序分组模式。
To use many of these features, you need to use the send()
/ recv()
family of system calls rather than write()
/ read()
.
要使用其中许多功能,您需要使用send()
/recv()
系列系统调用而不是write()
/ read()
。