Sample Header Ad - 728x90

After fork(), where does the child begin its execution?

26 votes
5 answers
32719 views
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: ~~~lang-text 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 thought 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?
Asked by thomas1234
Nov 28, 2010, 02:36 AM
Last activity: Nov 24, 2023, 06:23 AM