On Mon, 2010-01-11 at 15:01 +0530, Pete wrote: > 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()? In my opinion - its because vfork() function has the same effect as fork(), except that the behaviour is undefined if the process created by vfork() either modifies any data other than a variable of type pid_t used to store the return value from vfork(), or returns from the function in which vfork() was called, or calls any other function before successfully calling _exit() OR one of the exec family of functions. -- To unsubscribe from this list: send an email with "unsubscribe kernelnewbies" to ecartis@xxxxxxxxxxxx Please read the FAQ at http://kernelnewbies.org/FAQ