"Linux kernel design and implementation" Reading notes Chapter III

Source: Internet
Author: User

Chapter 3rd Process Management 3.1 process

1. Process
Process is the procedure that is in the execution period.

Processes include:

  • Executable program code
  • Open a file
  • Suspended signal
  • Kernel internal data
  • Processor status
  • One or more memory address spaces with memory mappings
  • One or more execution threads
  • The data segment used to hold global variables
  • ......

In fact, a process is a real-time result of executing program code

2. Thread execution

  • A thread, which is an object that is active in a 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, not a process.

The process provides two virtual mechanisms:

Virtual processors and virtual memory.

Virtual memory can be shared between threads, but each has its own virtual processor.

3. The fork system calls the system call to create a completely new process by copying an existing process. The fork () in the same position at the return point is actually implemented by the clone () system call.

3.2 Process descriptor and task structure

1. The kernel stores the list of processes in a two-way circular chain table called the task queue.

Each item in the list is a structure of type task_ struct called a process descriptor, which is defined in the <linux/sched.h> file.

2. The process descriptor contains all the information for a specific process.

3. The process descriptor contains:

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

1, Linux to allocate the TASK_ struct structure through the slab allocator, so as to achieve the object reuse and cache coloring purposes.

2. struct_ Thread_ info is defined in the file <asm/thread_ info.h>.

3. The THREAD_INFO structure of each task is allocated at the end of his kernel stack.

3.2.2 Process Descriptor Storage

1. The kernel identifies each process by a unique process identity value or PID.

  • PID maximum limit is defined in <linux/threads.h>
  • System administrators can increase the upper limit by modifying the/proc/sys/kernel/pid_max.

2, the hardware architecture is different, the implementation of the macro is also different.

  • You can take out a special register to hold a pointer to the current process task_ struct for faster access.
  • Create the THREAD_ info structure at the end of the kernel and indirectly find the TASK_ struct structure by calculating offsets.
3.2.3 Process Status

the State field in the process descriptor describes the current status of the process.
Five process states:

  • Task_ RUNNING (Run):
    The process is executable; it is either executing, or waiting for execution in the run queue-the only possible state that the process executes in user space.
  • Task_ interruptible (can be interrupted):
    Process is sleeping, waiting for certain conditions to reach,
  • Task_ uninterruptible (non-interruptible):
    Another blocking state in which a process is awakened only when the resource is valid and cannot be awakened by a signal or timed interrupt.
  • Task_ STOPPED (STOP):
    The process stops executing, the process is not put into operation and is not operational, common signals: SIGSTOP, SIGTSTP, Sigttin, Sigttou.
  • Task_ traced:
    Processes that are tracked by other processes.
3.2.4 Setting the current process state

set_ task_ State (task,state) function:

  • The function sets the specified process to the specified state, and a memory barrier is set to force the other processors to reorder.
  • The meaning of set_ Current_ State (task,state) and set_ task_ State (task,state) is equivalent.
3.2.5 Process Context
  • The kernel "executes on behalf of the process" and is in the context of the process--when a program invokes a system call or triggers an exception, it falls into kernel space.
  • The current macro is valid in this context.
  • System calls and exception handlers are explicitly defined interfaces to the kernel. The process only passes through these interfaces to get into kernel execution-all access to the kernel must pass through these interfaces.
3.2.6 Process Family Tree

1. There is an obvious succession relationship between the processes of the Unⅸ system.

  • All processes are descendants of the init process with PID 1.
  • The kernel initiates the INIT process at the last stage of system startup.
  • The process reads the initialization script of the system and executes other related programs, and finally completes the system startup process.

2. The process descriptor of the INIT process is statically allocated as Init_task.

3. The For_ Each_ process (Task) macro provides the ability to access the entire task queue in turn.

3.3 Process Creation

Mechanisms for generating processes:

In other operating systems:

  • First, create the process in the new address space,
  • Read in executable file
  • The final execution begins.

In Linux systems-using the fork () and EXEC () functions

  • Fork () Function: Creates a child process by copying the current process.
  • The EXEC () function: is responsible for reading the executable file and loading it into the address space to start running.
3.3.1 Write-time copy

1, the fork of Linux () is implemented using a write-time copy page--to have the parent and child processes share the same copy.

  • Write-time copying is a technique that can postpone or even eliminate copy data.
  • The replication of a resource only takes place when it is required to be written, and before that, it is shared only in read-only mode.

The actual cost of fork () is to copy the page table of the parent process and create a unique process descriptor for the child process.

3.3.2 Fork ()

1, through the Clone () system call Implementation fork (), Clone () to call Do_ Fork ().

Do_ Fork completes the creation of a lot of work, defined in the Kernel/fork.c file.

