void sink(struct args *p) { Chan_T c = p->c; int i = 0, j, prime, primes[256]; primes[0] = 0; while (Chan_receive(c, &prime, sizeof prime)) { for (j = 0; primes[j]; j++) if (prime%primes[j] == 0) break; if (primes[j] == 0) { printf(" %d", prime); primes[i++] = prime; primes[i] = 0; if (--p->nprime <= 0) break; if (i == p->n) { fflush(stdout); p->c = Chan_new(); Thread_new((int (*)(void *))sink, p, sizeof *p, NULL); filter(primes, c, p->c); return; } } } printf("\n"); Chan_receive(c, &prime, 0); }
int sink(void* cl) { struct args* p = cl; Chan_T input = p->c; int i = 0, j, x, primes[256]; primes[0] = 0; for (;;) { Chan_receive(input, &x, sizeof x); for (j = 0; primes[j] != 0 && x % primes[j] != 0; j++) ; if (primes[j] == 0) { if (x > p->last) { break; } Fmt_print(" %d", x); primes[i++] = x; primes[i] = 0; if (i == p->n) { p->c = Chan_new(); Thread_new(sink, p, sizeof * p, NULL); filter(primes, input, p->c); return EXIT_SUCCESS; } } } Fmt_print("\n"); Chan_receive(input, &x, 0); return EXIT_SUCCESS; }
void filter(int primes[], Chan_T listen, Chan_T output) { int i, j, n = sizeof i; while (n) { Chan_receive(listen, &i, sizeof i); for (j = 0; primes[j]; j++) if (i%primes[j] == 0) break; if (primes[j] == 0) n = Chan_send(output, &i, sizeof i); } Chan_receive(listen, &i, 0); }
void filter(int primes[], Chan_T input, Chan_T output) { int j, x; for (;;) { Chan_receive(input, &x, sizeof x); for (j = 0; primes[j] != 0 && x % primes[j] != 0; j++) ; if (primes[j] == 0) if (Chan_send(output, &x, sizeof x) == 0) { break; } } Chan_receive(input, &x, 0); }