Understanding the operating system through Linux (III): Process Management (II)

Source: Internet
Author: User

1. process representation

Linux abstracts processes into tasks and defines a struct task_struct to indicate a task. For each process, in its lifecycle, there will be a corresponding task_struct process descriptor in the memory, saving the important information required by the kernel for managing processes. A task_struct contains the following fields:

By saving the above information for each process, the system kernel can manage processes reasonably. For example, when a process is scheduled, the kernel needs to obtain the priority of each process to determine the time slice. When a process receives a signal, the kernel needs to check the method specified by the process for processing. All the information needs to be searched in the process descriptor.

Because the system runs multiple processes at the same time, multiple process descriptors are saved in the memory. To facilitate management and support quick search, the kernel maintains a hash table, use the PID as the key value and use the hash to solve the conflict. The elements in the same slot are connected using a two-way linked list, as shown in: (only)

With the above storage method, when the kernel needs to find a process descriptor, it only needs to map the process ID to a slot in the hash table, and perform sequential search on the linked list of the slot. Next, let's look at how a process is created in a linux system. With the above information, this process is easy to understand.

When a fork system calls the execution, the fork process will switch to the kernel mode and create a process descriptor of the task_struct type (there are other structures that will not be mentioned here ), most of the content in the newly created process descriptor will be set based on the parent process's process descriptor. Then, the system allocates a new PID and maps the PID to the corresponding slot in the hash table, if the slot is occupied, a new element is added to the linked list, which stores the memory address of the new process descriptor. Next, the system allocates memory space for the child process and copies the content in the memory space of the parent process. After this process is completed, the child process can start to run.

2. Process Scheduling

After learning about the process representation, let's look at how to implement process scheduling in linux.

(1) thread: in linux, scheduling is based on threads. Generally, processes are considered as resource containers, and threads are considered as an execution unit (that is, a continuous, in fact, the corresponding task_struct corresponds to a thread. A single-threaded process is represented as a task structure, while a multi-threaded process has multiple task structures, each thread has one. (This may be a bit messy, because the concepts of processes and threads in linux are vague. Unlike other systems, processes, lightweight processes, and threads are differentiated. However, we do not need to consider so much during programming, so there is no impact)

(2) Priority: linux divides threads into three types: Real-Time FIFO threads, real-time rotation threads, and common time-sharing threads.

The difference between real-time and common time-sharing is that the priority is different. The real-time thread is 0-99, and the common time-sharing thread is 100-139 (a total of 140 priority threads, 0-139 ), the lower the priority value, the higher the priority. The difference between FIFO and rotation is that FIFO is not preemptible, that is, after the arrival of the thread task is completely completed, the next thread task can be carried out, rotation means that each thread is allocated with a time slice, And the thread can run in the time slice. When the time slice is used up, the next thread is switched to run regardless of whether the current task is completed or not.

In addition to the priority, there is another value related to process scheduling, which is the NICE value in the range of-20 ~ + 19. The nice value indicates the friendliness of other processes. The friendly value indicates that the cpu time is sent out. The greater the NICE value of all processes, the less cpu time it uses, and the default value is 0. You can use the nice system call to modify it. The priority and NICE value jointly determine the cpu running time of a process.

(3) Data Structure of process scheduling: the linux scheduler maintains a data structure for each cpu to become runqueue, as shown below:

As shown in, a runqueue has two fields active and expired. They are a pointer pointing to an array with a length of 140, respectively, each array contains 140 head pointers of the linked list. Each linked list actually corresponds to a priority, and the linked list contains processes with this priority. The scheduling process of the scheduler is basically: first, a task is taken from the list with the highest priority in the active state for execution. When the time slice of the process consumes, move it to the linked list of the corresponding priority in expired, and then extract the next process for execution. In this way, the process with the highest priority is guaranteed to be executed first, and the process has a higher priority, the longer the time slice is allocated. After all processes in the active state have been executed, as long as the two pointers of active and expired are switched, the current process in the original expired state is now active again, repeat the preceding steps to ensure that low-priority processes can obtain cpu time.

OK, the process management part of the linux system is here. In fact, the implementation of this part is still very complicated. I can't make it clear if I have a limited level, in addition, the impact on practical applications is not very great, so we have no interest in further research. As long as we know the mechanisms and ideas of its implementation, prove that it is really a program, and the design methods are diverse, there is no absolute standard to achieve the goal, the next article will enter the linux memory management module, so stay tuned.

 

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.