Transcript 4061_13

4061 Session 13 (2/27)
Today
• Pipes and FIFOs
Today’s Objectives
• Understand the concept of IPC
• Understand the purpose of anonymous
and named pipes
• Describe where file descriptors point after
system calls to pipe, fork, and dup
• Write C code that allows parent and child
processes to communicate with pipes or
FIFOs
– Specifically: write a “filter”
Admin
• Quiz 2: Nice!
Inter-Process Communication (IPC)
• Techniques for the exchange of data
between processes or threads
Why IPC?
UNIX IPC
• Lots of ways to do it. We’ll talk about
some of the classics in 4061.
• Today:
– Pipes and FIFOs
• Later:
– Signals, Sockets
Pipes
• Aka: anonymous pipes or unnamed pipes
• Very simple IPC
• Remember bash shell: |
– Chain stdout of one process with stdin of
another process
– E.g.:
ps ax | grep java
Pipes: Conceptually
Pipes: Conceptually
• One-way channel for information
– Write to the input end, read from the output
end
• Think of a pipe as a buffer in the operating
system that you can read from and write
to.
– If you fill it up, the writer blocks
– If it’s empty, the reader blocks
Opening a Pipe
• A process issues a command to create a
pipe, and receives two file descriptors.
Forking
• To communicate with something, fork.
Cleaning Up
• Close unused descriptors.
Details
• What do the file descriptors point to?
– Well, files...but a special type (in pipefs).
– So we can still use file-oriented calls, such as
read, write, close, etc.
– No mount point, so not visible with ls
• If you close one end of a pipe, you can’t
reopen it
Details
• Often implemented as a circular buffer
Shortcut Methods
• popen and pclose
FIFOs (a.k.a. Named Pipes)
• (first in first out)
• File (of type FIFO) in the filesystem.
– Access permissions
• Kernel structures are set up on open
• Multiple processes can write, only one can
read
• Writes (smaller than the buffer) are atomic