Skip to content
MarekBykowski edited this page May 15, 2026 · 3 revisions

Linux IPC Cheat Sheet


Pipe

Flow

pipe() -> fork() \u2192 write(fd[1]) / read(fd[0]) \u2192 close()                          

Key Syscalls

Syscall Description
pipe(int fd[2]) Creates kernel buffer; fd[0] = read end, fd[1] = write end
read(fd[0], buf, n) Copies from kernel buffer into buf; blocks if empty; 0 on EOF
write(fd[1], buf, n) Copies buf into kernel buffer; blocks if full
close(fd[n]) Always close the unused end \u2014 read end sees EOF only when all write ends are closed
dup2(src, dst) Makes dst point at whatever src points at; used to redirect stdin/stdout to the pipe

Notes

  • Lives entirely in the kernel \u2014 no filesystem entry
  • After fork() both processes inherit both ends; manually close the end you do not use
  • dup2(fd[1], STDOUT_FILENO) + exec() is how shells implement cmd1 | cmd2
  • Parent forks twice (one per command), closes both ends, then wait()s \u2014 otherwise the reader never sees EOF
int fd[2];                                                                      
pipe(fd);                                                                       
if (fork() == 0) {          /* child: reader */                                 
    close(fd[1]);                                                               
    read(fd[0], buf, sizeof(buf));                                              
    close(fd[0]);                                                               
} else {                    /* parent: writer */                                
    close(fd[0]);                                                               
    write(fd[1], msg, strlen(msg) + 1);                                         
    close(fd[1]);                                                               
}                                                                               

Clone this wiki locally