Linux Kernel Analysis--description of processes and creation of processes

Source: Internet
Author: User

Linux Kernel Analysis--description of processes and creation of processes20135111 Li Guangji

Original works reproduced please indicate the source

"Linux kernel Analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000

First, the contents of the experiment

      1. Reading comprehension task_struct data structure http://codelab.shiyanlou.com/xref/linux-3.18.6/include/linux/sched.h#1235;

      2. Analyze the kernel processing process of the fork function Sys_clone, understand how to create a new process and how to create and modify task_struct data structure;

      3. Using the GDB trace to analyze a fork system calling the kernel handler Sys_clone, verifying your understanding of creating a new process for the Linux system, it is recommended to complete the experiment in the lab Building Linux virtual Machine environment.

      4. Paying special attention to where the new process starts? Why does it go smoothly? That is, the execution starting point is consistent with how the kernel stack is guaranteed.

      5. According to the knowledge of this week, we analyze the system call processing process of the fork function, write a signed blog, and in the blog article "real name (and the name of the final application certificate must be consistent) + Original works reproduced please specify the source +" Linux kernel Analysis "MOOC Course/http mooc.study.163.com/course/ustc-1000029000 ", the specific requirements of the blog content are as follows:

        1. Topic self-proposed, the content revolves about how the Linux system creates a new process;

        2. Can combine experiment, draw stack state to execute flowchart, etc.;

        3. The blog content needs to carefully analyze the new process start point and the corresponding stack state.

4. The summary section needs to clarify its understanding of the "creation of a new process for Linux systems"

II. Preparation of the experiment

3.1 Process

1. The process, which is at the execution stage, is not confined to an executable code, but is a generic term for the program in its execution and its associated resources.

In 2.Linux systems, threads are a special process, and there is no obvious distinction between processes and threads.

In 3.Linux systems, the usual fork () process creates child processes. Calls the fork () process into the parent process of its child process.

4.fork () inheritance is actually implemented by the clone () system call. Finally, exit execution via exit ().

3.2 Task descriptor and task structure

1. The task queue is essentially a list of processes and is a two-way circular linked list, where each item is of type Task_struct.

2.Linux distributes the TASK_STRUCT structure through the slab allocator,

3. The process descriptor (PID) is a unique identifying value for each process, with a maximum value of 32768.

The state in 4.PID describes the current status of the process, and each process must be in one of the following v States.

Task_running (Operational): Identifies whether the process is running or waiting to run, which is the only possible state for the process to execute in user space.

Task_interruptlble: (interruptible): The process sleeps, waits for a certain condition to be reached, waits to be awakened.

TASK_UNINTERRUPTLBLR: (Non-interruptible)

_task_traced: Processes that are tracked by other processes

_task_stopped: Stopping the execution process

5. Kernel generally uses set_task_state (State,task) to adjust process status

3.3 Process Creation

1.Linux splits the process creation into two separate functions: fork () and exec (), which copies the current process to create the child process, which is responsible for reading the executable and loading it into the address space to start running.

The 2.Linux fork () function has a write-time copy function, and the data is copied only when needed.

3.fork ()

A wonderful thing about a fork call is that it is called only once, but it can return two times, and it may have three different return values:

1) In the parent process, fork returns the process ID of the newly created child process;
2) in the sub-process, fork returns 0;
3) If an error occurs, fork returns a negative value;

After a successful creation of a new process, there are two fundamentally identical processes in the system, which do not have a fixed sequencing and which process first executes the process scheduling policy to look at the system. At this point, two processes are executed from the fork, but the PID is different.

3.4 Threads implemented in Linux

1. Thread creation is similar to process creation, but it is necessary to pass some parameter flags when calling clone () to indicate which resources need to be shared.

2. The parameters passed determine how the new creation process behaves and the kind of sharing between parent and child processes.

3. Kernel threads differ from normal threads in that kernel threads do not have a separate address space and only run in kernel space. Kernel threads can only be created by other kernel threads whose ancestors are Kthreadd

The Task running indicates that it can be run. Whether it is running depends on whether he has gained control of the kernel

Third, the experimental process

Enter the command in the diagram to complete the tracking preparation.

You can see the content by entering the help command and the Fork command in the kernel.

Return to the Linuxkernel folder and start the GDB trace with some of the following results:

TASK_STRUCT Data structure:

The main contents are as follows:

Task: Management of the process chain list

TTY: Console

FS: File System

Files: Open files

MM: Description of memory management

Signal: Description of inter-process communication signals

The kernel processing for the fork function

Set breakpoints in Sys_clone

Create a new process using system call Sys_clone (or fork,vfork) system calls, and all by calling Do_fork to implement process creation; Linux creates a new process by copying the parent process pcb--task_struct. To assign a new kernel stack to a new process, modify the process data that is copied, such as PID, process chain list, and so on, to perform copy_process and copy_thread.p->thread.sp = (unsigned long) childregs; The kernel stack top p->thread.ip = (unsigned long) ret_from_fork when dispatched to the child process;

Four. Experiment Summary:

Fork, Vfork and clone three system calls can create a new process, and all by calling Do_fork to achieve the creation of the process, set the child process is scheduled IP, that is, the child process starting point (ret_form_fork), through experimental learning, I am more aware of the process of the creation process, I hope to continue to deepen the impression of the Linux kernel to learn more deeply.

Linux Kernel Analysis--description of processes and creation of processes

Related Article

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.