Programming Pipe in Linux
Pipe is one of the message passing IPC techniques. A pipe is a one way communication channel. It is created by the pipe function,
#include <unistd.h>
int pipe(int fd[2]);
The array fd[2] returns two file descriptors referring to two ends of the created pipe: fd[0] for reading and fd[1] 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.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
void client(int readfd, int writefd) {
char msg[100];
int n;
char eof = EOF;
printf("%d client enter something: ", getpid());
fgets(msg, 100, stdin);
if (write(writefd, msg, strlen(msg)-1) == -1) { //-1: not send the newline
perror("error writing...");
exit(0);
}
if((n = read(readfd, msg, 100)) < 0){
perror("error reading...");
exit(0);
}
msg[n] = '\0';
printf("client received from server: %s\n", msg);
}
void server(int readfd, int writefd) {
char msg[100];
int n;
if ((n = read(readfd, msg, 100)) < 0) {
perror("error reading...");
}
msg[n] = '\0';
printf("%d server received from client: %s\n", getpid(), msg);
printf("server enter something: ");
fgets(msg, 100, stdin);
write(writefd, msg, strlen(msg)-1); //-1: not send the newline
}
int main(int argc, char **argv) {
int pipe1fd[2], pipe2fd[2];
int pid;
if (pipe(pipe1fd) == -1) {
perror("pipe:");
return 0;
}
if (pipe(pipe2fd) == -1) {
perror("pipe:");
return 0;
}
pid = fork();
if (pid == 0) {
//child process
close(pipe1fd[1]); //close the write end
close(pipe2fd[0]); //close the read end
client(pipe1fd[0], pipe2fd[1]);
exit(0);
} else {
//parent process
close(pipe1fd[0]);
close(pipe2fd[1]);
server(pipe2fd[0], pipe1fd[1]);
wait(NULL); //wait for child process to finish
return 0;
}
}
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[2]) and pipe2 (pipe2fd[2]).
2. call fork to create a child process
3. child process close write end of pipe1 (pipe1fd[1]) and read end of pipe2 (pipe2fd[0])
4. parent process close read end of pipe1 (pipe1fd[0]) and write end of pipe2 (pipe2fd[1])
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,
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
void client(int readfd, int writefd) {
char msg[100];
int n;
char eof = EOF;
printf("client enter something: ");
fgets(msg, 100, stdin);
if (write(writefd, msg, strlen(msg)-1) == -1) { //-1: not send the newline
perror("error writing...");
exit(0);
}
if((n = read(readfd, msg, 100)) < 0){
perror("error reading...");
exit(0);
}
msg[n] = '\0';
printf("client received from server: %s\n", msg);
}
void server(int readfd, int writefd) {
char msg[100];
int n;
if ((n = read(readfd, msg, 100)) < 0) {
perror("error reading...");
}
msg[n] = '\0';
printf("server received from client: %s\n", msg);
printf("server enter something: ");
fgets(msg, 100, stdin);
write(writefd, msg, strlen(msg)-1); //-1: not send the newline
}
int main(int argc, char **argv) {
int pipe1fd[2], pipe2fd[2];
int pid;
if (pipe(pipe1fd) == -1) {
perror("pipe:");
return 0;
}
if (pipe(pipe2fd) == -1) {
perror("pipe:");
return 0;
}
pid = fork();
if (pid == 0) {
//child process
// close(pipe1fd[1]); //close the write end
// close(pipe2fd[0]); //close the read end
client(pipe1fd[0], pipe2fd[1]);
exit(0);
} else {
//parent process
// close(pipe1fd[0]);
// close(pipe2fd[1]);
server(pipe2fd[0], pipe1fd[1]);
wait(NULL); //wait for child process to finish
return 0;
}
}
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.
#include <stdio.h>
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,
#include <stdlib.h>
#include <unistd.h>
void createTest() {
FILE *f;
f = fopen("test.txt", "w");
fprintf(f, "hello world\n");
fclose(f);
}
int main(int argc, char **argv) {
char buf[100], command[100];
FILE *pf;
createTest();
sprintf(command, "cat test.txt");
pf = popen(command, "r"); //read from cat
while (fgets(buf, 100, pf) != NULL) {
printf("%s", buf);
}
pclose(pf);
return 0;
}
Compile the code using the command below,
gcc -o pipe3 pipe3.c
And then run the program,
./pipe3
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.
PIPE_BUF
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.
#include <stdio.h>
#include <limits.h>
int main(void) {
printf("%d\n", PIPE_BUF);
return 0;
}
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,
#include <fcntl.h>
void nonblock(int fd) {
int flags;
if ((flags = fcntl(fd, F_GETFL, 0)) < 0) {
perror("F_GETFL error:");
return;
}
flags |= O_NONBLOCK;
if (fcntl(fd, F_SETFL, flags) < 0) {
perror("F_SETFL error:");
}
}
More details about pipes can be referred at reference 4.
References:
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.
5. http://manpages.courier-mta.org/htmlman7/pipe.7.html
Leave a Reply Cancel reply
40% Discount on My Book — Android NDK Cookbook
Android NDK Cookbook ebook 40% discount with promotion code MREANC40 at Packt Publishing The promotion code is valid until 15th June.Categories
- Android Apps (18)
- Android Audio Editor (1)
- TS 2 (3)
- Video Converter Android (8)
- Video2Gif (1)
- Android Tutorial (27)
- Android Dev Tools (1)
- API illustrated (8)
- Multimedia API (3)
- ffmpeg on Android (4)
- NDK (6)
- UI (6)
- Animation (2)
- Code Snippet (2)
- Coding Beyond Technique (18)
- a word, a world (4)
- Bug Rectified (4)
- Programming Habit (1)
- Software as a Career (1)
- Software as User Experience (1)
- Compilers and Related (2)
- ELF (2)
- Computer Languages (31)
- C/C++ (13)
- Java (9)
- JavaScript (2)
- PHP (1)
- Python (8)
- Data Structure & Algorithms (29)
- Bits (1)
- Data Structure (5)
- Integers (10)
- BigInteger (1)
- Prime (4)
- Search (3)
- Sorting (5)
- Strings (5)
- Database (1)
- SQLite (1)
- Digital Signal Processing (33)
- Distributed Systems (17)
- Apache Cassandra (6)
- Apache Hadoop (8)
- Apache Avro (3)
- Apache Nutch (3)
- Apache Solr (1)
- Linux Study Notes (40)
- crontab (1)
- Linux Kernel Programming (8)
- Linux Programming (12)
- IPC (2)
- Linux Network Programming (5)
- Linux Signals (2)
- Linux Shell Scripting (1)
- ssh (3)
- Machinery (30)
- misc (1)
- My Ideas (1)
- My Project (3)
- Mobile Caching (1)
- Selective Decoding (2)
- My Publication (1)
- My Readings (1)
- Networking (15)
- Program for Performance (8)
- Uncategorized (1)
- Virtual Machine (2)
- Web Dev (8)
- web components (3)
- Android Apps (18)
Recent Comments
Archives
- 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)




