X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=course%2Fsemester2%2Fpprog%2Fassignment1%2Fsingle-thread%2Fmain.c;fp=course%2Fsemester2%2Fpprog%2Fassignment1%2Fsingle-thread%2Fmain.c;h=e55c75f9abaebaeff18fc48a96c0c2087e9310a3;hb=20979b1c07eda73b7f86b2fe8cb66eb58d959e04;hp=e9ccb4111b1426479e57a151eb47a9c5a67978f9;hpb=47b0dba32b8e0e0deedfbfc6db49b65b930e2889;p=matches%2Fhonours.git diff --git a/course/semester2/pprog/assignment1/single-thread/main.c b/course/semester2/pprog/assignment1/single-thread/main.c index e9ccb411..e55c75f9 100644 --- a/course/semester2/pprog/assignment1/single-thread/main.c +++ b/course/semester2/pprog/assignment1/single-thread/main.c @@ -21,11 +21,12 @@ Options options; // global variable declared in "nbody.h" - Options passed to pr // --- Function forward declarations --- // void HandleArguments(int argc, char ** argv); //Interprets program arguments and sets up the "options" variable -unsigned UnsignedArgument(unsigned * i, int argc, char ** argv, unsigned * store, unsigned * store2); //Helper function to get switch value for unsigned +unsigned IntegerArgument(unsigned * i, int argc, char ** argv, int * store, int * store2); //Helper function to get integer switch void FloatArgument(unsigned * i, int argc, char ** argv, float * store); //Helper function to get switch value for float void DisplayStatistics(); //Called on exit of program, displays information about computation time, steps computed, etc void Interrupt(int dummy); // Interrupt handler function, called when SIGINT (Ctrl-C) is sent to program + // --- Function implementations --- // /** @@ -43,8 +44,8 @@ int main(int argc, char** argv) options.output = NULL; options.num_threads = 0; options.nested_threads = 0; - options.num_steps = 0; - options.timeout = 0; + options.num_steps = -1; // Negative values => simulation runs forever unless otherwise specified + options.timeout = -1; options.draw_graphics = true; options.pedantic_graphics = false; options.print_positions = false; @@ -70,6 +71,8 @@ int main(int argc, char** argv) exit(EXIT_FAILURE); } + + signal(SIGINT, Interrupt); //Handle SIGINT signals atexit(Universe_Cleanup); //On exit, cleanup universe (and write positions of bodies to file if supplied). atexit(DisplayStatistics); //On exit, print information about the computations done @@ -85,8 +88,10 @@ int main(int argc, char** argv) options.start_clock = clock(); //Get CPU cycles executed before simulation starts Simulation_Run(argc, argv); // Start the simulation - //printf("Got here!\n"); - exit(EXIT_FAILURE); //Should never get to this line + //printf("Main thread done!\n"); + + //pthread_exit(NULL); + exit(EXIT_SUCCESS); //Should never get to this line } @@ -119,34 +124,50 @@ void HandleArguments(int argc, char ** argv) switch (argv[i][1]) //The argument is a switch if we get here { case 'n': //Number of threads switch - UnsignedArgument(&i, argc, argv, &(options.num_threads), &(options.nested_threads)); + IntegerArgument(&i, argc, argv, &(options.num_threads), &(options.nested_threads)); #ifdef SINGLE_THREADED - fprintf(stderr, "Warning: -%c switch has no effect in the single-threaded program.\n", argv[i][1]); + fprintf(stderr, "Warning: -%c switch has no effect in the single-threaded program.\n", 'n'); + #else + if (options.num_threads <= 0) + { + fprintf(stderr, "Require at least one thread (-%c %s is invalid).\n",'n', argv[i]); + exit(EXIT_FAILURE); + } #endif //SINGLE_THREADED break; case 's': //Number of steps switch - UnsignedArgument(&i, argc, argv, &(options.num_steps), NULL); + IntegerArgument(&i, argc, argv, &(options.num_steps), NULL); + if (options.num_steps < 0) + { + fprintf(stderr, "Require zero or more steps to run (-%c %s is invalid).\n", 's', argv[i]); + exit(EXIT_FAILURE); + } break; case 't': //Timeout switch (in seconds) - UnsignedArgument(&i, argc, argv, &(options.timeout), NULL); + IntegerArgument(&i, argc, argv, &(options.timeout), NULL); + if (options.timeout < 0) + { + fprintf(stderr, "Require a timeout greater or equal to zero (-%c %s is invalid).", 't', argv[i]); + exit(EXIT_FAILURE); + } break; case 'g': //Graphics switch options.draw_graphics = !options.draw_graphics; break; case 'v': //Verbosity switch - UnsignedArgument(&i, argc, argv, &(options.verbosity), NULL); + IntegerArgument(&i, argc, argv, &(options.verbosity), NULL); break; case '-': - if (strcmp(argv[i]+1, "pedantic-graphics")) + if (strcmp(argv[i]+2, "pedantic-graphics") == 0) { options.pedantic_graphics = true; #ifdef SINGLE_THREADED fprintf(stderr, "Warning: %s switch has no effect in the single threaded program.\n", argv[i]); #endif //SINGLE_THREADED } - else if (strcmp(argv[i]+1, "fast-graphics")) + else if (strcmp(argv[i]+2, "fast-graphics") == 0) { options.pedantic_graphics = false; #ifdef SINGLE_THREADED @@ -156,7 +177,7 @@ void HandleArguments(int argc, char ** argv) } else { - fprintf(stderr, "Unrecognised switch -%s\n", argv[i]); + fprintf(stderr, "Unrecognised switch %s\n", argv[i]); exit(EXIT_FAILURE); } break; @@ -172,16 +193,16 @@ void HandleArguments(int argc, char ** argv) } /** - * @function UnsignedArgument - * @purpose Helper function to get an unsigned integer following a argument switch + * @function IntegerArgument + * @purpose Helper function to get up to two integers following a argument switch, seperated by ':' * @param i - position in the argument array, will be updated after this function * @param argc - number of arguments * @param argv - argument strings * @param store - pointer to unsigned to store result in - * @param store2 - pointer to second unsigned + * @param store2 - pointer to second integer (set to NULL if there is only one integer) * @returns 1 if store was filled, 2 if both store1 and store2 were filled */ -unsigned UnsignedArgument(unsigned * i, int argc, char ** argv, unsigned * store, unsigned * store2) +unsigned IntegerArgument(unsigned * i, int argc, char ** argv, int * store, int * store2) { if (*i >= argc-1) { @@ -209,12 +230,12 @@ unsigned UnsignedArgument(unsigned * i, int argc, char ** argv, unsigned * store } int val = atoi(argv[*i+1]); - if (val <= 0) + if (val <= 0 && strcmp("0", argv[*i+1]) != 0) { fprintf(stderr,"Supply a positive integer for the -%c switch. %s is invalid.\n", argv[*i][1], argv[*i+1]); exit(EXIT_FAILURE); } - *store = (unsigned)val; + *store = val; *i += 1; return (seperator == NULL) ? 1 : 2; @@ -256,3 +277,4 @@ void Interrupt(int dummy) QuitProgram(false); } +