int MainHelper::operator()() { parse_standard_options(); int res = 1; try { setup_logging(); parse_command_line_arguments(); check_no_unrecognized(); print_version_data(); res = run(); } catch (std::exception& e) { LOG_FATAL("caught exception: " << e.what()); } catch (...) { LOG_FATAL("caught unknown exception"); } at_exit(); google::protobuf::ShutdownProtobufLibrary(); return res; }
/** *The main function * *@param argc number of command line arguments *@param argv array of command line arguments *@return 0 for all ok, 1 for errors **/ int main(int argc, const char* argv[]){ parse_command_line_arguments(argc, argv); Apocalypse *app = new Apocalypse(); ExitStatus es = *app.getExitStatus(); delete app; return es; }
/* ----------------------------------------------------------------------- * main function, where all hell breaks loose * ----------------------------------------------------------------------- */ int main(int argc, char **argv) { parse_command_line_arguments(argc, argv, &cmdArgs); usrdef_init(); symtab_init(); /* begin parsing */ yyparse(); /* If there were parsing errors, exit. */ exit_on_errors(); /* Perform semantic analysis */ semantic_analysis(program); /* If there were errors during semantic analysis, exit. */ exit_on_errors(); /* If we should only perform semantic analysis, exit */ if (cmdArgs.exit_after_sem == 1) { exit(0); } if (cmdArgs.verbose == 1) { /* print the user defined data types */ printf("USER DEFINED DATA TYPES:\n"); printf("------------------------\n"); usrdef_print(); /* print the symbol table*/ printf("\n\n"); printf("SYMBOL TABLE:\n"); printf("-------------\n"); symtab_print(0); } /* Simple, wasn't it ?!! */ return 0; }
// BEGIN MAIN FUNCTION int main(int argc, char **argv) { int i, attempts; int rank, size; MPI_Init(&argc, &argv); // start MPI MPI_Barrier(MPI_COMM_WORLD); // wait for all processes to start MPI_Comm_rank(MPI_COMM_WORLD, &rank); // get current process id MPI_Comm_size(MPI_COMM_WORLD, &size); // get number of processes // set default parameters char task_file[1024]; bool verbose = false; bool wait_on_idle = false; int sleep_time = 300; bool retry = false; int max_retries = 10; // initialize buffer pointers char *buffer_in; char *buffer_out; char *system_command; // file statistics struct struct stat file_stats; // parse all command-line arguments parse_command_line_arguments(argc, argv, rank, task_file, &verbose, &wait_on_idle, &retry, &sleep_time, &max_retries); // initialize file lock structure struct flock fl; fl.l_whence = SEEK_SET; fl.l_start = 0; fl.l_len = 0; fl.l_pid = getpid(); // file descriptor int fd; // loop indefinitely while (true) { // try to open the task file if ((fd = open(task_file, O_RDWR)) == -1) { perror("[ERROR] open"); MPI_Finalize(); exit(1); } // attempt to lock file lock_file(&fl, fd); // get file statistics if (fstat(fd, &file_stats) == -1) { perror("[ERROR] fstat"); MPI_Finalize(); exit(1); } // check that there are tasks to process if (file_stats.st_size > 0) { // allocate buffer memory buffer_in = calloc(1+file_stats.st_size, sizeof(char)); buffer_out = calloc(1+file_stats.st_size, sizeof(char)); // read task file into buffer read(fd, buffer_in, file_stats.st_size); // read first task for (i=0;i<file_stats.st_size;i++) { // found newline if (buffer_in[i] == '\n') break; } // allocate memory for system command system_command = calloc((i+1), sizeof(char)); // copy task into system command buffer strncpy(system_command, buffer_in, i); // copy remaining tasks into output buffer strcpy(buffer_out, buffer_in+i+1); // return to start of file lseek(fd, 0, SEEK_SET); // truncate file ftruncate(fd, 0); // write truncated task list buffer to file write(fd, buffer_out, strlen(buffer_out)); // attempt to unlock file unlock_file(&fl, fd); // close file descriptor close(fd); // free task file buffers free(buffer_in); free(buffer_out); // zero attempts attempts = 0; // report task launch if (verbose) printf("[INFO]: Rank %04d launching: %s\n", rank, system_command); // retry if task fails while (attempts < max_retries && system(system_command) != 0) { attempts++; if (verbose) { if (retry) printf("[WARNING]: system command failed, %s (%d/%d)\n", system_command, attempts, max_retries); else printf("[WARNING]: system command failed, %s\n", system_command); } } // task was successful if (attempts < max_retries) { if (verbose) printf("[INFO]: Rank %04d completed: %s\n", rank, system_command); } // free system command buffer free(system_command); } else { if (wait_on_idle) { // report process wait if (verbose) printf("[INFO]: Rank %04d waiting for more tasks\n", rank); // attempt to unlock file unlock_file(&fl, fd); // close file descriptor close(fd); // sleep for wait period sleep(sleep_time); } else { // report that task file is empty if (verbose) printf("[INFO]: Task file is empty: Rank %04d exiting\n", rank); // attempt to unlock file unlock_file(&fl, fd); // close file descriptor close(fd); // clean up and exit MPI_Finalize(); exit(0); } } } return 0; }