Deep understanding of Linux kernel reading notes-Chapter 3-process (2)

Source: Internet
Author: User
Process switching: to control the execution of processes, the kernel must be able to suspend processes running on the CPU and resume the execution of a previously suspended process, this behavior is called process switching, task switching, or context switching. Hardware context: although each process can have its own address space... information & n process switching:
 
To control the execution of processes, the kernel must be able to suspend processes running on the CPU and resume the execution of a previously suspended process. this behavior is called process switching, task switching, or context switching.
 
Hardware context:
 
Although each process can have its own address space, all processes must share the CPU register. Therefore, before resuming the execution of a process, the kernel must ensure that each register is loaded with the value of the suspended process.
 
A set of data that must be loaded into the register before the process resumes execution is called the hardware context. The hardware context is a subset of the executable context of a process, because the executable context contains all the information required for the process. In Linux, part of the hardware context of a process is stored in the task status (TSS) segment, while the remaining part is stored in the kernel state stack. Process switching only occurs in the kernel state.
 
Execute process switching:
 
Essentially, each process switchover consists of two steps:
1. switch the global directory on the page to install a new address space.
2. switch the kernel state stack and hardware context
Creation process:
 
The Unix operating system depends on the process creation to meet your needs.
 
The modern Unix kernel introduces three different mechanisms to solve the problem of process creation:
1. write-time replication technology, allowing sub-processes to read the same physical page. As long as one of the two attempts to write a physical page, the kernel copies the content of this page to a new physical page and assigns this new physical page to the writing process.
2. lightweight processes allow parent and child processes to share many data structures of each process in the kernel
3. the process created by the vfork () system call can share the memory address space of its parent process. To prevent the parent process from overwriting the data required by the child process, the execution of the parent process is blocked until the child process exits or executes a new program.
Clone (), fork (), and vfork () system call:
 
In Linux, a lightweight process is created by a function named clone. Clone () is an encapsulation function defined in the C Language Library. it establishes a new stack of lightweight processes and calls the clone () system call hidden by programmers.
 
Traditional fork () and vfork () systems are also implemented using clone () in Linux.
 
The do_fork () function processes clone (), fork (), and vfork () system calls.
 
Kernel thread:
 
The modern operating system delegates important tasks, such as refreshing the disk cache, exchanging unused page boxes, and maintaining network connections to the kernel thread, the kernel thread is not dragged down by unnecessary user context.
 
In Linux, kernel threads are different from common processes in the following aspects:
1. the kernel thread runs only in the kernel state, while normal processes can run both in the kernel state and in the user state.
2. because the kernel thread runs only in the kernel state, it only uses linear address space greater than PAGE_OFFSET. no matter in the user state or kernel state, General processes can use 4 GB linear address space.
 
The kernel_thread () function creates a new kernel thread. it accepts the following parameters: Address of the kernel function to be executed, parameters to be passed to the function, and a set of clone flag. This function essentially calls do_fork ().
 
All processes are called process 0, idle process, or swapper process for historical reasons. it is a kernel thread created from none in the Linux initialization phase. The stark_kernel () function initializes all the data structures required by the kernel, activates the interrupt, and creates another kernel thread (init process) called Process 1 ). The PID of the newly created kernel thread is 1, and all kernel data structures of the process are shared with process 0.
 
After the init process is created, process 0 executes the cpu_idle () function. in essence, this function repeatedly executes hlt assembly language commands when the process is interrupted. The scheduler selects process 0 only when no other process is in the TASK_RUNNING state.
 
In a multi-processor system, each CPU has a process 0. as long as the power is turned on, the computer BIOS will start a CPU and disable other CPUs. The swapper process running on CPU0 initializes the kernel data structure, activates other CPUs, and creates another swapper process through the copy_process () function, PASS 0 to the newly created swapper process as their new PID. In addition, the kernel assigns appropriate CPU indexes to the cpu field of the thread_info descriptor of each process created by the kernel.
 
The kernel thread created by process 0 executes the init () function, and init () completes kernel initialization in turn. Init () calls execve () system call to mount the executable program init. as a result, the init kernel thread becomes a common process and has its own kernel data structure for each process. Before the system is shut down, the init process remains alive because it creates and monitors the activities of all processes executed outside the operating system.
 
Undo process:
 
The process is generally terminated by calling the exit () function, which releases the resources allocated by the C function library and executes each function registered by the programmer, and end the system call that recycles the process from the system.
 
Process termination:
 
There are two system calls in Linux2.6 to terminate the user-State application:
1. exit_group () system call. it terminates the entire thread Group, that is, the entire multi-thread-based application. Do_group_exit () is the main kernel function to implement this system call.
2. exit () is called by the system. it terminates a thread regardless of all other processes in the thread group to which the thread belongs. Do_exit () is the main kernel function to implement this system call.
 
Process deletion:
 
Unix allows a process to query the kernel to obtain the PID of its parent process, or the execution status of any of its child processes, including the termination status. Therefore, the Unix kernel is not allowed to discard data contained in the process descriptor field after the process is terminated. only after the parent process sends a wait ()-class system call related to the terminated process,. This is the reason for the introduction of the zombie state: Although the process is technically dead, it must save its descriptor until the parent process is notified.
 
If the parent process ends before the child process ends, there will be zombie processes everywhere in the system, and their process descriptors will occupy RAM permanently. therefore, all orphan processes must be forced to become child processes of the init process. In this way, when the init process uses a wait () system call to check its legal sub-process termination, it will cancel the Zombie process.

From Crazy Bird
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.