> On Mon, Jan 11, 2010 at 6:22 AM, Mulyadi Santosa
> <[email protected]> wrote:
> > On 1/11/10, Joel Fernandes <[email protected]> wrote:
> >> Oh I'm sorry, if you were talking about copying of the address space
> >> information that can be avoided, that does not happen because it
> >> would've already been copied before exec() in the child gets a chance
> >> to execute.. the fork system call calls do_fork somewhere which calls
> >> copy_process which does this copying so it can't be avoided in any
> >> case. The book says copy-on-write itself has more overhead that is
> >> avoided with exec() in the child, but I'm trying to figure how.
> >>
> >> -Joel
> >
> >
> > Hi Joel...
> >
> > Manish is right. Please notice that he talked about "why do we do copy
> > on write (COW) if soon after child is forked, it quickly does exec()".
> > So yes, COW has overhead, but imagine if parent ran first. COW will be
> > triggered for parent address space, then child soon runs too. Then it
> > issues exec(). Clearly, this waste certain amout of memory which can
> > be fairly avoided if child runs first.
After going through this thread, I just tried out the following simple
code:
int main (void) {
int pid;
int *testVar = (int *) malloc (sizeof (int));
*testVar = 10;
printf ("%d [%d] Main \n", *testVar, testVar);
pid=vfork(); // works fine if we use fork instead.
if (pid==0) {
printf ("Child %d [%d]\n", *testVar, testVar);
return 1;
} else if (pid > 0) {
printf ("Parent %d [%d]\n", *testVar, testVar);
*testVar=11; // segfault if we use vfork, as vfork blocks until child
returns call exec/exits.
wait(NULL);
printf ("Parent %d [%d]\n", *testVar, testVar);
return 1;
}
exit(0);
}
can someone let me know why this segfaults with vfork and not with fork?
>From my understanding - it is because the parent is blocked until the
child exec's/exits AND in the mean time when the program is being
executed the child/parent process is trying to change the *testVar is
causing to modify the parents read-only memory. CMIAW.
Another thing I noticed is on linux the child always gets to run first
in case of fork() and vfork()?
Cheers
~Pete
--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to [email protected]
Please read the FAQ at http://kernelnewbies.org/FAQ