Linux process scheduling and process structure __linux

Source: Internet
Author: User


Linux Process scheduling

type of scheduling policy

The process of the Linux kernel connects the struct TASK_STRUCT structure of the process through a double linked list, and the task_struct structure contains all the information associated with a process (such as the state of the process, priority, The address space of the process, etc.); Each process has a unique process descriptor, and Linux is the process descriptor that perceives the existence or demise of a process, and the structure of the process is defined in the <linux/sched.h> of the kernel.

The following is the definition of the Linux kernel process scheduling policy

Linux process scheduling

1, Sched_normal (Sched_other): Ordinary time-sharing process, this is a common user process, the default type of process, the use of dynamic priority scheduling strategy, the selection process based mainly on the size of the process goodness value. This process can be preempted by a process with a high goodness value at run time.

2, Sched_fifo: First in the first out of the real-time process, the CPU is always running. Run until a higher priority task arrives or you give up.

3, SCHED_RR: Time slice rotary real-time process, each scheduling, the CPU allocation team first process, and make it perform a time slice. When a time slice is run out, a clock interrupt request is issued by a timer, and the process is stopped and sent to the end of the ready queue;

4, Sched_batch: Batch processing process

5, Sched_edle: Only System Idle things can be scheduled to execute the process

second, scheduling classification

1,CFS(completely Fair scheduler,cfs) scheduling Category: Sched_normal, Sched_bath, Sched_idle

2, real-time scheduling class: Sched_rr, Sched_fifo

iii. timing of dispatching

1. Active: Call schedule () directly in the kernel. When the process needs to wait for resources and so on and temporarily stop moving, the state will be put in suspend (sleep), and actively request scheduling, let the CPU.

2, passive type (preemption)

User preemption: Linux2.4, Linux2.6

Kernel preemption: Linux2.6

(1) User preemption

User preemption occurs when a user space is returned from a system call, from an interrupt handler to a user space.

(2) Kernel preemption:

In a system that does not support kernel preemption, the process/thread is executed as soon as it is running in kernel space, knowing that it is actively discarding or the time slice is exhausted. Such very urgent processes or threads will not run for long periods of time.

In systems that support kernel preemption, higher-priority processes/threads can preempt low-priority processes/threads that are running in kernel space.

In the system that supports kernel preemption, the kernel preemption is not allowed in the special case:

A The kernel is doing interrupt processing. The process scheduling function, schedule (), makes a decision about this and prints the error message if it is invoked in an interrupt.

b The kernel is processing the bottom Half (the interrupted bottom half) of the interrupt context. The hardware interrupt returns and performs a soft interrupt before it is returned, and is still in the interrupt context.

c The process earns hold spinlock spin lock, Writelock/readlock read-write lock, etc., when holding these locks, should not be preempted, otherwise, because preemption will lead to other CPUs for a long time can not get locks and Deng.

D) The kernel is executing the scheduler scheduler. Preemption is due to the new scheduling, there is no reason to preempt the scheduler to run the scheduler.

To ensure that the Linux kernel is not preempted in the above case, the preemption kernel uses a variable preempt_count called the kernel preemption count. This variable is set in the THREAD_INFO structure of the process. Each time the kernel enters the above several states, the variable Preempt_count adds 1, indicating that the kernel does not allow preemption. Whenever the kernel exits from the above several states, the variable preempt_count is reduced by 1 while the judgment and dispatch can be preempted.

Kernel preemption can occur when:

The interrupt handler completes before returning to the kernel space.

When the kernel code once again has the ability to preempt, such as unlock and enable soft interrupt.

Four, schedule function workflow :

1) Clean up the currently running process

2 Select the next process to run (Pick_next_task)

3 Set up the new working environment.

4 process Context switch.

v. Allocation of process descriptors:

Linux through a slab allocator to create task_struct process structure, each task_struct placed in the end of the kernel stack, as long as through a stack of pointers can be found task_struct Location, because the process structure is created dynamically through the slab allocator, a new structure struct Thread_infois created at the end of the stack, and thethread_info structure varies according to the processor architecture selected. thread_info is defined in arch\xxx\include\asm (XXX represents the selected processor architecture), such as the thread_info definition of the MIPS architecture as follows:

The relationship between the kernel stack in the process and the process descriptor is shown in the following illustration:

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.