I'm trying to learn UNIX programming and came across a question regarding fork(). I understand that fork() creates an identical process of the currently running process, but where does it start? For example, if I have code
int main (int argc, char **argv)
{
int retval;
printf ("This is most definitely the parent process\n");
fflush (stdout);
retval = fork ();
printf ("Which process printed this?\n");
return (EXIT_SUCCESS);
}
The output is:
This is most definitely the parent process
Which process printed this?
Which process printed this?
I thought that fork()
creates a same process, so I initially that that in that program, the fork()
call would be recursively called forever. I guess that new process created from fork()
starts after the fork()
call?
If I add the following code, to differentiate between a parent and child process,
if (child_pid = fork ()) printf ("This is the parent, child pid is %d\n", child_pid);
else printf ("This is the child, pid is %d\n",getpid ());
after the fork() call, where does the child process begin its execution?
man fork
is sure enough to answer your question, btw