Embedded Engineer Exam Summary fork () function

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.