(Latest Revision: Tue Apr 4 21:25:24 PDT 2017 ) sequencing

### Sequencing

```

Sequencing Exercise

process  |      P1        |          P2
program  |  open window   |  jump through window

Unfortunately this 'script' can go horribly wrong if P2 acts before P1 is finished.
How can we make sure that the window is open when P2 jumps through it?

One solution: use a semaphore S initialized with value=0, and modify the
programs like this:

process  |      P1        |          P2
program  |  open window   |       wait(S)
|    signal(S)   |  jump through window

Exercise: verify that everything will go OK, whether P1 tends to be faster
or slower than P2.

Now suppose that we have three processes: P0, P1, P3, tasked with the following
jobs:

process  |       P0       |      P1        |          P2
program  |  unlock window |  open window   |  jump through window

Here, it's necessary that P0 act first, then P1, and P2 last.  How can we
make sure that they act in the correct sequence?

One way would be to use two semaphores T, and S, both initialized with value=0,
and modify the programs like this:

process  |       P0       |      P1        |          P2
program  |  unlock window |    wait(T)     |       wait(S)
|    signal(T)   |  open window   |  jump through window
|                |   signal(S)    |

Exercise: Verify that this will work, no matter what the relative 'speeds' are
of P0, P1, and P2.

Exercise: Try some more example problems - suppose one process has to unlock a
double door, two more processes have to open each half of the door, and a fourth
process has to run through the open door (it only fits if both halves are open).

```