[Linux Systematic Learning] Parent-child relationship of processes | fork process

=========================================================================

Click to go directly to personal homepage:Xiaobai is not a program Yuan

Linux 专栏:Linux system chemistry 习

=========================================================================

Table of contents

Foreword:

parent-child process

Introduction of parent-child process

View parent-child processes 

Query the dynamic directory of a process

Change the working directory of a process

fork creates process

The introduction of fork

Use of fork

The principle of fork

How is fork implemented?

Why is the code before fork not executed?

Why are there two return values?


Foreword:

In the last article, we talked aboutprocess. The program running in the memory and the executed instructions can be a process; and for Linux Have a certain understanding of the process and know how to use the command to view the process and the first system call. There are still many mysteries in the process that we need to explore, so let’s start learning today!


parent-child process

Introduction of parent-child process

Still the code and instructions from the previous article, each process has a PID, and the PPID in front of the attribute list is< a i=3>The ID of the parent process.

We run and terminate the executable program we wrote multiple times, and we will find that each timethe process ID will change, and The parent process ID always remains unchanged. 

 After querying, we can know that this parent process is our command line interpreter (bash) .

Summarize:

  • The essence of starting a process is to create a process, which is usually created through the parent process (father-child relationship)
  • The processes started by our command line are all sub-processes of bash.

View parent-child processes 

When a new process is created, the operating system will create a task_struct (PCB) for each process, which stores various information about the process. information, attributes, etc.; including PID and PPID are operations System internal data , in the operating system article we mentioned that users cannot get the internal data of the operating system through System call is possible, and Linux also provides us with corresponding function interfaces for us to use.

  • Get the child process ID: getpid()
  • Get the parent process ID: getppid()

The usage of these two functions can be queried through the man command.

  1 #include<stdio.h>
  2 #include<unistd.h>
  3 int main()
  4 
  5 {
  6     printf("我是一个进程,我的PID为:%d;我的PPID为:%d\n",getpid(),getppid());                                                                                                     
  7     return 0;
  8 }

 

By writing code, the function call gets our ID and PPID.​ 

Query the dynamic directory of a process

Linux operating system has adynamic directory structure, stores all processes; this directory can be queried through the PID of each process

  • Command: proc/PID

We bring up the complete directory of the entire process

Change the working directory of a process

We can usechdir()to modify the current working directory

  1 #include<stdio.h>
  2 #include<unistd.h>
  3 int main()
  4 
  5 {
  6     chdir("/root");                                                                   
  7     while(1)
  8     {
  9     printf("我是一个进程,我的PID为:%d;我的PPID为:%d\n",getpid(),getppid());
 10     sleep(1);
 11     }
 12     return 0;
 13 }

 


fork creates process

The introduction of fork

There aretwo ways to create a process under the Linux operating system

  • Start the process directly from the command line (Start manually)
  • Create a process through code(fork)

We have been using the first method to create a process, and we will create a process by demonstrating fork below.

Use of fork

man fork See how to use fork

  1 #include<stdio.h>
  2 #include<unistd.h>
  3 int main()
  4 
  5 {
  6     printf("我是一个进程,我的PID为:%d;我的PPID为:%d\n",getpid(),getppid());
  7     fork();
  8 
  9     printf("我是一个进程,我的PID为:%d;我的PPID为:%d\n",getpid(),getppid());
 10     sleep(1);                                                                         
 11     return 0;
 12 }

 

Running our compiled program we found that the same output statement was run twice. This is because there was only one process before fork, and fork created a process, so it was executed twice.

The principle of fork

How is fork implemented?

fork is a function, the function can set the return value; when the fork child process is successful, the PID of the child process is returned to the parent process, and 0 is returned to the child process; when the fork child process fails, -1 is returned; after the child process is successfully created, the fork will be returned A copy of the code means that there are two processes executing the same code, so two statements will be output.

  1 #include<stdio.h>
  2 #include<unistd.h>
  3 int main()
  4 
  5 {
  6     pid_t id =fork();
  7     if(id<0)
  8     {
  9          return 0;
 10     }
 11     else if ( id == 0 )
 12     {
 13         while(1)
 14         {
 15             printf("我是子进程,我的Pid为:%d,我的PPid为:%d\n",getpid(),getppid());
 16         }
 17         sleep(1);
 18     }
 19     else                                                                              
 20     {
 21 
 22         while(1)
 23         {
 24             printf("我是父进程,我的Pid为:%d,我的PPid为:%d\n",getpid(),getppid());
 25         }
 26         sleep(1);
 27     }
 28     return 0;
 29 }

 

Why is the code before fork not executed?

When learning C language, we know that when the code is executed, there will be a pointer to execute a statement, and the pointer will change accordingly; when the fork is executed, the pointer also changes to the fork statement; therefore, the statement after the fork can only be copied to Another process.

Why are there two return values?

This is related to the scenario in which our users use computers, just like I am currently writing a blog on CSDN in the same browser while still looking for relevant information. The browser process contains two processes, and the two processes do different things, or even more. But currently we can only do the same thing and execute the same code. In future studies, we will let them execute different code snippets through return values.


Today’s introduction to the father-son process and the fork process under Linux ends here. I hope you will gain a lot after reading it. You can also comment on the content of the article and share your own opinions in the comment area. Your support is the driving force for me to move forward. Thank you all for your support! ! !

Guess you like

Origin blog.csdn.net/qq_55119554/article/details/134472158