Process 2: Inter-process communication

Overview

In this lecture, we discuss the primary system calls shells use to arrange inter-process communication.

Full lecture notesTextbook readings

Primary process control system calls

fork creates a new process

execvp runs a new program

_exit terminates this process

waitpid monitors a child process for completion

Exit notification as a communication channel

API design

Primary IPC system calls for shells

The pipe system call

pipe illustration

Before pipe
After pipe

selfpipe

Pipe privacy

Pipe throughput

Pipe end-of-file

childpipe and dangling ends

Pipes and the shell

Full pipeline

Problem: How to move file descriptors around?

Initial shell
After pipe system call
Full pipeline

dup2

Pipe dance

Full pipeline

Pipe dance explained

Pipe philosophy

Literate programming

The literate programming challenge (1986)

“Given a text file and an integer k, print the k most common words in the file (and the number of their occurrences) in decreasing frequency.”

Jon Bentley; source

Donald Knuth’s solution

A literate program

Source

Doug McIlroy’s critique

tr -cs A-Za-z '\n' |
tr A-Z a-z |
sort |
uniq -c |
sort -rn |
head -n $1

“The … shell script was written on the spot and worked on the first try. … Knuth has shown us here how to program intelligibly, but not wisely. I buy the discipline. I do not buy the result. He has fashioned a sort of industrial-strength Faberge egg—intricate, wonderfully worked, refined beyond all ordinary desires, a museum piece from the start.”

Source