TEST_F(SimplePipelineTest, test_simple_pswf_exact) { // Config file std::string configfile(config_path + "fastimg_exact_pswf_config.json"); load_configdata(configfile); // Execute pipeline based on the loaded configurations stp::SourceFindImage sfimage = execute_pipeline(); int total_islands = sfimage.islands.size(); ASSERT_EQ(total_islands, 1); int label_idx = sfimage.islands[0].label_idx; int sign = sfimage.islands[0].sign; double extremum_val = sfimage.islands[0].extremum_val; int extremum_x_idx = sfimage.islands[0].extremum_x_idx; int extremum_y_idx = sfimage.islands[0].extremum_y_idx; EXPECT_EQ(label_idx, 1); EXPECT_EQ(sign, 1); EXPECT_NEAR(extremum_val, 0.49762858232302537, pipeline_tolerance); EXPECT_EQ(extremum_x_idx, 824); EXPECT_EQ(extremum_y_idx, 872); // Do not compare x_centre and y_centre (moments-fit) because there are differences // on the number of samples of the island. The difference seems to be caused by // numeric errors on sigma clip function (the function is not numerically stable). }
TEST_F(SimplePipelineTest, test_simple_gaussiansinc_oversampling) { // Config file std::string configfile(config_path + "fastimg_oversampling_gaussiansinc_config.json"); load_configdata(configfile); // Execute pipeline based on the loaded configurations stp::SourceFindImage sfimage = execute_pipeline(); int total_islands = sfimage.islands.size(); ASSERT_EQ(total_islands, 1); int label_idx = sfimage.islands[0].label_idx; int sign = sfimage.islands[0].sign; double extremum_val = sfimage.islands[0].extremum_val; int extremum_x_idx = sfimage.islands[0].extremum_x_idx; int extremum_y_idx = sfimage.islands[0].extremum_y_idx; double xbar = sfimage.islands[0].moments_fit.x_centre; double ybar = sfimage.islands[0].moments_fit.y_centre; EXPECT_EQ(label_idx, 1); EXPECT_EQ(sign, 1); EXPECT_NEAR(extremum_val, 0.10973023095014588, pipeline_tolerance); EXPECT_EQ(extremum_x_idx, 824); EXPECT_EQ(extremum_y_idx, 872); EXPECT_NEAR(xbar, 823.363524400699, pipeline_tolerance); EXPECT_NEAR(ybar, 871.582414171632, pipeline_tolerance); }
TEST_F(SimplePipelineTest, test_simple_gaussiansinc_exact) { // Config file std::string configfile(config_path + "fastimg_exact_gaussiansinc_config.json"); load_configdata(configfile); // Execute pipeline based on the loaded configurations stp::SourceFindImage sfimage = execute_pipeline(); int total_islands = sfimage.islands.size(); ASSERT_EQ(total_islands, 1); int label_idx = sfimage.islands[0].label_idx; int sign = sfimage.islands[0].sign; double extremum_val = sfimage.islands[0].extremum_val; int extremum_x_idx = sfimage.islands[0].extremum_x_idx; int extremum_y_idx = sfimage.islands[0].extremum_y_idx; double xbar = sfimage.islands[0].moments_fit.x_centre; double ybar = sfimage.islands[0].moments_fit.y_centre; EXPECT_EQ(label_idx, 1); EXPECT_EQ(sign, 1); EXPECT_NEAR(extremum_val, 0.11045229607808271, pipeline_tolerance); EXPECT_EQ(extremum_x_idx, 824); EXPECT_EQ(extremum_y_idx, 872); EXPECT_NEAR(xbar, 823.435310857686, pipeline_tolerance); EXPECT_NEAR(ybar, 871.615973834618, pipeline_tolerance); }
int main() {error = fopen("error_dump.rpt", "w"); snap = fopen("snapshot.rpt", "w"); /* code */ Read_DMEMORY(); Read_IMEMORY(); initialize(); setInstructions(); execute_pipeline(); fclose(error); fclose(snap); return 0; }
/* Execute a pipeline of commands */ void execute_pipeline(pipeline_t * pipeline, int pipeA[2], int index) { int pid, pipeB[2], ret; if (index < 0) return; if (index > 0) { ret = pipe(pipeB); fatal(ret < 0, "Could not create pipe."); } pid = fork(); fatal(pid < 0, "Could not create process"); /* Parent: exec */ if (pid > 0) { /* Not last, redirect input */ if (index > 0) { close(0); dup(pipeB[0]); close(pipeB[0]); } /* Not first, redirect output */ if (index < pipeline->ncommands - 1) { close(1); dup(pipeA[1]); close(pipeA[1]); } /* Not used. */ if (index > 0) close(pipeB[1]); /* Run command */ execvp(pipeline->command[index][0], pipeline->command[index]); /* Child: recurse */ } else { execute_pipeline(pipeline, pipeB, index - 1); return; } }
/* * execute_one_subcommand(): * Do file redirections if applicable, then call execute_pipeline() * which recursively executes the entire pipeline. * Does not return, so you want to fork() before calling me. */ void execute_one_subcommand(struct parsed_line *p) { extern void execute_pipeline(struct pipeline *pl); if (p->inputfile) { close(0); if (open(p->inputfile, O_RDONLY, 0) < 0) { perror(p->inputfile); exit(126); } } if (p->outputfile) { close(1); if (open(p->outputfile, O_WRONLY|O_CREAT|O_TRUNC, 0666) < 0) { perror(p->outputfile); exit(126); } } if (p->pl) execute_pipeline(p->pl); /* otherwise, we have a null command, which should just exit successfully */ exit(0); }
/* Main function */ int main(int argc, char **argv) { buffer_t *command_line; pipeline_t *pipeline; int pid, status, tmp, fd, ret; command_line = new_command_line(); pipeline = new_pipeline(); current_directory = (char*) malloc(1024*sizeof(char)); exeJobs = (s_exeJobs*)malloc(sizeof(s_exeJobs)); exeJobs->jobs = NULL; exeJobs->count = 0; /* Welcome message */ system("clear"); printf("Welcome to BlueShell!\n\n"); while (go_on) { current_directory = getcwd(current_directory, 1024); printf("%s%s%s ", SHELL_NAME, current_directory, PROMPT); fflush(stdout); ret = read_command_line(command_line); fatal(ret < 0, NULL); /* Parse command line. */ if (parse_command_line(command_line, pipeline) == 0) { if (pipeline->ncommands < 1) continue; /* Try to execute built in command */ if (exec_builtin(pipeline->command[0][0], pipeline->command[0])) continue; /* Running group of process in background */ if(RUN_BACKGROUND(pipeline)) { addJobsBack(pipeline); } /* Running group of process in foreground */ if (RUN_FOREGROUND(pipeline)) { /* More than one command */ if (pipeline->ncommands > 1) { /* Create another process and check for fatal error */ pid = fork(); fatal(pid < 0, "Could not create a new process."); /* If parent */ if (pid > 0) { /* Just wait for child to end */ wait(&status); /* If child */ } else { execute_pipeline(pipeline, NULL, pipeline->ncommands - 1); return EXIT_SUCCESS; } /* Only one command was read */ } else { /* Create another process and check for fatal error */ pid = fork(); fatal(pid < 0, "Could not create a new process."); /* If parent */ if (pid > 0) { /* Just wait for child to end */ addJobsFore(pipeline, pid); wait(&status); /* If child */ } else { /* Redirecting input */ if (REDIRECT_STDIN(pipeline)) { tmp = open(pipeline->file_in, O_RDONLY); fatal(tmp < 0, "Could not redirect input: file not found"); /* Close file descriptor 0 which is 'stdin' */ close(0); /* Make a copy of file descriptor opened for input, thus now it is associated with file descriptor 0 that has been just released */ fd = dup(tmp); fatal(fd < 0, "Could not redirect input."); close(tmp); } /* Redirecting output */ if (REDIRECT_STDOUT(pipeline)) { /* '>' Overwrite if file exists, or create a new one */ if (REDIRECT_STDOUT_WRITE(pipeline)) { tmp = open(pipeline->file_out, O_CREAT | O_TRUNC | O_RDWR, S_IRUSR | S_IWUSR); fatal(tmp < 0, "Could not redirect output: failed to create a file"); /* '>>' Append to existing file */ } else if(REDIRECT_STDOUT_APPEND(pipeline)) { tmp = open (pipeline->file_out, O_CREAT | O_APPEND | O_RDWR, S_IRUSR | S_IWUSR); fatal(tmp < 0, "Could not redirect output: failed to append to file"); } /* Close file descriptor 1 which is 'stdout' */ close(1); /* Make a copy of file descriptor opened for output, thus now it is associated with file descriptor 1 that has been just released */ fd = dup(tmp); fatal(fd < 0, "Could not redirect input."); close(tmp); } /* Execute a program */ execvp(pipeline->command[0][0], pipeline->command[0]); /* If program not found */ printf ("%s: command not found\n", pipeline->command[0][0]); return EXIT_FAILURE; } } /* Running group of process in background */ } else { if (REDIRECT_STDIN(pipeline)) { /* TODO */ } if (REDIRECT_STDOUT(pipeline)) { /* TODO */ } } } } release_command_line(command_line); release_pipeline(pipeline); return EXIT_SUCCESS; }
/* * execute_pipeline(): recursively execute a (struct pipeline *). * pl must be non-null. * Does not return. */ void execute_pipeline(struct pipeline *pl) { char *filepath; struct stat statbuf; static char *path[] = { "/bin", "/usr/bin", "." }; int i; extern char **environ; /* * first, if this is an actual pipeline (i.e. of length > 1), * make a pipe and handle all but one entry recursively. * The recursion is kinda wacky because the parse of a|b|c is * like (a|b)|c, but we recurse in the other direction. So b|c * is the recursive invocation but the top-level process has to be the * one which ends up execing c. Hence the exec is in the child process. */ if (pl->next) { int pipefd[2]; if (pipe(pipefd)) { perror("pipe"); exit(127); } switch (fork()) { case -1: perror("fork"); exit(127); case 0: /* child */ close(pipefd[0]); dup2(pipefd[1], 1); close(pipefd[1]); /* ... and proceed with command execution below. */ break; default: /* parent */ close(pipefd[1]); dup2(pipefd[0], 0); close(pipefd[0]); execute_pipeline(pl->next); return; /* won't be reached because e_pl() does not return */ } } /* * at this point we are either the child process which is the * non-base-case for the recursion and we did the "break" from case 0 * above, OR we are doing a pipeline of length one, which IS the base * case for the recursion. */ if (strchr(pl->argv[0], '/')) { /* if command contains a '/', don't apply the search path. */ filepath = pl->argv[0]; } else { /* find first executable item by that name in the path. */ for (i = 0; i < sizeof path / sizeof path[0]; i++) { filepath = efilenamecons(path[i], pl->argv[0]); if (stat(filepath, &statbuf) == 0) break; } if (i == sizeof path / sizeof path[0]) { fprintf(stderr, "%s: Command not found\n", pl->argv[0]); exit(127); } } (void)execve(filepath, pl->argv, environ); perror(filepath); exit(127); }