assume that the following programs are executed under the UNIX operating system environment:
Main () { printf ("Hello world\n"); Fork (); printf ("Hello world\n");}
If the program is running properly, the child process is created successfully, then the output on the screen is
A) 1 Hello World
B) 2 Hello World
C) 3 Hello World
D) 4 Hello World
An existing process can call the fork function to create a new process. A new process created by Fork is called a subprocess (child process). The fork function is called once but returns two times. The only difference of two returns is that the child process ID is returned in the parent process with a value of 0. A child process is a copy of the parent process that obtains a copy of the parent process's data space, heap, stack, and so on. Note that the child process holds a "copy" of the above storage space, which means that these storage spaces are not shared between parent and child processes. UNIX copies the address space content of the parent process to the child process, so that the child process has a separate address space. Under different Unix (like) systems, it is not possible to determine whether the sub-process runs first or the parent process before the fork, depending on the implementation of the system. Therefore, we should not make any assumptions about this when porting the code.after the fork function finishes executing, if the new process is created successfully, there are two processes, one child process and one parent process. In the subprocess, the fork function returns 0, and in the parent process, fork returns the process ID of the newly created child process. We can determine whether the current process is a child process or a parent process by the value returned by the fork.
so there are 3 Hello world.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Embedded Engineer Exam Summary fork () function