Linux fork ()

Source: Internet
Author: User

The C language under Linux can use fork () to establish a child process.
The fork function returns two values, and returns 0 for the child process; The parent process, which returns the child process ID. So with
if (fork () ==0)
{The code snippet executed by the child process;}
Else
{The code snippet executed by the parent process;}

Two. The fork () function: Creates a new child process.
As you can see, fork means branching, so it's equivalent to a tributary from where the current process is running, and then the new process and the old process will continue to run from the fork point (the fork point is the fork () function call).

As for the return value of the fork () function:
Child process return: 0
Parent process returns: >0 integer (returns the child process ID number)
Error return: 1

Therefore, if there is no error, for:
if (fork () ==0)
{Block A}
else//fork () returns nonzero, where the parent process executes
{Block B}
The contents of block a will be executed in the new process, and Block B will be executed in the old process.

Three. Fork is one of the most difficult concepts to understand for people who have just contacted the Unix/linux operating system to write multiple processes under Linux: it executes once but returns two values.

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 there is an error, fork () returns-1, no new process is created at this time. The initial process is still running.

There are two possible reasons for fork errors: (1) The current number of processes has reached the system-specified limit, when the value of errno is set to Eagain. (2) system memory is low,

At this point the value of errno is set to Enomem.

0: In child process, fork () returns 0

Positive number: In the negative process, fork () returns the PID of the positive child process

Next we look at how to use fork to create a child process.

The boilerplate code for creating child processes is as follows:

pid_t child;

if ((Child = Fork ()) <0)

/* ERROR Handling */

else if (child = = 0)

/* This is a new process */

Else

/* This is the original parent process */

The Fock function call returns two times, returns the ID of the child process to the parent process, and returns 0 to the child process.

This is because the parent process may have many child processes, so the child process must be traced through the returned child process ID.

While the child process has only one parent process, his ID can be obtained through getppid.

Looking at this program, the mind must first understand a concept: before the statement pid=fork (), only one process executes the code, but after this statement, it becomes two processes executed, the two processes of the code part is exactly the same, the next statement will be executed is pid=fork ( ) after that one statement. (if (pid>0) .... )

Of the two processes, the one that originally existed is called the "parent process", and the newly emerging one is called a "subprocess". The difference between a parent-child process and a process ID differs from the value of the variable PID, and the PID holds the return value of the fork. A wonderful thing about a fork call is that it is called only once, but it can return two times, and it may have three different return values:

Linux 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.