Pipe is one of the message passing IPC techniques. A pipe is a one way communication channel. It is created by the pipe function,
int pipe(int fd);
The array fd returns two file descriptors referring to two ends of the created pipe: fd for reading and fd for writing. Data written to the pipe is buffered by the kernel until it is read by the read end.
Below is a program use pipe to create a two way communication channel between two processes.
Compile the code using the command,
gcc -o pipe pipe.c
And below is a screenshot of a sample run,
Figure 1. Execution of Pipe Sample Program
The program above illustrates the steps of creating two pipes and use them for two way communication.
1. Create two pipes, pipe1 (pipe1fd) and pipe2 (pipe2fd).
2. call fork to create a child process
3. child process close write end of pipe1 (pipe1fd) and read end of pipe2 (pipe2fd)
4. parent process close read end of pipe1 (pipe1fd) and write end of pipe2 (pipe2fd)
POSIX defines “half-duplex” pipes, where communication is one way in pipes. System V Release 4 (SVR4) Unix implements “full-duplex” manner, which allows the two file descriptors for both reading and writing.
GNU/Linux implements “half-duplex” in a unique manner. One does not have to close one of them in order to use the other as shown in the above program. Comment out the close statements give the program below,
Compile the code using,
gcc -o pipe2 pipe2.c
And below is a screenshot of a run,
Figure 2. Execution of Modified Pipe Sample Program
popen and pclose
Linux provides two functions (popen and pclose) to create pipe to or from a process. It avoids the usage of pipe, fork, close, and wait.
FILE *popen(const char *command, const char *type);
int pclose(FILE *stream);
popen function starts a process by creating a pipe, forking and invoking a shell. The command argument accepts a shell command line, and the command is passed to /bin/sh using -c flag. The type argument expects either “r” or “w”. Note that since a pipe is unidirectional, we cannot pass both “r” and “w”, and the returned pipe stream is either read-only or write-only.
If the returned pipe stream is read-only, the calling process reads from the standard output.
If the returned pipe stream is write-only, the calling process writes to the standard input.
pclose function waits for the associated process to terminate and returns the exit status of the command passed to popen.
Note that with popen and pclose, it is not convenient to create two pipes for two way communication. Below is a sample program using popen and pclose,
Compile the code using the command below,
gcc -o pipe3 pipe3.c
And then run the program,
The program should give output as “hello world”. The code creates a new process for “cat test.txt” command, and reads from the pipe stream between cat command and itself.
Writing less than PIPE_BUF bytes is atomic. Writing more than PIPE_BUT bytes may not be atomic: the kernel may interleave the data with data written by other processes. For example, if two processes trying to write “aaaaaa” and “bbbbb” respectively to the same pipe. If the writes are atomic, the content is either “aaaaaabbbbb” or “bbbbbaaaaaa”. But if it’s not atomic, the content can be something like “aaabbaabbba”.
In Linux, PIPE_BUF is set at limits.h, and the program below can print it out.
On my Ubuntu Linux, the value is 4096.
Non Block Pipe
The default pipe blocks both reads and writes. A non blocking pipe can be created by fcntl with O_NONBLOCK flag. The code block below illustrates how to create a non block pipe,
More details about pipes can be referred at reference 4.
1. POSIX wikipedia page: http://en.wikipedia.org/wiki/POSIX
2. Understanding Linux Kernel Inter-process Communication: Pipes, FIFO & IPC: http://linux.omnipotent.net/article.php?article_id=12504&page=2
3. popen Linux man page.
4. Unix Network Programming, Volume 2.
40% Discount on My Book — Android NDK CookbookAndroid NDK Cookbook ebook 40% discount with promotion code MREANC40 at Packt Publishing The promotion code is valid until 15th June.
- Android Apps (18)
- Android Tutorial (27)
- Code Snippet (2)
- Coding Beyond Technique (18)
- Compilers and Related (2)
- ELF (2)
- Computer Languages (31)
- Data Structure & Algorithms (29)
- Database (1)
- SQLite (1)
- Digital Signal Processing (33)
- Distributed Systems (17)
- Linux Study Notes (40)
- Machinery (30)
- misc (1)
- My Ideas (1)
- My Project (3)
- My Publication (1)
- My Readings (1)
- Networking (15)
- Program for Performance (8)
- Uncategorized (1)
- Virtual Machine (2)
- Web Dev (8)
- web components (3)
- May 2013 (2)
- April 2013 (1)
- March 2013 (4)
- December 2012 (2)
- November 2012 (6)
- October 2012 (6)
- September 2012 (3)
- August 2012 (13)
- July 2012 (15)
- June 2012 (3)
- May 2012 (8)
- April 2012 (4)
- March 2012 (13)
- February 2012 (19)
- January 2012 (9)
- December 2011 (11)
- November 2011 (12)
- October 2011 (4)
- September 2011 (12)
- August 2011 (16)
- July 2011 (15)
- June 2011 (6)
- May 2011 (10)
- April 2011 (13)
- March 2011 (20)
- February 2011 (4)
- November 2010 (2)
- May 2010 (1)
- April 2010 (1)
- February 2010 (1)