linux的管道

Posted on 2009-04-16 20:31  wanderxjtu  阅读(324)  评论(0)    收藏  举报
来自man手册:

Pipe Capacity
       A pipe has a limited capacity.  If the pipe is  full,  then  a  write(2)
       will block or fail, depending on whether the O_NONBLOCK flag is set (see
       below).  Different implementations have different limits  for  the  pipe
       capacity.   Applications  should  not  rely on a particular capacity: an
       application should be designed so that a reading process  consumes  data
       as  soon  as  it is available, so that a writing process does not remain
       blocked.

       In Linux versions before 2.6.11, the capacity of a pipe was the same  as
       the  system  page  size (e.g., 4096 bytes on i386).  Since Linux 2.6.11,
       the pipe capacity is 65536 bytes
.

PIPE_BUF
       POSIX.1-2001 says that write(2)s of less than  PIPE_BUF  bytes  must  be
       atomic: the output data is written to the pipe as a contiguous sequence.
       Writes of more than PIPE_BUF bytes may be  non-atomic:  the  kernel  may
       interleave  the data with data written by other processes.  POSIX.1-2001
       requires PIPE_BUF to be at least 512 bytes.  (On Linux, PIPE_BUF is 4096
       bytes
.) 

shell的pipe使用的就是unix编程中的pipe()。

shell在执行一个 `a | b`时,先pipe(),然后fork一个子shell,stdout重定向到pipe写端,exec a,fork另一个子shell,stdin重定向到读端,exec b,然后在两个子进程中就能通过pipe通信了。

所以在linux中就尽管用管道吧,没有内存的问题。





博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3