int main (int argc, char *argv[]) { int write_character = -1; g_prog_mode = PRG_SENDER_MODE; /*check parameters */ if ((buffer_size = get_buffer_size(argc, argv)) == RETURN_ERROR) { return EXIT_FAILURE; } /*create semaphore and ringbuffer*/ if (create_environment() == RETURN_ERROR) { return EXIT_FAILURE; } /*write ringbuffer*/ do { /*read stdin input*/ write_character = fgetc(stdin); /*decrement semaphore*/ if (my_sem_wait() == RETURN_ERROR) { return EXIT_FAILURE; } /*write ringbuffer*/ write_char_to_buffer(write_character); /*increment semaphore*/ if (my_sem_post() == RETURN_ERROR) { return EXIT_FAILURE; } } while (write_character != EOF); /*check for errors in input of stdin*/ if (ferror(stdin)) { fprintf(stderr, "%s: %s%s\n", g_program_name, "Error while reading input from \"stdin\".", strerror(errno)); destroy_environment(PRG_ERROR); return EXIT_FAILURE; } /*destroy semaphore and ringbuffer*/ if (destroy_environment(PRG_SUCCESS) == RETURN_ERROR) { return EXIT_FAILURE; } else { return EXIT_SUCCESS; } }
/* Run a simulation, using the specified environment file. */ static void run_one_simulation (struct dirent *env_file) { if (! open_log_file (env_file->d_name)) return; LOG ((LOG_SCREEN | LOG_FILE), "*** Starting simulation %s ***", env_file->d_name); /* Parse the environment file. */ if (! initialize_from_env_file (env_file)) return; /* Create all agents. */ if (! spawn_all_agents ()) return; for (env.current_time = 0; env.current_time < env.simulation_length; env.current_time++) { LOG (LOG_FILE, " "); LOG ((LOG_SCREEN | LOG_FILE), "Starting Turn %u", env.current_time); LOG (LOG_FILE, " "); log_map (); LOG (LOG_FILE, " "); run_one_turn (); if (env.alive_agents == NULL) { LOG ((LOG_SCREEN | LOG_FILE), " ** All agents have died -- aborting simulation"); break; } } /* Clean up. */ summarize_results (); destroy_all_agents (); destroy_agent_list (&env.alive_agents); destroy_all_predators (); destroy_environment (); LOG ((LOG_SCREEN | LOG_FILE), "*** End of simulation %s ***\n", env_file->d_name); close_log_file (); }