Note that there are some explanatory texts on larger screens.

plurals
  1. POparent doesn't create child?
    primarykey
    data
    text
    <p>I try to create 4 child processes who will work simultaneously, but the output of my program is quite random: sometimes one of the processes is not created (the printf statement is not executed). I can not understand the reason for this, because when I use the wait() function in my parent it should wait for it's children to finish? Here is my code:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;unistd.h&gt; #include &lt;sys/types.h&gt; #define NR_OF_PROCESSES 4 void readProcess(void); void tokenProcess(void); void calculatorProcess(void); void errorProcess(void); void (*functionTable[NR_OF_PROCESSES]) (void) = {readProcess, tokenProcess, calculatorProcess, errorProcess}; int main(void) { int i; int status; for (i = 0; i &lt; NR_OF_PROCESSES; i++) { int pid = fork(); if (pid &lt; 0) { perror("could not fork"); return 0; } if (pid == 0) { functionTable[i](); exit(0); } } printf("parent is waiting"); wait(status); return (0); } void readProcess(void) { printf("readprocess running, PID=%d, PPID=%d\n",getpid(),getppid()); } void tokenProcess(void) { printf("tokenprocess running, PID=%d, PPID=%d\n",getpid(),getppid()); } void calculatorProcess(void) { printf("calculatorprocess running, PID=%d, PPID=%d\n",getpid(),getppid()); } void errorProcess(void) { printf("errorprocess running, PID=%d, PPID=%d\n",getpid(),getppid()); } </code></pre> <p>Also, I want to add interprocess communication with pipes later on. Will this be possible if I implement the processes this way? Any better solution?</p>
    singulars
    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