Note that there are some explanatory texts on larger screens.

plurals
  1. POParent process killing child process in infinite loop
    primarykey
    data
    text
    <p>To solve my problem, I set <code>prctl(PR_SET_PDEATHSIG, SIGHUP);</code> as in <a href="https://stackoverflow.com/questions/284325/how-to-make-child-process-die-after-parent-exits">stackoverflow answer</a> before i called <code>exec*</code>, and took out the part where we pipe the PID. It works!!!!! Wow....</p> <p>HOWEVER, stackoverflow won't let me say I've answered my own question yet...</p> <p>So I tried to write a program, which I want to run a program, and kill that program after a cpl seconds if it doesn't finish. DADDY forks off a CHILD, which forks off another BABY, CHILD pipes the PID of the BABY to DADDY, which then waits a second and kills them both if they haven't wrapped up their business (it's a macabre scene). But it doesn't work, DADDY stays in S+ State, and the infinite loop that is Baby goes on forever until I ctr+c. On the bright side, this code is an amalgamation of everything I've learnt on stack-overflow. Here we go.</p> <pre><code>#include &lt;math.h&gt; #include &lt;signal.h&gt; #include &lt;stdint.h&gt; #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; #include &lt;time.h&gt; #include &lt;unistd.h&gt; static int read_from_pipe(int file) { int c; FILE *stream = fdopen(file, "r"); if (fscanf(stream, "%d", &amp;c) != 1) { fprintf(stderr, "Failed to read integer from pipe\n"); exit(1); } fclose(stream); return c; } static void write_to_pipe(int file, int pidRacket) { FILE *stream = fdopen(file, "w"); fprintf(stream, "%d", pidRacket); fclose(stream); } static int spawnpipe(char *fileName, int *fd) { int pid; int pipe_fds[2]; char *command[] = {"racket", fileName, NULL}; if (pipe(pipe_fds) &lt; 0) { fprintf(stderr, "FE: pipe\n"); exit(1); } switch ((pid = fork())) { case -1: printf("syserr"); exit(1); case 0: close(1); close(2); dup(pipe_fds[1]); close(pipe_fds[0]); close(pipe_fds[1]); execvp(*command, command); perror("execv"); exit(EXIT_FAILURE); default: *fd = pipe_fds[0]; close(pipe_fds[1]); return pid; } } static int spawnfp(char *fileName, FILE **fpp) { int fd, pid; pid = spawnpipe(fileName, &amp;fd); *fpp = fdopen(fd, "r"); return pid; } int main(int argc, char *argv[]) { pid_t pid; int mypipe[2]; if (pipe(mypipe)) { fprintf(stderr, "Pipe failed.\n"); return EXIT_FAILURE; } pid = fork(); if (pid &lt; (pid_t) 0) { fprintf(stderr, "Fork failed.\n"); return EXIT_FAILURE; } else if (pid != (pid_t) 0) { double diff = 0; clock_t launch = clock(); close(mypipe[1]); int pidRacket = read_from_pipe(mypipe[0]); while (diff &lt; 1.3) { clock_t done = clock(); diff = ((double)done - (double)launch) / (double)CLOCKS_PER_SEC; } kill(pidRacket, SIGKILL); kill(pid, SIGKILL); return EXIT_SUCCESS; } else if (pid == (pid_t) 0) { close(mypipe[0]); char buf[100]; FILE *fp; char *fileName = argv[1]; int pidRacket = spawnfp(fileName, &amp;fp); write_to_pipe(mypipe[1], pidRacket); if (argc == 1) { printf("Not enough arguments!"); _exit(EXIT_FAILURE); } else if (argc == 2) { } sleep(1); while (fgets(buf, sizeof buf, fp)) { printf("%s\n", buf); } fclose(fp); kill(pid, SIGKILL); return 0; } } </code></pre> <p>Credit to quinsley and vijay!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload