Fork function record

Source: Internet
Author: User

Http://www.dzsc.com/data/html/2009-9-10/78615.html

First, let's look at the prototype of the fork function:

# I nclude <sys/types. h>

# I nclude <unistd. h>

Pid_t fork (void );

Return Value:

Negative number: if an error occurs, fork () returns-1, and no new process is created. The initial process is still running.

Zero: in the sub-process, fork () returns 0

Positive: In the parent process, fork () returns the PID of the positive child process.

The Fock function is called twice but returns the ID of the child process to the parent process, and returns 0 to the child process because the parent process may have many child processes, therefore, the returned child process ID must be used to track the child process. The child process has only one parent process, and its ID can be obtained through getppid.

#include<stdio.h>#include <unistd.h>int main(){ pid_t pid; int count=0; pid=fork(); printf("pid=%d\n",pid); count++; printf("count=%d\n",count); if(pid>0)  printf("this is parent");  else if (pid==0)  printf("this is child");  else  printf("error\n");  printf("pid=%d\n",pid);  return 0;}

Running result:

Qq @ Ubuntu :~ /Desktop/fork $./fork
PID = 1, 3505
Count = 1
This is parentpid = 3505
Qq @ Ubuntu :~ /Desktop/fork $ pid = 0
Count = 1
This is childpid = 0

Before the statement pid = fork (), only one process is executing this Code. However, after this statement, the two processes are executing the same code, the next statement to be executed is if (pid> 0 ).......

Both the child process and the parent process execute the code after the fork function call. The child process is a copy of the parent process. For example, the data space and stack space of the parent process will copy the sub-process, rather than share the memory.

In Linux, the next process has three parts of data in the memory: "code segment", "Stack segment", and "data segment ". "Code segment", as its name implies, stores the data of program code. If several processes on the machine run the same program, they can use the same code segment. The "Stack segment" stores the return address of the subroutine, the parameters of the subroutine, and the local variables of the program. The data segment stores the global variables, constants, and dynamic data space allocated by the Program (for example, space obtained using functions such as malloc ). If the system runs several identical programs at the same time, the same stack segment and data segment cannot be used between them.

After careful analysis, we can know:

Once a program calls the fork function, the system prepares the preceding three segments for a new process. First, the system allows the new process and the old process to use the same code segment, because their programs are the same, the system copies a copy of the data segment and stack segment to the new process. In this way, all data of the parent process can be left to the child process. However, once a child process starts running, it inherits all the data of the parent process, but in fact the data has been separated and there is no impact between them, that is, they no longer share any data.

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.