Basic Article 3: fork function of process Basics

Source: Internet
Author: User
Tags glob
0. order 1. basic Content 1) Process Composition: 2) after the fork function is called, the system does the following work: 2. function details 1) function prototype 2) function 3) description 3. summary:

0. Order

The fork function is a function used to create a process in Linux. It is also the only function used to create a process. Therefore, I learned the basics of fork functions. 0. Order 1. Basic Content1) Process Overview: Simply put, a process is a process of one execution of a program. It is a dynamic concept. According to the definition in the textbook, a process is an example of program execution and is the basic scheduling unit of Linux. For programmers, the most important thing is to distinguish between processes and programs. A program refers to a piece of code that completes functions, or a tool. It is a static concept, while a process, it is dynamic. For example, in the Linux VI Editor, it is a tool used for text editing in Linux, so it is a program, and in the Linux terminal, you can enable two VI editor processes. Once the process is mentioned, we should generate the idea of dynamically executing the program from the first sentence of the Code to the last sentence.
A process consists of the following elements: 1.> the current context of the process, that is, the current execution status of the process; 2.> current execution directory of the process 3.> files And Directories accessed by the process 4.> process access permissions, such as its file mode and ownership
5.> memory and other system resources allocated to the Process

The fork () function creates a process that is almost identical to the original process through system calls, that is, the two processes can do exactly the same thing, but if the initial parameters or input variables are different, the two processes can also do different things.

2) After the fork function is called, the system will do the following:After a process calls the fork () function, (1) the system allocates resources to new processes, such as the space for storing data and code. (2) then copy all the values of the original process to the new process. There is only a small number (a few of these values are described in man fork in detail), which is different from the original process value. It is equivalent to cloning a self. 2. Function details 1) function prototype
Synopsis
# Include <unistd. h>

Pid_t fork (void );

2) create a sub-process. It is a process, not a thread. 3) Description(1) create a child process by copying and calling the process (that is, the parent process. Replication: A child process shares the code space with its parent process, but its data space is independent of each other. The data space of a child process is copied from the parent process. The values in the created child process are the same as those of the parent process. However, because the data space is independent of each other, therefore, the data in the data space changes with the child process, and thus is no longer the same as the parent process. The fork function is used to create a new process in the process. This new process does not replace the original process, but exists as a sub-process of the current process. This sub-process has a new process identifier PID. And this sub-process will inherit everything from the parent process! What is everything that inherits the parent process?Is to clone all of the parent processes, including Code, The status of the parent process being executed., Parent process working directory, All resources of the parent processAnd so on. Fork system calls will all be copied. Example:
/* It can be used to indicate that the data space between parent and child processes is independent of each other */
# Include <sys/types. h>
# Include <sys/Wait. H>
# Include <stdio. h>
# Include <stdlib. h>
# Include <unistd. h>
# Include <string. h>

Int glob = 6;
Char Buf [] = "A write to stdout/N ";

Int main ()
{
Int var;
Pid_t PID;

Var = 88;

Fprintf (stderr, "% s", Buf );

Printf ("before fork \ n ");

If (pid = fork () <0)
{
Fprintf (stderr, "fork error \ n ");
}
Else if (pid = 0)
{

Glob ++;
VaR ++;
Printf ("child process \ n ");
Printf ("pid = % d getpid = % d, Father pid = % d, glob = % d, Var = % d \ n", PID, getpid (), getppid (), glob, VAR );
Exit (0 );
}
Else
{
VaR + = 20;
Glob + = 20;
Printf ("begin into father Process \ n ");
Sleep (2 );
Printf ("Father Process \ n ");
Printf ("pid = % d, Father pid = % d, glob = % d, Var = % d \ n", getpid (), getppid (), glob, VAR );
}

Return 0;
}

Running result
A write to stdout/nbefore fork
Begin into father Process
Child Process
PID = 0 getpid = 4039, Father pid = 4038, glob = 7, Var = 89
Father Process
PID = 4038, Father pid = 3317, glob = 26, Var = 108
(2) After a new process is created successfully, two processes are basically identical in the system, The execution of these two processes is not in a fixed sequence. Which process is executed first depends on the system's process scheduling policy.. (3) After the creation is complete, we can imagine that the two processes have been running at the same time, and the steps are consistent. After fork, they have done different jobs, that is, they are splitting, this is why fork is called. after fork is executed, two processes are displayed. According to 1), the Parent and Child processes share the code space, so the code is the same. According to 1), the data space of the two is independent of each other. Based on the difference between fork's return values in the parent process and the child process, the Parent and Child processes will execute different code segments, to achieve different functions. 4) it is particularly important that fork copy the current situation of the process. Remember to copy the original data in the code instead of the current situation. This is best reflected by the following code.
# Include <unistd. h>
# Include <stdio. h>
Int main (void)
{
Int I = 0;
Printf ("I son/PA ppid PID fpid \ n ");
// Ppid indicates the PID of the parent process of the current process.
// PID indicates the PID of the current process,
// Fpid indicates the value that fork returns to the current process.
For (I = 0; I <2; I ++ ){
Pid_t fpid = fork ();
If (fpid = 0)
Printf ("% d child % 4D % 4D % 4D \ n", I, getppid (), getpid (), fpid );
Else
Printf ("% d parent % 4D % 4D % 4D \ n", I, getppid (), getpid (), fpid );
}
Return 0;
}
Its running result
 
I son/PA ppid PID fpid
0 parent 3317 4096 4097
1 parent 3317 4096 4098
1 child 1 4098 0
0 child 1 4097 0
1 parent 1 4097 4099
1 child 1 4099 0
The order of running results is different from that in the http://blog.csdn.net/jason314/article/details/5640969. The execution of these two processes is not in a fixed sequence. Which process is executed first depends on the system's process scheduling policy.. 3. conclusion: The fork () function creates a process that is almost identical to the original process through a system call, that is, the two processes can do exactly the same thing (because they share the code space ), however, different values returned by the fork function generate different functions (fork return values are different, and different code segments are executed. Because the data space is independent of each other, data is independent of each other ). Keep two important points in mind: 1) shared program space and data space are independent of each other. 2) There is no fixed sequence of parent and child process execution. Which process must be executed first depends on the system's process scheduling policy. Http://blog.csdn.net/jason314/article/details/5640969 http://blog.csdn.net/dog_in_yellow/article/details/2041079 http://xuzhigang921.blog.163.com/blog/static/56199220201123122639986/ http://coolshell.cn/articles/7965.html a fork interview

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.