The Little Book of Semaphores

Second Edition

Allen B. Downey

Download the book in PDF now!

The video

Watch an introduction to semaphores (and Free Books) I presented at Northeastern University:

The book

The Little Book of Semaphores is a free (in both senses of the word) textbook that introduces the principles of synchronization for concurrent programming.

In most computer science curricula, synchronization is a module in an Operating Systems class. OS textbooks present a standard set of problems with a standard set of solutions, but most students don't get a good understanding of the material or the ability to solve similar problems.

The approach of this book is to identify patterns that are useful for a variety of synchronization problems and then show how they can be assembled into solutions. After each problem, the book offers a hint before showing a solution, giving students a better chance of discovering solutions on their own.

The book covers the classical problems, including "Readers-writers," "Producer-consumer", and "Dining Philosophers." In addition, it collects a number of not-so-classical problems, some written by the author and some by other teachers and textbook writers. Readers are invited to create and submit new problems.

Second edition!

The second edition of the book is now available in Postscript and PDF.

In addition, the LaTeX source code is available in a tar file. The book is under the GNU Free Documentation License. Readers are free to copy and distribute the text; they are also free to modify it, which allows them to adapt the book to different needs, and to help develop new material.

Please send comments and corrections to semaphores{at}greenteapress{dot}com.

Example code

threading_cleanup.py
counter.py
counter_mutex.py
coke.py
counter.c
counter_mutex.c
semaphore.c

Other Free Books

From Green Tea Press:

Donate towards my web hosting bill!

Are you using one of our books in a class?

We'd like to know about it. Please consider filling out this short survey.