/* * CS460: Operating Systems * Jim Plank * threadpc2.c -- threaded producer/consumer driver program #2 */ #include #include #include #include #include #include #include "dlist.h" extern struct inputstruct *read_input(); extern struct outputstruct *process_input(); produce(buffer) Dlist buffer; { struct inputstruct *input; struct outputstruct *output; while(1) { input = read_input(); if (input == NULL) { output = NULL; } else { output = process_input(); } dl_insert_b(buffer, output); lwp_yield(SELF); if (output == NULL) return; } } consume(buffer) Dlist buffer; { struct outputstruct *output; Dlist tmp; while(1) { while(dl_empty(buffer)) lwp_yield(SELF); tmp = buffer->flink; output = (struct outputstruct *) tmp->val; dl_delete_node(tmp); if (output == NULL) return; print_output(output); } } main() { Dlist buffer; stkalign_t *pstack, *cstack; thread_t pt, ct; lwp_setstkcache(MINSTACKSZ*sizeof(stkalign_t), 2); buffer = make_dl(); pstack = lwp_newstk(); cstack = lwp_newstk(); lwp_create(&pt, produce, 1, 0, pstack, 1, buffer); lwp_create(&ct, consume, 1, 0, cstack, 1, buffer); lwp_join(pt); lwp_join(ct); exit(1); }