1PIPE(7) Linux Programmer's Manual PIPE(7)
2
3
4
6 pipe - overview of pipes and FIFOs
7
9 Pipes and FIFOs (also known as named pipes) provide a unidirectional
10 interprocess communication channel. A pipe has a read end and a write
11 end. Data written to the write end of a pipe can be read from the read
12 end of the pipe.
13
14 A pipe is created using pipe(2), which creates a new pipe and returns
15 two file descriptors, one referring to the read end of the pipe, the
16 other referring to the write end. Pipes can be used to create a commu‐
17 nication channel between related processes; see pipe(2) for an example.
18
19 A FIFO (short for First In First Out) has a name within the file system
20 (created using mkfifo(3)), and is opened using open(2). Any process
21 may open a FIFO, assuming the file permissions allow it. The read end
22 is opened using the O_RDONLY flag; the write end is opened using the
23 O_WRONLY flag. See fifo(7) for further details. Note: although FIFOs
24 have a pathname in the file system, I/O on FIFOs does not involve oper‐
25 ations on the underlying device (if there is one).
26
27 I/O on Pipes and FIFOs
28 The only difference between pipes and FIFOs is the manner in which they
29 are created and opened. Once these tasks have been accomplished, I/O
30 on pipes and FIFOs has exactly the same semantics.
31
32 If a process attempts to read from an empty pipe, then read(2) will
33 block until data is available. If a process attempts to write to a
34 full pipe (see below), then write(2) blocks until sufficient data has
35 been read from the pipe to allow the write to complete. Non-blocking
36 I/O is possible by using the fcntl(2) F_SETFL operation to enable the
37 O_NONBLOCK open file status flag.
38
39 The communication channel provided by a pipe is a byte stream: there is
40 no concept of message boundaries.
41
42 If all file descriptors referring to the write end of a pipe have been
43 closed, then an attempt to read(2) from the pipe will see end-of-file
44 (read(2) will return 0). If all file descriptors referring to the read
45 end of a pipe have been closed, then a write(2) will cause a SIGPIPE
46 signal to be generated for the calling process. If the calling process
47 is ignoring this signal, then write(2) fails with the error EPIPE. An
48 application that uses pipe(2) and fork(2) should use suitable close(2)
49 calls to close unnecessary duplicate file descriptors; this ensures
50 that end-of-file and SIGPIPE/EPIPE are delivered when appropriate.
51
52 It is not possible to apply lseek(2) to a pipe.
53
54 Pipe Capacity
55 A pipe has a limited capacity. If the pipe is full, then a write(2)
56 will block or fail, depending on whether the O_NONBLOCK flag is set
57 (see below). Different implementations have different limits for the
58 pipe capacity. Applications should not rely on a particular capacity:
59 an application should be designed so that a reading process consumes
60 data as soon as it is available, so that a writing process does not
61 remain blocked.
62
63 In Linux versions before 2.6.11, the capacity of a pipe was the same as
64 the system page size (e.g., 4096 bytes on x86). Since Linux 2.6.11,
65 the pipe capacity is 65536 bytes.
66
67 PIPE_BUF
68 POSIX.1-2001 says that write(2)s of less than PIPE_BUF bytes must be
69 atomic: the output data is written to the pipe as a contiguous
70 sequence. Writes of more than PIPE_BUF bytes may be non-atomic: the
71 kernel may interleave the data with data written by other processes.
72 POSIX.1-2001 requires PIPE_BUF to be at least 512 bytes. (On Linux,
73 PIPE_BUF is 4096 bytes.) The precise semantics depend on whether the
74 file descriptor is non-blocking (O_NONBLOCK), whether there are multi‐
75 ple writers to the pipe, and on n, the number of bytes to be written:
76
77 O_NONBLOCK disabled, n <= PIPE_BUF
78 All n bytes are written atomically; write(2) may block if there
79 is not room for n bytes to be written immediately
80
81 O_NONBLOCK enabled, n <= PIPE_BUF
82 If there is room to write n bytes to the pipe, then write(2)
83 succeeds immediately, writing all n bytes; otherwise write(2)
84 fails, with errno set to EAGAIN.
85
86 O_NONBLOCK disabled, n > PIPE_BUF
87 The write is non-atomic: the data given to write(2) may be
88 interleaved with write(2)s by other process; the write(2) blocks
89 until n bytes have been written.
90
91 O_NONBLOCK enabled, n > PIPE_BUF
92 If the pipe is full, then write(2) fails, with errno set to
93 EAGAIN. Otherwise, from 1 to n bytes may be written (i.e., a
94 "partial write" may occur; the caller should check the return
95 value from write(2) to see how many bytes were actually writ‐
96 ten), and these bytes may be interleaved with writes by other
97 processes.
98
99 Open File Status Flags
100 The only open file status flags that can be meaningfully applied to a
101 pipe or FIFO are O_NONBLOCK and O_ASYNC.
102
103 Setting the O_ASYNC flag for the read end of a pipe causes a signal
104 (SIGIO by default) to be generated when new input becomes available on
105 the pipe (see fcntl(2) for details). On Linux, O_ASYNC is supported
106 for pipes and FIFOs only since kernel 2.6.
107
108 Portability notes
109 On some systems (but not Linux), pipes are bidirectional: data can be
110 transmitted in both directions between the pipe ends. According to
111 POSIX.1-2001, pipes only need to be unidirectional. Portable applica‐
112 tions should avoid reliance on bidirectional pipe semantics.
113
115 dup(2), fcntl(2), open(2), pipe(2), poll(2), select(2), socketpair(2),
116 stat(2), mkfifo(3), epoll(7), fifo(7)
117
118
119
120Linux 2.6.15 2005-12-08 PIPE(7)