Linux kernel Analysis Week sixth-the process of analyzing the Linux kernel creating a new process

Source: Internet
Author: User

The Linux kernel is one of the important tasks of the operating system for process management.

This experiment is an overview of the kernel's process of creating a new process.

For simplicity, create a process using fork and then user state. The code is as follows:

Here is the preparation??? CD LINUXKERNELRM-RF menugit clone https://github.com/mengning/menu.gitcd menumv test_fork.c test.cmake rootfs

Open GDB for remote debugging?

Set Breakpoint B sys_cloneb do_forkb DUP_TASK_STRUCTB COPY_PROCESSB copy_threadb ret_from_fork

The general process in Linux is created by an existing process, which is what we call the parent process, the child process. The specific creation is achieved by fork (). Let's take a look at the general working process of fork () in the 0.11 core:

1) in memory, request a page of memory storage Process Control block task_struct, and return the process number NR, and the task array in the NR place task_struct pointer, but also the task of the current pointer to the NR place;

2) Copy the contents of the parent process's task_struct to the task_struct of the new process as a template

3) To modify the information in the Task_struct, the main work: Set the parent process, clear the signal bitmap, time slice, run time, set the TSS according to the current environment (the kernel pointer esp0 points to the top of the Task_struct page), set the LDT selector, etc. ( According to NR, the corresponding LDT descriptor in the GDT is pointed.

4) Set the code snippet for the new process, the base address of the data segment, and the section length: Update the code start address in Task_struct: Process number (NR) x64m, update the code snippet and data segment descriptor in the local Descriptor table in Task_struct. 5) Copy the page table directory entries and page tables of the parent process: In the page Catalog table, the page table directory entry for the parent process is copied, the destination address is computed from the linear address of the new process, a free page is requested for each corresponding page table directory entry, the page table directory entry is updated with the page table address, and the items in the parent Process page table are copied to the corresponding page table of the new process. , that is, the child process shares physical memory with the parent process at this time.

6) Update the file information in task_struct: File open number plus 1, parent process's current directory reference number plus 1. 7) Set the TSS and LDT descriptor entries: Set the TSS descriptor entry for the new task in the Global Descriptor List (GDT) and the descriptor entry for the LDT segment, so that the TSS descriptor entries and the LDT description entry respectively point to Task_struct's TSS structure and the LDT structure.

8) Set the task to the ready state to return the new process number to the current process (parent process).

As you can see, in fork (), the kernel does not immediately allocate code and data physical memory pages for the new process, and the new process uses the parent process to work with the existing code and data physical memory pages of the parent process. Only memory pages that are accessed when the memory is accessed by one process during the subsequent execution are copied to the new requested memory page before the write operation.

In addition, at the end of the fork is set the task to a ready state, because the fork () is a system call, in the system call section SYSTEM_CALL.S, you can see after the system function returns, call the Dispatch function schedule (), in schedule (), The ready state of the new process is detected and switched to the new process with switch_to () to execute.

Linux kernel Analysis Week sixth-the process of analyzing the Linux kernel creating a new process

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.