#include "sporadic_task.hh" #include "time_io.hh" #include "file_io.hh" #include "options.hh" #include "program.hh" #include #include #include #include #include #include #include void Sporadic_Task::run(ms_t duration) { if (!quiet) { std::cout << "task " << id << " executes: " << program << std::endl; } // for extern processes, this is implicit if (!fork_processes) { run_program(program, program_args); } } void Sporadic_Task::launch_extern_program() { assert(fork_processes); char* const program_name = const_cast(program.c_str()); int arg_array_size = argc + 1; char* program_argv[arg_array_size]; program_argv[0] = program_name; for (int i = 0; i < argc; i++) { program_argv[i + 1] = const_cast(program_args[i]->c_str()); } program_argv[arg_array_size] = NULL; if (execv(program_name, program_argv) == -1) { std::cerr << "Error executing program: " << program_name << std::endl; exit(127); } } void Sporadic_Task::check_task(Sporadic_Task* t) { assert( 0 < t->id); assert(ms_t(0) < t->c); assert( t->c <= t->d); assert( t->d <= t->t); } Sporadic_Task::Sporadic_Task() { std::cout << "Create task. State ID, C, D, and T: "; std::cin >> id >> c >> d >> t; argc = -1; while (argc == -1) { std::cout << "State program of t" << id << ": "; std::cin >> program; argc = number_of_args(program); } if (argc > 0 ) { size_t strings_size = argc*sizeof(std::string *); program_args = (std::string **)malloc(strings_size); assert(program_args); std::cout << "Input the " << argc << " arguments of " << program << ": "; for (int i = 0; i < argc; i++) { std::string *s = new std::string(); std::cin >> *s; program_args[i] = s; } } else { program_args = NULL; } check_task(this); } Sporadic_Task::Sporadic_Task(std::ifstream& f) { file_goto_next_char(f, 't'); f >> id; file_goto_next_char(f, '('); f >> c; file_goto_next_char(f, ','); f >> d; file_goto_next_char(f, ','); f >> t; file_goto_next_char(f, ' '); std::getline(f, program, '('); int arg_list_start_pos = f.tellg(); argc = 0; char current; if (fork_processes) { bool done = false; while (!done) { current = f.get(); switch (current) { case ')': done = true; break; case ' ': continue; default: argc++; bool argument_done = false; while (!argument_done) { current = f.get(); switch (current) { case ',': argument_done = true; break; case ')': argument_done = done = true; break; } } } } } else { argc = number_of_args(program); } f.seekg(arg_list_start_pos); if (argc > 0 ) { size_t strings_size = argc*sizeof(std::string *); program_args = (std::string **)malloc(strings_size); assert(program_args); char delim = ','; for (int i = 0; i < argc; i++) { if (i != 0) { f.get(); } std::string *s = new std::string(); if (i == argc - 1) { delim = ')'; } std::getline(f, *s, delim); program_args[i] = s; } } else { program_args = NULL; } check_task(this); if (debug) { std::cout << "The program is " << program << "." << std::endl; for (int i = 0; i < argc; i++) { std::cout << "Argument " << i << ": " << *(program_args[i]) << std::endl; } } } void Sporadic_Task::store() { std::cout << this << std::endl; } std::ostream& operator<<(std::ostream& os, const Sporadic_Task* t) { os << "t" << t->id << " = (" << t->c << ", " << t->d << ", " << t->t << ") " << t->program; int argc = t->argc; os << "("; for (int i = 0; i < argc; i++) { os << *(t->program_args)[i]; if (i != argc - 1) { os << ", "; } } os << ")"; return os; }