简体   繁体   English

叉和管的创建

[英]Fork and pipe creation

My book on C applied to Linux, says that if a process creates a child with a fork() , then the pipe created between them follow this principle: 我的C语言书籍适用于Linux,它说如果进程使用fork()创建一个子进程,则在它们之间创建的管道将遵循以下原则:

It is important to notice that both the parent process and the child process initially close their unused ends of the pipe 重要的是要注意,父进程和子进程最初都会关闭未使用的管道末端

If both processes start with their pipe-end closed, how they know when the other is free to communicate? 如果两个进程都以其管道末端关闭开始,那么他们如何知道另一个何时可以自由通信? Maybe, is there an intermediate buffer between the processes? 也许,进程之间是否有中间缓冲区?

Pipes on computers works very much like pipes in real life. 计算机上的管道非常类似于现实生活中的管道。 There are two ends, you put something into one end and it comes out the other end. 有两个末端,您将某物放入一个末端,然后从另一末端出来。

Normally when using pipes in a program, you usually only want the input-end, where you write data, or you want the output-end, where data is read from. 通常,在程序中使用管道时,通常只需要输入端(在其中写入数据)或输出端(在其中读取数据)。 If the parent process only wants to write to the child process, and the child process only reads from the parent process, then the parent process could close the read end after the fork , and the child process can close the write end. 如果父进程仅想写入子进程,并且子进程仅从父进程读取,则父进程可以在fork之后关闭读取端,而子进程可以关闭写入​​端。

Pipe is an interprocess communication mechanism provided by the kernel. 管道是内核提供的一种进程间通信机制。 A process writing on the pipe need not worry whether there is some other process to read it. 写在管道上的进程不必担心是否还有其他进程可以读取它。 The communication is asynchronous. 通信是异步的。 The kernel takes care of the data in transit. 内核负责传输中的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM