Linux Pipes and FIFOs David Ferry Chris Gill

  • Slides: 8
Download presentation
Linux Pipes and FIFOs David Ferry, Chris Gill CSE 522 S - Advanced Operating

Linux Pipes and FIFOs David Ferry, Chris Gill CSE 522 S - Advanced Operating Systems Washington University in St. Louis, MO 63130 1

Pipes are a common way to move data between programs, e. g. : cat

Pipes are a common way to move data between programs, e. g. : cat filename | grep “search_string” • Linux provides pipe() system call that allows inter-process communication • Command above relies on shell “pipelines” which do not necessarily use pipe() CSE 522 S – Advanced Operating Systems 2

Linux’s Pipes The Linux pipe() system call: Process A • Allows anonymous (nonnamed) communication

Linux’s Pipes The Linux pipe() system call: Process A • Allows anonymous (nonnamed) communication Write FD • Is unidirectional Pipe Imp. • Produces / consumes Read FD data through file syscalls write() and read() • Data stored in kernel Process B via pipefs filesystem See /fs/pipe. c for implementation CSE 522 S – Advanced Operating Systems 3

Pipe() Semantics Pipes are only usable between related processes: 1) pipe() creates a read

Pipe() Semantics Pipes are only usable between related processes: 1) pipe() creates a read Write FD Process A and write descriptor Read FD 2) Process fork() 3) Reader deletes write FD, writer deletes read FD Write FD 4) Reader reads, Process B Read FD writer writes CSE 522 S – Advanced Operating Systems 4

FIFOs (Named Pipes) Variant to pipes: • Handle to FIFO exists as a regular

FIFOs (Named Pipes) Variant to pipes: • Handle to FIFO exists as a regular file • Read and written like regular file • Data is stored in kernel (not disk) • Allows non-related processes to communicate • Supports multiple readers & writers • Must be open at both ends before reading or writing CSE 522 S – Advanced Operating Systems 5

Pipe and FIFO Limits Atomicity: • I/O is atomic for data quantities less than

Pipe and FIFO Limits Atomicity: • I/O is atomic for data quantities less than PIPE_BUF • Typical: PIPE_BUF = 4096 bytes Write capacity: • Typically 64 K • Writers block or fail if pipe is full Polling vs. Blocking: • Readers may block or fail based on flags set during pipe creation CSE 522 S – Advanced Operating Systems 6

FIFOs vs. Files Even though FIFOs have a handle in the regular file system,

FIFOs vs. Files Even though FIFOs have a handle in the regular file system, they are not files! Files backed by a real filesystem FIFOs not backed Files have no atomicity guarantee FIFOs must be opened for reading and writing before either may occur • Files have no practical capacity limit • • CSE 522 S – Advanced Operating Systems 7

Pipe Paradigms Pipes are useful for implementing many design patterns and idioms: Producer /

Pipe Paradigms Pipes are useful for implementing many design patterns and idioms: Producer / Consumer Process A Pipe Process B FIFO Process D Process A Client / Server Process B Process C Active Object Pipe Process A Pipe CSE 522 S – Advanced Operating Systems Process B 8