Note that there are some explanatory texts on larger screens.

plurals
  1. POvfork never ends
    text
    copied!<p>The following code never ends. Why is that? </p> <pre><code>#include &lt;sys/types.h&gt; #include &lt;stdio.h&gt; #include &lt;unistd.h&gt; #define SIZE 5 int nums[SIZE] = {0, 1, 2, 3, 4}; int main() { int i; pid_t pid; pid = vfork(); if(pid == 0){ /* Child process */ for(i = 0; i &lt; SIZE; i++){ nums[i] *= -i; printf(”CHILD: %d “, nums[i]); /* LINE X */ } } else if (pid &gt; 0){ /* Parent process */ wait(NULL); for(i = 0; i &lt; SIZE; i++) printf(”PARENT: %d “, nums[i]); /* LINE Y */ } return 0; } </code></pre> <p><strong>Update:</strong></p> <p>This code is just to illustrate some of the confusions I have regarding to <code>vfork()</code>. It seems like when I use <code>vfork()</code>, the child process doesn't copy the address space of the parent. Instead, it shares the address space. In that case, I would expect the nums array get updated by both of the processes, my question is in what order? How the OS synchronizes between the two? </p> <p>As for why the code never ends, it is probably because I don't have any <code>_exit()</code> or <code>exec()</code> statement explicitly for exit. Am I right?</p> <p><strong>UPDATE2:</strong><br> I just read: <a href="http://dev.fyicenter.com/Interview-Questions/UNIX/Difference_between_the_fork_and_vfork_system.html" rel="nofollow"><strong>56. Difference between the fork() and vfork() system call?</strong></a> and I think this article helps me with my first confusion.</p> <blockquote> <p>The child process from vfork() system call executes in the parent’s address space (this can overwrite the parent’s data and stack ) which suspends the parent process until the child process exits.</p> </blockquote>
 

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