Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze Linux kernel source code do_fork

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article shows you how to analyze the Linux kernel source code do_fork, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

We all know that process is the most important abstract concept in the Linux kernel, so how on earth do we produce a process when we fork it?

Push will talk a little bit about the do_fork function, which plays an important role in process creation.

How the kernel abstracts a process

The kernel abstracts a process through a structure called task_struct, which is defined (in the case of kernel 2.6) in include/linux.sched.h.

The intercepted task_struct is as follows:

The above task_struct attributes are the attributes in the structure of the part selected in our section, from which we can roughly understand that the properties that identify a process will roughly indicate the state of the process, the flag of the process, and whether the process is tracked by other processes, the depth of the process lock, the priority of the process, the pid of the process, the parents of the process, the child list of the process, and the list of file descriptors opened by the process. The file system in which the process is located, the signal of the process. Wait, wait, wait.

Do_fork simple analysis

People who come into contact with linuxC programming know that to create a process, we need to call the fork function, fork actually calls the clone function to achieve, and the most critical function of the clone function is the do_fork function.

Before analyzing do_fork, we can imagine roughly how a process is created, and what would you do if you were asked to create a process?

We can analyze it this way, since the original process is abstracted into a task_struct, then the new process is also a task_struct, but some of its attributes will be different from the original task_struct, then the job to create a new process is to assign a task_struct structure that is the same as the original process, and then modify the properties of the new process that is different from the original task_struct.

Do_fork is defined in the kernel/fork.c file.

Before analyzing the function, let's analyze the parameters of its function.

The parameters are as follows:

1.clone_flags: this parameter is the most important parameter in this function, and each bit in this value represents the setting of each property in the child process task_struct

2.stack_start: the start address of the child process's user state stack

3.regs: when a system call occurs, you need to switch from the user mode to the kernel state. This structure is used to store the values in the general register in the user mode process and store them in the kernel state stack.

4.stack_size: not currently in use, usually set to 0

5.parent_tidptr: the pid address of the parent process in user mode

6.child_tidptr: the pid address of the child process in user mode

The flag bit macro of clone_flags is defined as follows:

To take a simple example, when we set the macro CLONE_VM in our parameters, we assume that our newly created process and its parent process want to share VM. When we set CLONE_FILES, it means that the open file descriptor is shared between the parent and child processes.

The first thing do_fork does when it starts execution is to define a new task_struct pointer for the child process:

Struct task_struct * p

Below, we will examine some bit combinations that are not allowed by clone_flags, such as:

If (clone_flags & CLONE_NEWUSER) {if (clone_flags & CLONE_THREAD) return-EINVAL;}

In the above, it is not allowed to set both the CLONE_NEWUSER flag and the CLONE_THREAD flag at the same time, which will cause an error.

Similar to the above, when a series of security checks are completed, the copy_process function enters the stage. The workflow of the copy_process function is as follows:

1) call the dup_task_struct function to create a kernel stack, thread_info structure, task_struct, etc. for the new process, of course, the values are exactly the same as the parent process.

The dup_task_struct function is defined as follows:

2) after checking and ensuring that the child process is newly created, the number of processes owned by the current user does not exceed the resource limit assigned to it, as follows:

3) the child process sets out to distinguish itself from the parent process. Many properties inherited from the parent process have to be cleared 0 or set an initial value, but most of the data in the task_struct has not been modified. Some of the code is as follows:

4) assign a CPU to the child process as follows:

Sched_fork (p, clone_flags)

5) then the child process copies some resources of the parent process, as follows, call the copy_files function to copy the file descriptor opened by the parent process:

Call copy_fs to inherit the file system to which the parent process belongs.

Calling the copy_signal function to copy and set the new signal_struct,signal_struct contains a lot of information about the running of the process, and the copy_mm function is called to deal with the memory problems with the new process.

Call the copy_io function to copy the Istroke O of the parent process:

There are also calls to copy_namespaces and copy_thread, which will not be repeated here.

6) call alloc_pid to assign a pid to the new process.

Pid = alloc_pid (p-> nsproxy- > pid_ns)

7) copy_process does some finishing touches and returns the task_struct pointer of the new process, which returns to do_fork again, and the newly created child process is awakened and put into operation first.

Summary

With regard to the source code understanding of process creation, I feel that I can mainly grasp two points. What is the process abstracted into by the kernel? We must have some understanding of its data structure (task_struct). The most important thing in the second creation process is to copy the attributes in the task_struct of the parent process, but the key point is which to copy and which are different between the child process and the parent process. It is very simple that we only need to grasp the clone_flags parameters in the process creation function to know how to copy.

The above content is how to analyze the Linux kernel source code do_fork, have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report