Skip to content

Commit

Permalink
more improvements on diagnostic output and making sure all messages a…
Browse files Browse the repository at this point in the history
…re consumed
  • Loading branch information
George K. Thiruvathukal committed Nov 9, 2020
1 parent a7f0259 commit 6844fcf
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
14 changes: 13 additions & 1 deletion bounded-buffer/bboptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ int bb_options_get(bb_options_t* options, int argc, char **argv)
{"cdelay", required_argument, 0, 'y' },
{"gen", required_argument, 0, 'g' },
{"bsize", required_argument, 0, 'b' },
{"help", no_argument, 0, 'h' },
{0, 0, 0, 0 }
};

c = getopt_long(argc, argv, "abc:d:012",
c = getopt_long(argc, argv, "",
long_options, &option_index);
if (c == -1)
break;
Expand Down Expand Up @@ -61,6 +62,17 @@ int bb_options_get(bb_options_t* options, int argc, char **argv)
options->gen_count = atoi(optarg);
break;

case 'h':
fprintf(stderr, "usage: %s [--suppliers SUPPLIERS] [--consumers CONSUMERS] [--sdelay SDELAY] [--cdelay CDELAY] [--gen GEN] [--bsize BSIZE]\n", argv[0]);
fprintf(stderr, "\tSUPPLIERS is number of suppliers (default: %d)\n", NUM_SUPPLIERS);
fprintf(stderr, "\tCONSUMERS is number of suppliers (default: %d)\n", NUM_CONSUMERS);
fprintf(stderr, "\tSDELAY is max random delay in millisconds (default: %d)\n", SUPPLIER_DELAY);
fprintf(stderr, "\tCDELAY is max random delay in millisconds (default: %d)\n", CONSUMER_DELAY);
fprintf(stderr, "\tBSIZE is bounded buffer size (default: %d)\n", BB_SIZE);
fprintf(stderr, "\tGEN number of messages to generate per supplier (default: %d)\n", GEN_COUNT);
exit(0);
break;

case '?':
break;

Expand Down
2 changes: 1 addition & 1 deletion bounded-buffer/bbuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void bounded_buffer_cleanup(bounded_buffer_t* bb) {

void bounded_buffer_print_info(bounded_buffer_t* bb) {
INFO("buffer { size: %d, length: %d, head: %d, tail: %d, ", bb->size, bb->tail - bb->head, bb->head, bb->tail);
INFO("[");
INFO("entries : [");
int add_comma = 0;
for (int i=bb->head; i < bb->tail; i++) {
if (add_comma)
Expand Down
6 changes: 3 additions & 3 deletions bounded-buffer/bounded-buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ void* consumer(void *tsd)
bb_options_t* options = bb_tsd->options;
int my_id = bb_tsd->id;
int max_to_consume = options->no_suppliers * options->gen_count / options->no_consumers;
INFO("consumer { id: %d, state: \"running\", messages: %d }\n", my_id, max_to_consume);
int not_consumed = options->no_suppliers * options->gen_count % options->no_consumers;
if (not_consumed > 0)
INFO("consumer { id: %d, extra_messages: %d }\n", my_id, not_consumed);
if (my_id < not_consumed)
max_to_consume++;
INFO("consumer { id: %d, state: \"running\", messages: %d }\n", my_id, max_to_consume);
for (int i=0; i < max_to_consume; i++) {
entry_t* entry = bounded_buffer_get(bb);
millisecond_sleep( rand() % options->consumer_max_delay_ms);
Expand Down

0 comments on commit 6844fcf

Please sign in to comment.