fork a child process and the parent process

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_36561697/article/details/90733843

First, father and son after the fork process

The new process created by fork is called the child process (child process). This function is called once but returns twice. The difference between the two is the return of the child process the return value is 0 , and returns the value of the parent process is the process of new process (child process) of the id. The reason for the child returned to the parent process id is: a child process because the process can be more than one, not a function of the process so that a process can obtain all of its child processes id . A child process, the reason fork returns 0 to it, because it can be called at any time getpid () to get their pid; can also call getppid () to get the id of the parent process . (Process id 0 is always used by the exchange process, so the process is a child process id, not 0).

After the fork, the operating system will be an exact copy of the parent process child process, although the father-son relationship, but it seems in the operating system, they are more like brother relationship, the two processes share code space, but the data are independent of space , the contents of the child data space is a complete copy of the parent process, is also the same as the instruction pointer, the child has a parent process is currently running to the position of (the value of the program counter pc two processes are the same, that is to say, from the fork a child process return at the beginning of execution), but a little different, if successful fork, the child process fork return value is 0, the parent process fork return value is the process ID of the child process, if the fork is unsuccessful, the parent process returns an error .
In this way you can imagine, two processes have been running at the same time, and in unison, after the fork, they were as different jobs, which is the bifurcation. This is also the reason why it's called a fork fork

As for a first run, it may be related to the operating system (scheduling algorithm), but also to the issue is not important in practical applications, if needed parent and child collaboration, can be resolved by way primitive.

include <stdio.h>  

#include <unistd.h>  
#include <stdlib.h>  
#include <errno.h>  
  
int main(void)  
{  
        pid_t pid=fork();  
        if(pid==0)  
        {  
                int j ;  
                for(j=0;j<10;j++)  
                {  
                        printf("child: %d\n",j);  
                        sleep(1);  
                }  
        }  
        else if (pid>0)  
        {  
                int i;  
                for(i=0;i<10;i++)  
                {  
                        printf("parent: %d\n",i);  
                        sleep(1);  
                }  
        }  
        else  
        {  
                fprintf(stderr,"can't fork ,error %d\n",errno);  
                exit(1);  
        }  
        printf("This is the end !");  
}  

 

Second, inheritance fork out the child and the parent process  

fork out process, basically everything except the process ID of the parent process has a copy, basically it means that not all of the following, we would say that the child process inherited from the parent process something there, something no inherit. Another point to note, the child gets only a copy of the parent process, rather than the parent resource itself.

Inherited by the child from the parent process to:     
Eligibility 1. Process (real (real) / active (effective) / saved (saved) User number (UIDs) and group number (GIDs))
2. Environment (Environment)
3. Stack
4. memory
5. open file descriptors (note positions corresponding to the file is shared by the parent and child, which can cause ambiguous cases)
6. Close (close-on-exec) execution flag (Translator's Note: close-on- exec flag () on the file descriptor, POSIX.1 requires that all directory stream must be closed when the exec function calls fnctl. more details, see "UNIX advanced programming environment" WR Stevens, 1993, Jinyuan translated (hereinafter referred to as "high-level programming"), section 8.9 and 3.13)
7. the signal (signal) control settings
8.nice value (Translator's Note: nice nice value is set by a function, the value indicates the priority of the process, the value the smaller, the higher the priority)
process scheduling category (scheduler class) (Translator's Note: refers to the process of scheduling category belongs to the category when the process is scheduled in the system, different types have different priority, according to the process of scheduling categories and nice value, process scheduler to calculate the global priority (global pro each process cess prority), a high-priority process takes precedence)
8. process group number
9. session of the ID (Session ID) (Translator's Note: translation from "Advanced Programming" means: the process of dialogue belongs (session) ID, a session of the group include one or more processes, more detailed description see " advanced programming "section 9.5)
10. The current working directory
11. root directory (Translator's Note: not necessarily the root directory" / ", it may change the chroot function)
12. file way to create word shielding (file mode creation mask (umask) ) (Translator's Note: translation from "advanced programming", refer to: create a new document default mask character)
13. resource constraints
14. the control terminal

sub-process is unique:

process No.
1. different parent process ID (translation by Note: the child different parent process ID number of the parent process and the parent process, the parent process ID can be obtained getppid function)
2. their own copy of the file descriptors and directory streams (Translator's Note: the directory stream created by the opendir function because it is read sequentially, Gu called "directory stream")
3. the child process does not inherit the parent process, the text (text), data and other lock memory (memory locks) (Translator's Note: refers to the memory lock is locked virtual memory page, the lock 4. the kernel will not allow it When a need to change (page out), a detailed description see "The GNU C Library Reference Manual" version 2.2, 1999, Section 3.4.2)
The system time (Translator's Note in tms structure: tms structure may function times get that holds four data recording process for using a central processor: time (CPU central Processing Unit), including: user time, system time, users of each sub-process total time, total process time of each sub-system)
6. resource using (resource utilizations) is set to 0
8. The blocking signal set is initialized to the empty set (Translator's Note: Here description is not clear, slightly modified according to the translation fork function manual page)
9. not created by inheriting timer_create timer function
10. The asynchronous input and output do not inherit

Guess you like

Origin blog.csdn.net/qq_36561697/article/details/90733843