C
Pipe
Learn unidirectional data flow between processes with pipe.
What is a pipe?
A pipe is an anonymous channel providing unidirectional data flow between related processes. The pipe(int fds[2]) call returns two file descriptors: fds[0] for reading and fds[1] for writing.
The typical pattern is: first create the channel with pipe, then produce a child with fork. The parent uses the write end, the child the read end; they close the ends they do not use with close. Data then flows with write and read. Pipes are buffered; write blocks until a reader is attached.
Sharing a pipe with fork
The fork call gives the child a copy of the parent's descriptor table. So both processes hold both pipe ends. To keep the channel unidirectional, the parent closes the read end, the child closes the write end.
The parent writes with write(fds[1], "merhaba", 7), the child reads with read(fds[0], buf, 7). read blocks until data arrives, so output ordering is guaranteed. When done, both sides close the remaining end; the parent reaps the child with wait.
Exercise Send a message through a pipe
Set up the channel with int fds[2]; pipe(fds);. Fork a child with pid_t pid = fork();. In the child: close(fds[1]);, char buf[16]; memset(buf, 0, sizeof(buf));, read(fds[0], buf, 7);, printf("%s\n", buf);, close(fds[0]); _exit(0);. In the parent: close(fds[0]);, write(fds[1], "merhaba", 7);, close(fds[1]);, wait(NULL);.
Show example solution
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
int main(void) {
int fds[2];
pipe(fds);
pid_t pid = fork();
if (pid == 0) {
close(fds[1]);
char buf[16];
memset(buf, 0, sizeof(buf));
read(fds[0], buf, 7);
printf("%s\n", buf);
close(fds[0]);
_exit(0);
}
close(fds[0]);
write(fds[1], "merhaba", 7);
close(fds[1]);
wait(NULL);
return 0;
}
Test yourself
-
After pipe(fds), what is fds[0] used for?
- Error code
- Write end
- Read end
- Process id
Show answer
Read end — fds[0] is the read end, fds[1] is the write end.
-
pipe call
int fds[2]; pipe(____);
Show answer
int fds[2]; pipe(fds);
-
Match pipe parts
- pipe
- Creates channel
- fork
- Splits process
- fds[0]
- Read end
- fds[1]
- Write end
Next lesson: FIFO