Process 3: Pipe sizes and interrupts

Overview

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

Full lecture notesTextbook readings

pipe illustration

Before pipe
After pipe

Pipe privacy

Pipe throughput

childpipe and dangling ends

Pipe end-of-file

Multi-stage pipelines

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

Process hierarchy: manyfork and zombie processes

Pipes and relay races

Relay race attempt 1

$ /bin/echo Handoff | grep H

Successful relay handoff

https://www.thestar.com/sports/olympics/2016/08/18/why-the-us-womens-4x100-relay-team-ran-by-themselves-on-the-track.html

Relay race attempt 2

$ /bin/echo Handoff > handoff.txt & grep H handoff.txt

Unsuccessful relay handoff

https://www.dailymail.co.uk/sport/olympics/article-2185586/London-Olympics-2012-Great-Britain-looking-overcome-baton-woes-relays.html

Unsuccessful relay handoff

https://www.stabroeknews.com/2016/08/19/sports/u-s-grasp-second-chance-4x100-relay/

Explanation

Race condition

Reasoning about race conditions

Relay race barriers and parallel actions

Racer

racer-poll

Racer arguments

Polling and blocking

Signals

Signal system calls

void handle_signal(int signal_number) {
    // do something to handle the signal
}

...
struct sigaction sa;
sa.sa_handler = handle_signal; // or SIG_IGN or SIG_DFL
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sigaction(SIGINT, &sa, nullptr);

When can a signal be delivered?

racer-block

racer-blockvar

Race conditions!

racer-selfpipe