#include #include #define READ_END 0 #define WRITE_END 1 int main(int argc, char **argv) { int pid; int pipefd[2]; // Create pipe and fds pipe(pipefd); pid = fork(); if (pid == 0) { /* first child, gets write end of pipe */ dup2(pipefd[WRITE_END], STDOUT_FILENO); close(pipefd[READ_END]); execlp( "/bin/ls", "ls", NULL ); } else if (pid > 0) { /* parent, forks a second child */ pid = fork(); if (pid == 0) { /* second child, gets read end of pipe */ dup2(pipefd[READ_END], STDIN_FILENO); close(pipefd[WRITE_END]); execlp("/usr/bin/less", "less", NULL); } else { /* parent, pipe fds must be closed so that 'less' gets an EOF */ close(pipefd[READ_END]); close(pipefd[WRITE_END]); wait(NULL); wait(NULL); } } }