From cb1f2be037024674f638e23ff967b4e6012cdc40 Mon Sep 17 00:00:00 2001 From: mrb0nk500 Date: Fri, 11 Oct 2019 01:18:09 -0400 Subject: Make C version of test-mvmt faster, by waiting for the child process to exit before reading the childs output. By doing this, it now runs almost twice as fast, although I am not supprised that it runs way faster, since clld is already rediculously fast. --- test-mvmt.c | 48 ++++++++++++++++-------------------------------- 1 file changed, 16 insertions(+), 32 deletions(-) diff --git a/test-mvmt.c b/test-mvmt.c index 952dd9d..27f0aa3 100644 --- a/test-mvmt.c +++ b/test-mvmt.c @@ -53,7 +53,6 @@ int startpipe(char *path, char *input , char *output, ssize_t *size) { fprintf(stderr, "Error occured while duplicating stdout.\n"); close(fd1[1]); } - /*fprintf(stderr,"%s\n", path);*/ if (path == NULL) fprintf(stderr, "oof, yet even more errors, this time with getcwd.\n"); if (execl(path, "clld", (char *)0) < 0) { @@ -61,28 +60,30 @@ int startpipe(char *path, char *input , char *output, ssize_t *size) { return -4; } return 0; - /*printf("%s\n", path);*/ } else { + int status; close(fd0[0]); close(fd1[1]); - /*if (write(fd0[1], input, strlen(input)) != strlen(input)) - fprintf(stderr, "Error occured while trying to write to clld.\n"); - close(fd0[1]); - if ((*size = read(fd1[0], output, 129) < 0)) - fprintf(stderr, "Error occured while trying to read the output of clld.\n"); - close(fd1[0]); - if (size == 0) { - fprintf(stderr, "Child process has closed it's pipe.\n"); - return 0; - }*/ write(fd0[1], input, strlen(input)); - usleep(2000); + while (pid = waitpid(pid, &status, WUNTRACED) > 0) { + if (pid == -1) { + perror("waitpid"); + exit(EXIT_FAILURE); + } + if (WIFEXITED(status)) { + printf("\033[10;1H\033[2Kchild exited, status=%d", WEXITSTATUS(status)); + } else if (WIFSIGNALED(status)) { + printf("\033[10;1H\033[2Kchild killed (signal %d)", WTERMSIG(status)); + } else if (WIFSTOPPED(status)) { + printf("\033[10;1H\033[2Kchild stopped (signal %d)", WSTOPSIG(status)); + } else { + printf("\033[10;1H\033[2KUnexpected status (0x%x)", status); + } + } close(fd0[1]); *size = read(fd1[0], output, 129); close(fd1[0]); return 0; - /*printf("rv: %i\n", rv);*/ - /*printf("%s", output);*/ } return 0; } @@ -92,7 +93,6 @@ void split(char *str, char **splitstr) { int i=0; p = strtok(str,"\n"); while(p!= NULL) { - /*printf("%s\n", p);*/ splitstr[i] = malloc(strlen(p) + 1); if (splitstr[i]) strcpy(splitstr[i], p); @@ -137,26 +137,10 @@ int main (int argc, char **argv) { startpipe(path, input, &ln, &size); char *line[65] = {NULL}; split(ln, line); - /*printf("%s\n%s\n%s\n%s\n", line[0], line[1], line[2], line[3]);*/ x1 = atof(strtok(line[2], ",")); y1 = atof(strtok(NULL, "\0")); xvel = atof(strtok(line[3], ",")); yvel = atof(strtok(NULL, "\0")); - /*for (int i = 0; i