Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If what you want is creating multiple child processes doing their "own business" right after their creation, you should use <code>vfork()</code> (used to create new processes without fully copying the address space of the father process) and <code>exec()</code> family to replace the children processes' images with whatever you want.</p> <p>if you don't want the father to wait until the child is finished, you should take advantage of asynchronous signal handling. A SIGCHLD is sent when a child process ends. So you can put the <code>wait()</code> within the signal handler for SIGCHLD rather than the father process and let the signal handler collect returning status for child process. </p> <p>Below is a toy example:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;unistd.h&gt; #include &lt;sys/types.h&gt; #include &lt;signal.h&gt; #include &lt;stdlib.h&gt; sig_atomic_t child_process_ret_status; void spawn(char *program,char *argv[]){ pid_t child_pid=vfork(); if(child_pid&gt;0){ printf("the parent process pid: %d\n",(int)getpid()); printf("the cpid: %d\n",(int)child_pid); system("ping -c 10 www.google.com"); }else{ printf("the new process %d is going to execute the new program %s\n",(int)getpid(),program); execvp(program,argv); printf("you'll never see this if everything goes well.\n"); } } void child_process_ret_handle(int sigval){ if(sigval == SIGCHLD){ printf("SIGCHLD received !\n"); wait(&amp;child_process_ret_status); } } int main(void){ signal(SIGCHLD,child_process_ret_handle); char *program="sleep"; char *argv[]={ "sleep", "5", NULL }; spawn(program,argv); if(WIFEXITED (child_process_ret_status)){ printf("child process exited successfully with %d\n",WEXITSTATUS (child_process_ret_status)); }else{ printf("the child process exited abnormally\n"); } printf("parent process: %d returned!\n",getpid()); return 0; } </code></pre>
    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.
    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