-
Notifications
You must be signed in to change notification settings - Fork 770
Pipes, Part 1: Introduction to pipes
What is a pipe?
A POSIX pipe is almost like its real counterpart - you can stuff bytes down one end and they will appear at the other end in the same order. Unlike real pipes however, the flow is always in the same direction, one file descriptor is used for reading and the other for writing. The pipe
system call is used to create a pipe.
int filedes[2];
pipe( filedes );
printf("read from %d, write to %d\n", filedes[0], filedes[1]);
These file descriptors can be used with read
-
// To read...
char buffer[80];
int bytesread = read(filedes[0], buffer, sizeof(buffer));
And write
-
write(filedes[1], "Go!", sizeof("Go!"));
A common method of using pipes is to create the pipe before forking.
int filedes[2];
pipe( filedes );
pid_t child = fork();
if(child > 0) { /* I must be the parent */
char buffer[80];
int bytesread = read(filedes[0] , buffer, sizeof(buffer));
// do something with the bytes read
}
The child can then send a message back to the parent:
if(child ==0) {
write(filedes[1], "done", 4);
}
Todo : fdopen
example
If you need to send data to and from a child, then two pipes are required (one for each direction). Otherwise the child would attempt to read its own data intended for the parent (and vice versa)
Todo
Todo
Legal and Licensing information: Unless otherwise specified, submitted content to the wiki must be original work (including text, java code, and media) and you provide this material under a Creative Commons License. If you are not the copyright holder, please give proper attribution and credit to existing content and ensure that you have license to include the materials.