C language programming in Linux system creation function fork () execution parse _c language

Source: Internet
Author: User
Tags function prototype

Recently looking at the communication between the processes, see the fork () function, although used before, this time after thinking deepened understanding. The summary is as follows:

1. The function itself

(1) header file

#include <unistd.h>
#include <sys/types.h>

(2) Function prototype

pid_t fork (void);
(pid_t is a macro definition whose essence is int is defined in #include<sys/types.h>)
Return value: Returns two values if a successful call is made, a child process returns 0, and the parent process returns the Subprocess ID, otherwise an error returns-1

(3) Function description

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). A child process is a copy of the parent process, which obtains copies of resources such as the parent process 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, and the child process has a separate address space .

2. Code Execution explanation

(1) The code is shown in the following figure

(2) Analysis

From the knowledge of operating system, process is the basic unit of system resource allocation, so the process resource space is not shared by the child process and the parent process. Until line 8th of the code snippet is executed, only the default main process is in the system. After the 8th line of code snippet is executed, there are two processes in the system, that is, the main process and the child processes created by it.

Creates a subprocess, the fork () function returns two numeric values, and if the creation succeeds, the child processes return 0; The parent process returns the Subprocess ID. Use the resource space diagram as follows:

After the fork () function is executed, the main process generates a copy of the resource space for the parent process. The PID in the main process is the PID of the child process (pid>0), and the PID in the subprocess is 0.

The parent and child processes are executed from the next line after the fork () function, that is, line 9th. Because of the pid>0 in the main process, the else if (pid>0) segment code can be executed, and the subprocess Pid=0 can execute the else if (pid==0) segment code.

(3) Code execution results are as follows:

Visible, "Before the fork ..." was only performed once. "After the fork ..." executed two times.

(Specific execution results may vary by process scheduling, and the following four output orders may be different.) But the first output must be "before the fork ...".

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.