Reprinted Please note: Author: gogofly mailbox: gogofly_lee@yahoo.com.cn
Task Scheduling and context switching
1. Task Scheduling Overview:
Schedulers is the main responsibility of the kernel. In fact, it is a judge who decides which task currently occupies the CPU. Most Real-Time kernels are based on the priority scheduling algorithm, each task is given a certain priority based on its importance. Based on this algorithm, the CPU always gives priority to the tasks with the highest priority. However, the kernel type determines when a high-priority task can obtain the CPU usage right. There are two types of priority-based kernels:
Not preemptible or preemptible.
1) Non-preemptible kernel:
The non-preemptible kernel requires each task to voluntarily give up the CPU usage right, during which it cannot be preemptible by high-priority tasks. It has the following features:
A. As task switching is not required when the return is interrupted, the interrupt response is fast.
B. You can call the non-reentrant function at the task level without worrying about data corruption.
C. Almost no semaphores are needed to protect shared data. That is to say, data is exclusive During task running.
But its biggest drawback is that the response time is uncertain. When a higher-priority task is ready, it does not know when it will be executed, which is a fatal defect in the real-time system. Therefore, the non-preemptible kernel must be used in the front and back-end systems.
2) preemptible kernel:
In an embedded system, processes (tasks) are preemptible. By setting a priority for each process (task), when the system has a higher priority than the currently running process (task) when a process (task) with a higher priority is executed, the current process (task) is interrupted and the scheduler is called to select a higher priority process (task) for running. With the preemptible kernel, high-priority processes (tasks) can be executed first to ensure real-time system response.
In a multitasking system, process (task) scheduling mainly includes the following aspects:
2. Task Scheduling:
In a multi-task system, a system function is provided to switch between processes (tasks). In general, there are two ways to switch between processes (tasks:
1) The process (task) itself directly calls the task switching function to switch the process (task:
When the current process (task) is immediately blocked because it cannot obtain necessary resources, the process (task) will directly call the process (task) switchover function to implement the process (task) inter-scheduling.
In Linux, you can directly call the schedule () function.
In UCOS, ossched () is called.
2) Delay in calling the task switching function for process (task) Switching:
This method sets a scheduling flag for the current process (task) and calls the task switching function in delayed mode to switch the process (task.
In Linux, this scheduling flag is always checked before the execution of the user State process is resumed. The flag here is tif_need_resched. If this flag exists, call the scheduling function to switch the process. This situation mainly includes the following types:
A. The current process has used up its CPU time slice, And the scheduler_tick () function has completed the delayed call of Schedule.
B. When the priority of a wake-up process is higher than that of the current process, the try_to_wake_up () function completes the delayed call of Schedule.
C. When the system calls sched_setscheduler.
In these cases, the kernel state is entered due to system calls or interruptions, or the user State is returned when the current process is in the kernel state.
In UCOS, all tasks have different priorities, so there are no tasks with the same priority and there is no concept of system call, therefore, the delayed call of task scheduling can only occur when the interrupt processing is completed and the return result is returned. In the osintext () function, check whether a task with a high priority is ready. If a task with a high priority is ready, switch tasks.
3. Scheduling Algorithm:
In Linux, a complex scheduling algorithm is selected, which can be divided into the following types:
Sched_fifo:This algorithm is mainly used for real-time processes. When the scheduler assigns the CPU to the current process, if there is no process with a higher priority to run, this process will continue to occupy the CPU until the process exits or voluntarily gives up the CPU, even if there are other processes with the same priority.
Sched_rr: The real-time process of time slice round-robin. For processes with different priorities, processes with the same priority will be scheduled based on the time slice. When the time slice of the current process is used up, other processes with the same priority are scheduled to run to ensure the CPU scheduling fairness of processes with the same priority.
Sched_normal:This algorithm is mainly used for common processes and time-based scheduling.
In the UCOS system, all tasks are real-time tasks, so there is no common task scheduling mechanism. In order to simplify the scheduling algorithm, different tasks have different priorities, it is impossible to have multiple tasks with the same priority. In fact, its scheduling algorithm is only sched_fifo in Linux, that is, a task with the highest priority can be preemptible to a task with a lower priority.
4. Context switching:
Context switching is the core of multi-task scheduling and also the basis for running multiple programs in parallel on a single CPU.
Task context: the environment in which a task runs. For example, for x86 CPUs, the task context can include program counters, stack pointers, and General registers.
Context switching: In a multitasking system, context switching refers to the event that occurs when the control of the CPU is transferred from the running task to another ready task, the current running task is in the ready (or suspended or deleted) state, and the other selected ready task becomes the current task. Context switching includes saving the running environment of the current task and restoring the running environment of the task to be run. The context content depends on the specific CPU.
For different hardware architectures, context switching involves two steps:
A. If there is virtual memory, switch the global directory of the page to install a new address space.
B. Switch the kernel stack and hardware context because the hardware context provides all the information required for the kernel to execute a new process, including CPU information.
In the preemptible kernel, context switching using interrupts is an ideal mechanism. When an interrupt occurs, the interrupt forces the CPU to give control to the operating system, which is equivalent to a context switch. This not only reduces the consequences of program errors, but also improves the switching efficiency. UCOS is a typical example of context switching using the interrupt mechanism.
In UCOS, if the scheduler determines that the task needs to be switched, it will call context switching OS _task_sw () for actual context switching. OS _task_sw () is a macro call that contains Soft Interrupt commands of the microprocessor. This interrupt is used to implement context switching between tasks. Therefore, OS _task_sw () is an architecture-related macro and has different implementation methods for different hardware systems. This is also a key point for UCOS porting in different hardware architectures.
Because UCOS does not support virtual memory, page Directory switching is not required. A feature of many other real-time multi-task embedded systems is also an important aspect of distinguishing Linux systems.
In 2.6 Linux kernel, a new scheduling mechanism O (1) scheduler is introduced, which can complete process switching within a fixed period of time. If the scheduler determines that the task needs to be switched, the context switching function context_switch () is called to perform context switching. This function calls switch_mm () switch the global directory of the page to install a new address space, and then call switch_to () to switch the specific hardware context.
Summary:
This section mainly introduces task scheduling and algorithms in the multi-task system, and compares context switching in Linux and UCOS systems. For details, refer to the Linux kernel source code and UCOS source code.
Refer:
1. Linux kernel Primer
2. Understanding the Linux Kernel
3. embedded real-time operating system uC/OS-II
4. Embedded Computer System Design Principles