"Linux kernel Design and implementation" Chapter III study notes

Source: Internet
Author: User

Chapter III Process Management

"Learning time: 1 hours 30 minutes Compose Blog time: 2 hours"

"Learning content: Process descriptors, process creation and finalization, thread creation"

Processes are the most basic of the UNIX operating system abstraction concepts. Process management is the heart of all operating systems.

First, the process

1. The process is a procedure at the time of implementation. In addition to executable code, it also includes open files, pending signals, kernel internal data, one or more execution threads, 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. Thread: Is the object that is active in the process. Each thread has a separate program counter, a process stack, and a set of process registers. the kernel dispatches an object that is a thread.

3. 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

4. The life cycle of the process

    • The newly created process calls the Exec () function to create a new address space and load the new program into it;
    • 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 () to return the status of the terminating process

5. The process begins to survive at the time it was created, which is usually the result of invoking the fork system. The system call creates a completely new process by copying an existing process. The fork system call returns two times from the kernel, one time to the parent process, and another to the newly generated child process.

Ii. process descriptors and task structures

Process Descriptor : The process list is stored in a doubly linked list (Task list), which is the structure of the task_struct that is the process descriptor. The type is defined in <linux/sched.h>. The data contained in the process descriptor can fully describe an executing program:

    • It opens the file
    • The address space of the process
    • Suspended signal
    • Status of the process
    • Other more information

2.1 Assignment of process descriptors
    • Objective: Linux allocates TASK_ struct structure through slab, in order to achieve object reuse and cache coloring, so as to avoid resource consumption caused by dynamic allocation and release of resources.
    • The task_ struct is now dynamically generated with the slab allocator, so it is only necessary to create a new struct struct Thread_ info on the bottom of the stack (for the downward-growing stack) or on the top of the stack (for an upward-growing stack)

    • The end of the stack for each task (for example, for an upward-growing stack, which is at the top of the stack) is a struct Thread_ info, which points to the task_ struct struct
2.2 Storage of process descriptors

1. Most of the code in the kernel that processes the processing process is done through the 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 thread_ the latter 13 significant bits of the stack pointer to calculate the offset (via the Current_ thread_ info function)

MOVL $-8192,%%esp,%eax3. Process status

2.3 Process Status

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

    • Task_running Run
    • Task_interrupt can be interrupted
    • Task_uninterrupt Non-disruptive
    • Task_traced processes that are tracked by other processes
    • task_stopped Process stops running

    • Task_running: May be running or may indicate an executable
    • Task_ interrupt/task_uninterrupt: Both indicate that it is blocking, but the latter state will not be awakened after receiving a signal.
2.4 Setting the current state of the process

Call the set_ task_ State (task,state) function to set the process to the specified status

2.5 Process Context
    • 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
    • Contrast: In an interrupt context, the system does not represent process execution-there is no process to interfere with these interrupt handlers
2.6 Process Family Tree
    • All processes are descendants of the init process with PID 1
    • For a given process, get the next process in the list:

      List_ entry (task->tasks.prev,struct task_struct,tasks)

Third, process creation

How the UNIX system processes are created

    • 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
3.1 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.
    • The actual cost of fork is simply to copy the page table of the parent process and create a unique process descriptor for the child process
3.2 Fork ()
    1. Linux implements fork via clone system call
    2. Called by clone to call Do_fork ()
    3. Define Do_ fork () in <kernel/fork.c> to complete most of the work in the creation, it calls the Copy_process function, and then lets the process start running

The last copy_process returned is a pointer to the child process.

3.3 Vfork ()

The vfork () system call and the fork () function are the same except for page table entries that do not copy the parent process. The child process runs as a separate thread of the parent process in its address space, and the parent process is blocked until the child process exits or executes exec ().

The implementation of the Vfork () system call is done by passing a special flag to the clone () system call:

Iv. implementation of threads in Linux

The threading mechanism provides a set of threads that run in shared memory address space within the same program. 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.

4.1 Creating Threads

1. thread creation is similar to normal process, except that you need to pass some parameter flags to indicate the shared resource when you call Clone () .

2. The parameter flags passed to clone () determine how the new creation process behaves and what kind of resources are shared between parent and child processes

4.2 Kernel threads

Kernel threads differ from normal processes only in that kernel threads do 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
V. The end of the process

Most of the finalization process relies on do_exit () to complete:

5.1 Deleting a 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.
5.2 Resolving the Orphan process

1. Orphan process : The parent process exits before the process, leaving behind the child process, the orphan process

2. Workaround : Find a new parent process within the current thread group for the orphan process, or directly with Init as its parent process

    • Call Order: Do_ exit ()-->forget_ Original_ parent ()-->find_ new_ parent ()-->ptrace_ exit_ finish ()

Once the system successfully finds and sets up a new parent process for the process, there is no danger of hosting the zombie process again. The init process routinely calls wait () to check its child processes, clearing all of its associated zombie processes.

Summarize

Through this chapter, I have a deep understanding of the core concepts of the operating system-the process, and the relationship between processes and threads. It also mastered the process of storing and representing processes in Linux using Task_ structs and thread_info, and creating processes through fork (). A process is a very basic abstraction that plays a critical role in the understanding of process scheduling.

"Linux kernel Design and implementation" Chapter III study 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.