"Linux kernel Design and implementation" Chapter III reading notes

Source: Internet
Author: User

I. Process (Task) Description 1. The process is a program in the execution period; In addition to executable code, it also includes open files, pending signals, kernel internal data, one or more threads of execution, and many other resources
    • A thread is an object in a process activity; the kernel dispatches a thread rather than a process.
    • In Linux systems, threads and processes are not differentiated
    • There may be two or more processes executing the same program, or even n processes sharing open files, address space, and the like
2. In modern operating systems, processes provide two virtual mechanisms: virtual processors and virtual memory
    • Virtual memory can be shared between threads in the same process, but each has its own virtual storage
3. Overview of the process
    1. The newly created process calls the Exec () function to create a new address space and load the new program into it;
    2. Finally, the program exits execution through the exit () function, and the process exits execution and becomes a zombie process until the parent process calls wait () or waitpid () (returns the state of the terminating process)
Ii. process descriptor 1. Process Descriptor
    1. The list of processes is stored in the task queue, a doubly linked list
    2. The items in the list are task_struct, which is the structure of the process descriptor type.
    3. This type is defined in <linux/sched.h>
    4. Task_struct contains all the resources required by the process

      2. Allocation and storage
    5. Purpose: Linux allocates task_struct structure through slab to achieve object reuse and cache coloring (to avoid resource consumption caused by dynamic allocation and release of resources)
    6. Allocation: The end of the stack for each task (for example, for an upward-growing stack, which is at the top of the stack) has a struct thread_info, which points to the TASK_STRUCT structure
    7. Find:
      1. Most of the code in the kernel that processes the processing process is done through task_struct, so you need to find the process descriptor for the currently running process through the current macro
      2. In the X86 system, current shields the post 13 significant bits of the stack pointer to calculate the Thread_info offset (via the Current_thread_info function)

8. The process must be in one of five states at any time

      1. Task_running
      2. Task_interrupt
      3. Task_uninterrupt
      4. task_traced
      5. task_stopped

9. Set the current status of the process

10. Call the Set_task_state (task,state) function to set the process to the specified state

11. Process Context

    1. Executable code is executed from an executable file loaded into the address space of the process. When a program executes a system call, the kernel "executes on behalf of the process" and is in the context of the process
    2. Contrast: In an interrupt context, the system does not represent process execution-there is no process to interfere with these interrupt handlers
    3. Process Inheritance Relationships
    4. All processes are descendants of the init process with PID 1
Ii. process creation process for UNIX system creation

Fork () Creates a child process by copying the current process

EXEC () is responsible for reading the executable file and loading it into the address space to start running a write-time copy

Linux fork () uses write-time copies to postpone or even dispense copies. The kernel does not replicate the entire address space when it creates a new process, but rather allows the parent process and child processes to share the same copy, until the child process/parent process needs to write it.

Thus, the actual cost of fork is simply to copy the page table of the parent process and create a unique process description for the child process

Fork function

Linux implements fork via clone system call

Called by clone to call Do_fork ()

Define Do_fork () in <kernel/fork.c> to complete most of the work in the creation, which calls the Copy_process function and then lets the process start running

Creation of Threads

In a Linux system, a thread is only considered a process that shares certain resources with other processes. Each thread has its own task_struct

Create thread: Similar to a normal process, except that you need to pass some parameter flags to indicate shared resources when calling clone ()

Kernel threads: The only difference from a normal process is that the kernel thread does not have a separate address space.

It can only be created through other kernel threads, and the kernel derives all kernel threads through the Kthread kernel process

The newly created thread is in a non-operational state until wake_up_process () explicitly wakes it

Iii. process End Delete process descriptor
    1. This task is performed separately from cleanup work, because the system can still get its information after the process is terminated
    2. Implementation of Process descriptor deletion via Release_task ()
    3. At this point, all resources have been released.
Addressing the orphan process
    1. Overview: The parent process exits before the process, leaving behind the child process, the orphan process
    2. Workaround: Find a new parent process for the orphan process within the current thread group, or directly with Init as its parent process
      • Call Order: Do_exit ()-->forget_original_parent ()-->find_new_parent ()-->ptrace_exit_finish () (This function looks for the parent process for the tracked process, because the tracked process takes the debugger as the temporary father)

"Linux kernel Design and implementation" Chapter III reading notes

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.