11 fprintf(stderr, "fork() failed: %s\n", strerror(error));
16 printf("%d: Forked child %d\n", getpid(), child);
21 /* reset child's child count */
26 int run(const char *cmd, char *const argv[])
29 if ((child = do_fork()))
34 printf("Failed to execv command %s: %s\n", cmd, strerror(error));
39 int harvest_zombies(int pid)
46 pid = waitpid(pid, &status, 0);
49 fprintf(stderr, "%d: Error on wait(): %s\n", getpid(),
53 printf("pid %d: exit %d. %d still running\n", pid, status,
60 * Runs a command cmd with params argv, connects stdin and stdout to
63 * Returns the pid of the executed process
65 int run_piped(const char *cmd, char *const argv[], int *readfd, int *writefd)
67 int rfd[2], wfd[2], error, pid;
71 fprintf(stderr, "pipe() failed: %s\n", strerror(error));
77 fprintf(stderr, "pipe() failed: %s\n", strerror(error));
93 dup2(wfd[0], STDIN_FILENO);
94 dup2(rfd[1], STDOUT_FILENO);
96 /* Now we have redirected both stdin and stdout to parent process */
99 printf("Failed to execv command %s: %s\n", cmd, strerror(error));
105 * Runs a command cmd with params argv, connects stdin and stdout to
108 * Returns the pid of the executed process
110 int run_piped_stream(const char *cmd, char *const argv[],
111 FILE **readf, FILE **writef)
113 int rfd, wfd, pid, error;
115 pid = run_piped(cmd, argv, &rfd, &wfd);
118 *readf = fdopen(rfd, "r");
119 if (*readf == NULL) {
122 "Error opening file stream for fd %d: %s\n",
123 rfd, strerror(error));
129 *writef = fdopen(wfd, "w");
130 if (*writef == NULL) {
133 "Error opening file stream for fd %d: %s\n",
134 wfd, strerror(error));