[转]进程间通信—–管道

转自 https://blog.csdn.net/qq_35116371/article/details/71843606

http://www.qnx.com/developers/docs/qnx_4.25_docs/tcpip50/prog_guide/sock_ipc_tut.html

 

pipe和socket和unix domain socket

pipe和unix domain socket(socketpair)用于一个host上

net domain socket 用于多个host间 

what are the differences between pipes and sockets, and when/how should you choose one over the other?

Both pipes and sockets handle byte streams, but they do it in different ways...

  • pipes only exist within a specific host, and they refer to buffering between virtual files, or connecting the output / input of processes within that host. There are no concepts of packets within pipes.
  • sockets packetize communication using IPv4 or IPv6; that communication can extend beyond localhost. Note that different endpoints of a socket can share the same IP address; however, they must listen on different TCP / UDP ports to do so.

Usage:

  • Use pipes:
    • when you want to read / write data as a file within a specific server. If you're using C, you read() and write() to a pipe.
    • when you want to connect the output of one process to the input of another process... see popen()
  • Use sockets to send data between different IPv4 / IPv6 endpoints. Very often, this happens between different hosts, but sockets could be used within the same host

BTW, you can use netcat or socat to join a socket to a pipe.

 

it is important to mention the existence of UNIX domain sockets, which are available on any POSIX compliant operating system. Although very similar to "normal" internet sockets in terms of usage semantics, they are purely local to the machine (of course internet sockets can also work locally), and thus almost behave like a pipe. Almost, because a UNIX pipe is by definition unidirectional:

Pipes and FIFOs (also known as named pipes) provide a unidirectional interprocess communication channel. A pipe has a read end and a write end. Data written to the write end of a pipe can be read from the read end of the pipe. (excerpt from the man page pipe(7))

UNIX domain sockets also have a very unusual feature, as besides data, they also allow sending file descriptors: this way, an unprivileged process can access any file whose descriptor has been sent over the socket. This technique, according to Wikipedia, is used by the ClamAV antivirus scanning daemon.

原文链接: https://www.cnblogs.com/yi-mu-xi/p/12785162.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    [转]进程间通信-----管道

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/403461

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年4月23日 上午9:13
下一篇 2023年4月23日 上午9:13

相关推荐