2. Work done by Copy_ process ():

  • Call the DUPtaskstruct () to create a kernel stack, a threadinfo structure, and a taskstruct for the new process with the same values as the current process, at which point the child process and the parent process descriptor are identical.
  • Check to make sure that the number of processes currently owned by the current user does not exceed the limit on the resources allocated to it after the child process is newly created.
  • The child process begins to differentiate itself from the parent process, and many members of the process descriptor are cleared 0 or set to the initial value, and the process descriptor members that are not inherited are mainly statistics, and most of the data in the task_struct remains unmodified.
  • The status of the child process is set to task_uninterruptible to ensure that it will not run.
  • Copyprocess () invokes the copyflags to update the flags members of TASK_STRUCT.
  • Call Alloc_pid () to assign a valid PID to the new process.
  • Copy_process () copies or shares open files, file system information, signal processing functions, process address spaces, and namespaces, depending on the parameter flags passed to clone ().
  • Finally, Copy_ process () does the finishing work and returns a pointer to the child process.
3.3.3 Vfork ()

1, Vfork () is similar to fork (), and implementation is done by passing a special flag to the clone () system call.

2, the advantage is not to copy the parent Process page table entries, but ideally, the system is best not to call Vfork ().

3.4 Implementation of threads in Linux

1. The threading mechanism provides a group thread that runs within the same-program shared memory address space, and the threading mechanism supports concurrent programming techniques.

2. Comparison of the thread mechanism of different systems:

In other systems:-Threads are abstracted into a resource that consumes less, and runs fast execution units. In Linux systems:-threads are just a means of sharing resources among processes.

3.4.1 Creating Threads

1, the creation of threads when invoking clone () requires passing some parameter flags to indicate the resources that need to be shared.

2. The clone () parameter flag determines how the new creation process behaves and the kind of resources that are shared between parent and child processes.

3.4.2 Kernel Thread

1. Kernel thread--a standard process that runs independently in kernel space.

2. The difference between a kernel thread and a normal process is that the kernel thread does not have a separate address space.

3. Run the PS-EF command on a Linux system and you can see the kernel thread.

4. The kernel automatically handles this by deriving all the new kernel threads from the Kthreadd kernel process, stating that there is an interface in the

The new task is created by the Kthread kernel into the system calling process through Clone ().

5. Kernel exit mechanism:

  • The kernel thread starts running until it is known that call Do_exit () exits.
  • Other parts of the kernel call Kthread_stop () to exit.
3.5 Process End

1, the process of destruction is caused by itself.

2. The process end calls the Do_ exit function, which never returns.

3. The only purpose of the zombie process exists is to provide information to its parent process.

3.5.1 Deleting a process descriptor

1. In a Linux system, the cleanup work and process descriptor deletions that are required for the process to be terminated are executed separately.

2. Call the Release_task function--The final release of the process descriptor.

3.5.2 The dilemma caused by the orphan process

1. If the parent process exits before the child process, there must be a mechanism to ensure that the child process can find a new parent, otherwise these orphaned processes will always be in a zombie state upon exiting.

2, the solution is to the child process in the current thread group to find a thread as a father, if not, let Init do their parent process. Exit Notify () is called in Doexit (), which calls forgetoriginalparent () and the latter calls findnewReaper () to perform the seek parent.

Summary

The main content of this chapter-the process

  • Why processes are so important
  • The relationship between a process and a thread
  • General characteristics of the process
  • How Linux stores and represents processes
  • How to create a process
  • How to load a new execution image into an address space
  • How to represent the hierarchical relationship of a process
  • How the parent process collects information about its descendants
  • How the process eventually dies.
Problem

1, what is the slab distributor?

  • Slab is a memory allocation mechanism for Linux operating systems.
  • Slab allocators are managed based on objects, and the same type of object is classified as a class, and whenever you want to request such an object, the slab allocator allocates a unit of that size from a slab list, and when it is released, it is re-saved in the list.
  • The slab allocator does not discard allocated objects, but frees them and saves them in memory. When a new object is requested later, it can be fetched directly from memory without repeating the initialization.
  • The memory area of the object cache is divided into multiple slab, each of which consists of one or more contiguous page boxes that contain both assigned and idle objects.
  • Adding the slab allocator to the cache and object is a compromise between time and space.

2. What is a destructor?

  • The destructor name should also be the same as the class name, just precede the function name with a bitwise inverse ~ to distinguish it from the constructor.
  • It cannot take any parameters, and there is no return value (including void type).
  • There can be only one destructor and cannot be overloaded.
  • If the user does not write a destructor, the compilation system automatically generates a default destructor, and it does nothing. So there are no explicit destructors in many simple classes
Resources

1, Slab Baidu Encyclopedia

2. destructor Baidu Encyclopedia

3. "Linux kernel Design and implementation" Chapter III

"Linux kernel design and implementation" Reading notes Chapter III

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.