From e4f855fdbca5e8858043ff03c6e17759b5371dcf Mon Sep 17 00:00:00 2001 From: Brandon Rodriguez <brodriguez8774@gmail.com> Date: Sat, 10 Mar 2018 17:08:42 -0500 Subject: [PATCH] Add comments and last-minute output cleanup --- Main.c | 55 +++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 45 insertions(+), 10 deletions(-) diff --git a/Main.c b/Main.c index ef92d47..1fcf3eb 100644 --- a/Main.c +++ b/Main.c @@ -9,18 +9,42 @@ /** * Description: * - * - * First arg determines overall sleep time for the program. + * Args: + * First arg determines overall sleep time for the program. * Main thread will directly use this arg as the sleep time, as long as it is greater than MIN_SLEEP. * Subthreads' max sleep will be this arg modded by SLEEP_MAX. - * Second arg is number of producer threads to create. - * Third arg is number of consumer threads to create. + * Second arg is number of producer threads to create. + * Third arg is number of consumer threads to create. + * + * Program first creates consumer and producer threads, based on provided args. + * Once all threads are initialized, main sleeps based on args provided. + * When main wakes up, it terminates all threads and closes program. + * + * While main is sleeping, all threads enter loop of: + * * Initially sleep for random period of time between SLEEP_MIN and SLEEP_MAX + * (to ensure as much thread unpredictability as possible). + * * If producer, generate random number between 0 and 10,000. + * * Call (hopefully) thread-safe custom functions to access bounded buffer. + * * If producer, put generated number into lowest available buffer index. + * * If consumer, grab number from highest buffer index that is populated. + * * Return out of thread-safe functions and read out either value that was added or value that was taken + * (depending on if producer or consumer). If error occured, will be printed here. + * + * To make random numbers less random by using a constant seed on every run, comment out line #122 in main. */ /** * Known Issues: + * Ran with: + * Varying min and max sleep times. + * Up to 100 second total run-time. + * Between 1 and 25 different consumers at once. + * Between 1 and 25 different producers at once. + * 1 consumer and 25 produers. + * 25 consumers and 1 producer. * + * Seems to be no issues, as far as I can tell. Reads from and writes to buffer (seemingly) as intended. */ @@ -37,9 +61,11 @@ // Constant Defines. #define SLEEP_MIN 2 -#define SLEEP_MAX 3 +#define SLEEP_MAX 10 + // Variable Declaration. +int wait_on_main = 1; // Method Declaration. @@ -92,8 +118,8 @@ int main(int argc, char* argv[]) { max_time = calloc(1, sizeof(int)); *max_time = nap_length; - // Seeding random generator. - // srand(time(NULL)); TODO: Commented for debugging. Uncomment later. + // Seeding random generator. Uncoment this for consistent seeding every program run. + srand(time(NULL)); intialize_item_buffer(); @@ -116,8 +142,12 @@ int main(int argc, char* argv[]) { } // Sleep main. - printf("Main will now sleep for %d seconds.\n", nap_length); + sleep(1); + printf("\nInitialization complete.\n"); + printf("Main will now sleep for %d seconds.\n\n\n", nap_length); + wait_on_main = 0; sleep(atoi(argv[1])); + printf("\n\n"); // Terminate program. First tell all threads to cancel. @@ -163,6 +193,9 @@ void* producer(void* max_time) { buffer_item item; printf("Producer thread created.\n"); + while(wait_on_main != 0) { + sleep(wait_on_main); + } // Get random max for individual thread sleep. Max possible is SLEEP_MAX. Minimum is SLEEP_MIN. thread_sleep_max = *(int*)max_time % SLEEP_MAX; @@ -177,7 +210,7 @@ void* producer(void* max_time) { // printf("Sleeping producer for %d seconds.\n", sleep_actual); sleep(sleep_actual); - item = rand() % 1000; + item = rand() % 10000; if (insert_item(item) != 0) { fprintf(stderr, "Producer failed to properly insert item %d.\n", item); } else { @@ -198,7 +231,9 @@ void* consumer(void* max_time) { int* item; printf("Consumer thread created.\n"); - + while(wait_on_main != 0) { + sleep(wait_on_main); + } // Get random max for individual thread sleep. Max possible is SLEEP_MAX. Minimum is SLEEP_MIN. thread_sleep_max = *(int*)max_time % SLEEP_MAX; -- GitLab