# separate components into different files use stdio.h use sched.h use rendezvous.h int main() sched_init() channel_int ch rendezvous_init(&ch.r) count_to_three c3 count_to_three_init(&c3, &ch) printer pr printer_init(&pr, &ch) start(&c3.p) start(&pr.p) run() count_to_n cn count_to_n_init(&cn, &ch, 15) start(&cn.p) run() return 0 struct channel_int rendezvous r int data # we could do with Plan 9 cc's struct inheritance # my namespaces feature will be better, though! struct count_to_three process p channel_int *out count_to_three_init(count_to_three *x, channel_int *out) process_init(&x->p, count_to_three_f) x->out = out int count_to_three_f(process *p) count_to_three *d = (count_to_three *)p rendezvous *out_r = &d->out->r switch p->pc 1 if meet(out_r, p) p->pc = 2 return 0 2 d->out->data = 1 part(out_r) if meet(out_r, p) p->pc = 3 return 0 3 d->out->data = 2 part(out_r) if meet(out_r, p) p->pc = 4 return 0 4 d->out->data = 3 part(out_r) return 0 struct printer process p channel_int *in printer_init(printer *x, channel_int *in) process_init(&x->p, printer_f) x->in = in int printer_f(process *p) printer *d = (printer *)p rendezvous *in_r = &d->in->r switch p->pc 1 while 1 p->pc = 2 pass(in_r, p) return 0 2 printf("%d\n", d->in->data) return 0 struct count_to_n process p channel_int *out int n count_to_n_init(count_to_n *x, channel_int *out, int n) process_init(&x->p, count_to_n_f) x->out = out x->n = n int count_to_n_f(process *p) count_to_n *d = (count_to_n *)p rendezvous *out_r = &d->out->r switch p->pc 1 if meet(out_r, p) p->pc = 2 return 0 2 d->out->data = 1 part(out_r) while d->out->data < d->n if meet(out_r, p) p->pc = 3 return 0 3 ++ (d->out->data) part(out_r) return 0