简体   繁体   English

Pipe,Fifo,read()和write()函数

[英]Pipe, Fifo, read() and write() functions

I am just learning about pipes, fifo and I have a question. 我正在学习管道,fifo,我有一个问题。

What happen if one program opens a pipe for reading and another program opens a pipe for writing. 如果一个程序打开一个管道进行读取,而另一个程序打开一个管道进行写入,会发生什么情况。 The two programs run in paralel. 这两个程序并行运行。 Let's say that the first program tries to read form pipe, but there is no information so it's block until the second program writes something. 假设第一个程序尝试读取表单管道,但是由于没有任何信息,因此直到第二个程序写东西之前它一直处于阻塞状态。

In the second step, the first program is faster, and tries to read again form pipe. 在第二步中,第一个程序更快,并尝试再次读取管道。 This time there is some information from the last time. 这次有一些上次的信息。 What does the program do? 该程序做什么? Does it read the information from the last time? 它是否从上次读取信息?

Thanks! 谢谢!

If I interpret your question correctly, the answer is NO. 如果我正确解释了您的问题,答案是否定的。 Any information read by the first read is consumed and no longer available in the pipe. 第一次读取的所有信息都会被消耗,并且在管道中不再可用。 If nothing further has been written to the pipe, the second read will block. 如果没有进一步写入管道,则第二次读取将被阻塞。

Yes, the program which read from the pipe will read everything in the order as the other program has written it, but not necessarily in the same chunk sizes. 是的,从管道读取的程序将按照其他程序编写的顺序读取所有内容,但不一定要使用相同的块大小。 And if there is something available, it will read it (or as much of it that fits into the read buffer). 并且,如果有可用的东西,它将读取它(或放入读缓冲区的尽可能多的东西)。

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

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