5 int run(const char *cmd, char *const argv[])
11 fprintf(stderr, "fork() failed: %s\n", strerror(error));
16 printf("%d: Forked child %d\n", getpid(), child);
21 /* reset child's child count */
25 printf("Failed to execv command %s: %s\n", cmd, strerror(error));
30 int harvest_zombies(int pid)
37 pid = waitpid(pid, &status, 0);
40 fprintf(stderr, "%d: Error on wait(): %s\n", getpid(),
44 printf("pid %d: exit %d. %d still running\n", pid, status,
51 * Runs a command cmd with params argv, connects stdin and stdout to
54 * Returns the pid of the executed process
56 int run_piped(const char *cmd, char *const argv[], int *readfd, int *writefd)
58 int rfd[2], wfd[2], error, pid;
62 fprintf(stderr, "pipe() failed: %s\n", strerror(error));
68 fprintf(stderr, "pipe() failed: %s\n", strerror(error));
75 fprintf(stderr, "fork() failed: %s\n", strerror(error));
90 dup2(wfd[0], STDIN_FILENO);
91 dup2(rfd[1], STDOUT_FILENO);
93 /* Now we have redirected both stdin and stdout to parent process */
96 printf("Failed to execv command %s: %s\n", cmd, strerror(error));
102 * Runs a command cmd with params argv, connects stdin and stdout to
105 * Returns the pid of the executed process
107 int run_piped_stream(const char *cmd, char *const argv[],
108 FILE **readf, FILE **writef)
110 int rfd, wfd, pid, error;
112 pid = run_piped(cmd, argv, &rfd, &wfd);
115 *readf = fdopen(rfd, "r");
116 if (*readf == NULL) {
119 "Error opening file stream for fd %d: %s\n",
120 rfd, strerror(error));
126 *writef = fdopen(wfd, "w");
127 if (*writef == NULL) {
130 "Error opening file stream for fd %d: %s\n",
131 wfd, strerror(error));