2 * A program to sort concurrently N "random" numbers
3 * The reduced space and time should be linear in the
4 * number of processes, and can be reduced when the length of
5 * the buffer queues is increased.
6 * In full search it should be exponential.
9 #define N 7 /* Number of Proc */
10 #define L 10 /* Size of buffer queues */
11 #define RANDOM (seed * 3 + 14) % 100 /* Calculate "random" number */
13 chan q[N] = [L] of {byte};
15 proctype left(chan out) /* leftmost process, generates random numbers */
20 counter = 0; seed = 15;
22 :: out!seed -> /* output value to the right */
23 counter = counter + 1;
25 :: counter == N -> break
26 :: counter != N -> skip
28 seed = RANDOM /* next "random" number */
32 proctype middle(chan in, out; byte procnum)
33 { byte counter, myval, nextval;
38 counter = N - procnum;
39 in?myval; /* get first value from the left */
42 in?nextval; /* upon receipt of a new value */
44 :: nextval >= myval -> out!nextval
47 myval=nextval /* send bigger, hold smaller */
50 :: counter == 0 -> break
54 proctype right(chan in) /* rightmost channel */
59 in?biggest /* accepts only one value, which is the biggest */
69 run middle ( q[proc-1] , q[proc], proc );