Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You want to catch the signal SIGCHLD and then call a function. You want to use <code>signal()</code> to do this (<code>man signal</code>). In the below example <code>child_died()</code> is called after the signal SIGCHLD is caught (child process is dead at this point). If there is info that you need to pass from the child to the parent that <code>child_died()</code> needs for a conditional, you'll need to use a pipe (<code>man pipe</code>). Here is an example of signal and pipe:</p> <pre><code>#include &lt;signal.h&gt; #include &lt;strings.h&gt; #include &lt;stdio.h&gt; #define MAX_SIZE 256 void child_died(int); int fd[2]; int main() { int pid; extern int fd[]; char *str; pipe(fd); signal(SIGCHLD, child_died); if ((pid = fork()) &lt; 0) { // error printf("Error\n"); } else if (pid == 0) { // child printf("In child process\n"); close(fd[0]); str = strdup("Message from child process.\n"); write(fd[1], str, strlen(str) + 1); } else { // parent wait(); printf("Now in parent\n"); } return 0; } void child_died(int s) { char *str; close(fd[1]); read(fd[0], str, MAX_SIZE); printf("%s", str); } </code></pre> <p>Saved as sigchld.c and compiled with: <code>gcc -Wall sigchld.c -o sigchld</code></p> <p>If you want to pass an int (like the child pid) or some other data type from the child process to the parent process you'll either will have to convert it to a string and back again or find another way to do it.</p> <p>If you want to ignore SIGCHLD then in my example you would put that logic into <code>child_died()</code>. Just use a simple <code>if</code>. I don't know how you are deciding when to ignore the signal and when to print what you want, that's why I included the pipe stuff in my code above. I figured you would use some info from the child process and since <code>child_died()</code> is called on the parent process, you will need a way to transfer data from the child to parent process.</p> <p>I hope this helps. There may be better or easier ways of accomplishing this that I don't know of. If there are then please comment.</p>
 

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