Tags: bell labs, clever tricks, concurrent programming, concurrent programs, condition variables, conventional language, csp, first computers, kernighan, meaningful names, message passing, natural expression, pdos, rare cases, rsc, russ cox, straightforward logic, style point, typical models, word processor,
Concurrent Programming
in the Bell Labs CSP style
Russ Cox
rsc@mit.edu
PDOS Group Meeting
February 5, 2002
http://plan9.bell-labs.com/~rsc/thread.html
1
Overview
World runs in parallel; our typical models of programming do
not.
Lots of solutions: processes, threads, semaphores, spin locks,
condition variables, message-passing, monitors, ...
Present Bell Labs model for concurrent programs.
2
Credo
The most important readers of a program are people.
``We observe simply that a program usually has to be read
several times in the process of getting it debugged. The harder
it is for people to grasp the intent of any given section, the
longer it will be before the program becomes operational.''
Kernighan and Plauger, 1974.
``Write programs for people first, computers second.''
McConnell, 1993.
``Programs are read not only by computers but also by
programmers. A well-written program is easier to understand
and to modify than a poorly-written one. ... Code should be
clear and simple straightforward logic, natural expression,
conventional language use, meaningful names, neat
formatting, helpful comments and it should avoid clever
tricks and unusual constructions.''
Kernighan and Pike, 1999.
In rare cases, performance or other concerns keep code from
being as readable as possible.
3
Why should you care?
From a programming language and style point of view, the
interface presented by libasync is very detail-oriented with no
real benefit.
Like writing a word processor in assembly: too much
detail, language doesn't do enough for the programmer.
Libasync is really doing the jobs of (at least) two libraries:
concurrency support - multiple loci of execution
asynchrony support - overlapping I/O with computation
Let's try again with a helpful concurrency model.
Layer asynchrony on top.
4
Roadmap from here
Introduction
Bell Labs threads
Example: Chord network mapper
Thread library model
Layering asynchrony on top of threads
Summary
5
What this is not
Andrew Birrell, ``An Introduction to Programming with
Threads,'' DEC SRC Technical Report #35, January 6, 1989.
You must unlearn what you have learned.
too much detail
language doesn't do enough for the programmer
6
What this is
A different style of programming.
A different way to think about programming.
Not exclusive to systems.
really slick power series calculator
don't have time to discuss, see web page
7
Hoare, 1978
``Communicating Sequential Processes,'' CACM 21(8),
August 1978.
Lots of ways to program multiprocessors:
communication: shared memory, mainly
synchronization: semaphores, critical regions, monitors,
etc.
Hoare: one primitive, synchronous communication over
unbuffered channels.
8
Example for Flavor
Parallel version of sieve of Eratosthenes:
One process for each prime found so far; filters out multiples
of that prime from a stream carrying the natural numbers.
7 11 13 ... 5 7 11 ... 3 5 7 ... 2 3 4 ...
5 3 2
# generate 2, 3, ...
counter(out: chan of int)
{
for(i=2;; i++)
out