Linux system process Deep understanding

Source: Internet
Author: User
Tags data structures

1. What is a process

A process is a program that is in execution and a generic term for all the resources it contains, including virtual processor, dummy space, registers, stacks, global data segments, and so on.

In Linux, each process is created with a data structure called the Process Control block, or PCB. PCB contains a lot of important information for system scheduling and process itself to perform use. All processes of the PCB are stored in the kernel space. The most important information in the PCB is process PID, the kernel through this PID to uniquely identify a process. The PID can be recycled, with a maximum of 32768. The init process has a PID of 1, and the other processes are descendants of the INIT process.

In addition to the Process Control block (PCB), each process has a separate kernel stack (8k), a process descriptor structure that is stored in kernel space as control information for the process, while the user space of the process primarily stores code and data.

 2. The creation of the process

Processes are created by calling:: Fork (),:: Vfork () and:: Clone () system call to create a new process. In the kernel, they are all implemented by calling Do_fork. The traditional fork function directly copies all the resources of the parent process to the child process. and Linux:: Fork () Use write-time copy page implementation, that is, the parent process and child processes share the same copy of the resource, only when the data changes, the data will occur replication. In general, EXEC () is invoked immediately after the child process is created, thus avoiding replicating all the resources of the parent process.

The difference between the three is as follows:

:: Fork (): All data structures of the parent process are copied to the child process (write-time copy page).

:: Vfork (): Copies only the task_struct and kernel stacks, so only one thread of the parent process (no separate user space) is generated.

:: Clone (): Powerful, with many parameters. :: Clone () allows you to selectively inherit resources from the parent process, you can choose between like:: Vfork () and the parent process to share a virtual space, so that you create threads, you can not share with the parent process, you can even choose to create processes and the parent process is no longer a parent-child relationship, It's a brotherly relationship.

3. Cancellation of the process

The process exits execution by calling exit (), which ends the process and frees all resources. The parent process can query whether the child process ends by WAIT4 (). The process exits execution in a zombie state until its parent process calls wait () or waitpid (). When the parent process exits, the kernel specifies the other processes of the thread group or the INIT process as the new parent process for its child processes. The kernel can force a process to terminate when a process receives a signal that cannot be processed or ignored, or when an unrecoverable CPU exception is generated in the kernel state and the kernel is running on behalf of the process.

4. Process Management

The kernel stores process information in a bidirectional cyclic list called Task List (kernel space). Each item in a list is of type task_struct, called a process descriptor, that contains all the information for a specific process, including open files, the address space of the process, the pending signal, the status of the process, and so on.

Linux allocates task_struct through the slab allocator, which achieves object reuse and cache coloring (by proactively allocating and reusing task_struct to avoid resource consumption resulting from dynamic allocation and deallocation).

The kernel organizes all processes in the task_running state into a running bidirectional loop queue. The scheduling function gets the most worthwhile execution of the process by scanning the entire operational queue. Avoid scanning all processes and improve scheduling efficiency.

5. The kernel stack of the process

Linux allocates a 8KB memory area for each process, which holds two different data structures for the process: Thread_info and the kernel stack of the process.

When a process is in a kernel state, it uses a different stack than the user state, and the kernel controls the path with very few stacks, so the stack and descriptor say 8KB is enough.

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.