Linux fork () return value description

Source: Internet
Author: User

 

For the main process fork (), the newly created sub-process ID is returned, and the sub-process fork () returns 0

 

Http://blog.chinaunix.net/u1/53053/showart_425189.html

The Process configuration has a unique process control block PCB, which consists of the proc structure and the USR structure.
The following describes system calls related to processes in sequence:
1: The fork () function creates a sub-process.

# Include <sys/types. h>/* provides the pid_t type definition */# include <unistd. h>/* provides the function definition */pid_t fork (void );

Just look at the fork name. It may be rare for several people to guess what it is. Fork is called to replicate a process. When a process calls it, two processes are almost identical, and we get a new process. The fork name is said to have come from a workflow that is somewhat similar to the fork shape.
In Linux, there is only one way to create a new process, that is, the fork we are introducing. Other library functions, such as system (), seem to be capable of creating new processes. If you can look at their source code, you will understand that they actually call fork internally. This includes running the application under the command line. The new process is also created by calling fork by shell. Fork has some interesting features. Let's use a small program to learn more about it.

/* Fork_test.c */# include <sys/types. h> # inlcude <unistd. h> main () {pid_t PID;/* only one process */PID = fork (); /* two processes are running at the same time */If (PID <0) printf ("error in fork! "); Else if (pid = 0) printf (" I am the child process, my process ID is % d/N ", getpid ()); elseprintf ("I am the parent process, my process ID is % d/N", getpid ());}

Compile and run:

$gcc fork_test.c -o fork_test$./fork_testI am the parent process, my process ID is 1991I am the child process, my process ID is 1992

When you look at this program, you must first understand the concept: Before the statement pid = fork (), only one process is executing this code, but after this statement, the code of the two processes is completely the same. The next statement to be executed is if (pid = 0 ).......

In the two processes, the original one is called the "parent process" and the new one is called the "Child process ". The difference between parent and child processes is not only the process ID, but also the variable PID value. The PID stores the fork return value. One of the wonders of fork calling is that it is called only once, but can return twice. It may have three different return values:

  1. In the parent process, fork returns the process ID of the newly created sub-process;
  2. In the child process, fork returns 0;
  3. If an error occurs, fork returns a negative value;

There are two possible reasons for Fork errors: (1) the current number of processes has reached the limit set by the system, and the errno value is set to eagain. (2) If the system memory is insufficient, the errno value is set to enomem. (For more information about errno, see the first article in this series .)

 

 

 

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.