1 // Concurrent version of prime sieve of Eratosthenes.
2 // Invented by Doug McIlroy, inventor of Unix pipes.
3 // See http://plan9.bell-labs.com/~rsc/thread.html.
4 // The picture halfway down the page and the text surrounding it
5 // explain what's going on here.
7 // Since NENVS is 1024, we can print 1022 primes before running out.
8 // The remaining two environments are the integer generator at the bottom
9 // of main and user/idle.
19 // fetch a prime from our left neighbor
21 p
= ipc_recv(&envid
, 0, 0);
24 // fork a right neighbor to continue the chain
25 if ((id
= fork()) < 0)
26 panic("fork: %e", id
);
30 // filter out multiples of our prime
32 i
= ipc_recv(&envid
, 0, 0);
34 ipc_send(id
, i
, 0, 0);
43 // fork the first prime process in the chain
44 if ((id
= fork()) < 0)
45 panic("fork: %e", id
);
49 // feed all the integers through
51 ipc_send(id
, i
, 0, 0);