Linux fork () return value Description __linux

Source: Internet
Author: User

For the main process fork () to return the newly created subprocess ID, the subprocess Fork () returns 0

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

Process configuration has a unique process control block PCB, composed of proc structure and USR structure.
The process-related system calls are described in turn below:
1:fork () function to create a child process

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

Only by looking at the name of Fork, it may be rare for a few people to guess what it is for. The function of the fork system call is to replicate a process. When a process calls it, there are two almost identical processes after it completes, and we get a new process. It is said that fork's name is derived from this work flow which is quite similar to the shape of the fork.
In Linux, there is only one way to create a new process, which is the fork we are introducing. Some other library functions, such as system (), seem to be able to create new processes, and if you look at their source code, they will understand that they actually call fork inside. This includes us running the application at the command line, and the new process is made by the shell call Fork. Fork has some very interesting features, let's take a little program to learn more about it.

/* fork_test.c
/* #include <sys/types.h>
#inlcude <unistd.h>
Main ()
{
pid_t pid;
 
/* At this time there is only one process * *
pid=fork ();
/* There are already two processes 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 ());
else
printf ("I am the parent process, my process ID is%d/n", getpid ());
}
Compile and run:
$GCC fork_test.c-o fork_test
$./fork_test
I am the parent process, my process ID was 1991
I am the child PR Ocess, my process ID is 1992
When you look at this program, you must first understand a concept in your mind: Before the statement pid=fork (), only one process executes the code, but after that statement, it becomes two processes executing, the code portion of the two processes is identical, and the next statement to be executed is the IF (pid==0 )......。

In two processes, the pre-existing one is called the "parent process", and the new one is called a "subprocess." The difference between a parent-child process, except for the process identifier (procedure ID), the value of the variable PID is also different, and the PID stores the return value of the fork. One of the wonders of fork invocation is that it's only called once, can be returned two times, it may have three different return values: In the parent process, fork returns the process ID of the newly created child process, fork returns 0 in the subprocess, and fork returns a negative value if an error occurs;

There are two possible reasons for fork Error: (1) The current number of processes has reached the upper limit of the system, when the errno value is set to Eagain. (2) system memory is not enough, then the value of errno is set to Enomem. (For the purpose of errno, please refer to 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.