# 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) print_dots pd print_dots_init(&pd, 20) start(&c3.p) start(&pr.p) start(&pd.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 process_counter if meet(out_r, p) p->pc = process_counter_next return 0 process_counter_inc process_counter d->out->data = 1 part(out_r) if meet(out_r, p) p->pc = process_counter_next return 0 process_counter_inc process_counter d->out->data = 2 part(out_r) if meet(out_r, p) p->pc = process_counter_next return 0 process_counter_inc process_counter 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 process_counter while 1 p->pc = process_counter_next pass(in_r, p) return 0 process_counter_inc process_counter 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 process_counter if meet(out_r, p) p->pc = process_counter_next return 0 process_counter_inc process_counter d->out->data = 1 part(out_r) while d->out->data < d->n if meet(out_r, p) p->pc = process_counter_next return 0 process_counter_inc process_counter ++ (d->out->data) part(out_r) return 0 # this one is an example of normal yielding struct print_dots process p int n print_dots_init(print_dots *x, int n) process_init(&x->p, print_dots_f) x->n = n int print_dots_f(process *p) print_dots *d = (print_dots *)p switch p->pc process_counter while d->n > 0 printf(".\n") -- d->n return process_counter_next process_counter_inc process_counter . return 0