Context switches, CPU context description

1. What is the context?

Linux is a multitasking operating system, it supports much larger than the number of CPU tasks to run at the same time, of course, these tasks are not actually in the run, but the system in a very short period of time, they will take turns to allocate CPU , caused to the user the illusion of many tasks running simultaneously.

Before each task runs, CPU needs to know where to load the task, and from where to start running. In other words, we need to set up the system in advance to give him the CPU registers and program counter (Program Counter, PC)

  • CPU registers: CPU built-in capacity is small, but fast memory

  • Program Counter: the position command is used to store the CPU is executed, or the next instruction to be executed position

In summary, we have the answer

What is the context:

We usually say that the context is also called CPU context CPU is running before any task, we must rely on the environment, including CPU registers and program counter

Context switching: is a first save CPU context before the task (i.e., CPU registers and program counter) together, and then load the new task context to these registers and program counter, and finally jump to a new location within the meaning of the program counter, run new task.

2. Details of context switching

Depending on the switch to run the task CPU, it can be divided into 进程上下文切换, 线程上下文切换,中断上下文切换

We first understand the knowledge points below two context switches related to system calls, processes running state

The process of running state:

Linux accordance with privilege level, the process of running into space 内核空间and 用户空间. State process running in both spaces are called 内核态and 用户态.

  • Kernel space (Ring 0): has the highest authority, direct access to all the resources (read the file,)

    • Allocate memory, IO operation, create a child process ...... all kernel operations. This also shows that, when frequent IO operations, System parameters will be high.

  • User space (Ring 3): can only access limited resources, can not directly access memory and other hardware devices, you must enter the kernel through system calls, you can visit these privileged resources

    • A typical user mode space program has: Shells, database, web server, PHP program, Java program ......

When linux system cpu top command to view, and the user can see the two system corresponding cpu is user mode and kernel mode resource occupied

As above, our web service is running 用户态under no rights to the io file, when you need to read a file, it involves the system calls the

System call:

Transition from user mode to kernel mode, it needs to be done by the system call. For example, when viewing the file, you need to perform multiple system calls: open, read, write, close and so on. Process system call is as follows:

  • The saved original instruction position in the CPU registers the user state;

  • To perform kernel code, the CPU registers need to be updated to the new location of kernel mode instruction, the last hop to the kernel state operation kernel tasks;

  • After the system call, CPU registers need to restore previously saved user mode, and then switch to the user space, continue to run the process;

So, once the process of system calls, in fact, it is twice the CPU context switch occurs.

Process context switch?

  • Process execution terminates before it Yours CPU will be released, then wait for a process time slice is removed from the ready queue;

  • When a process time slice is exhausted, it will be suspending the system to switch to other processes waiting for the CPU to run;

  • A process because of the need relatively large system resources (such as memory shortage), this time the process will be suspended, the system will schedule other processes to perform;

  • When there is a higher priority process (process of system operation) takes time slice, in order to ensure a higher priority process can be executed, the current process is suspended;

  • If the current process has a sleep function, he will be suspended;

Thread context switches?

On the operating system, the thread is the smallest unit of execution, the process is the smallest unit of resource management . To put it plainly, the so-called core task calls, in fact, the object of scheduling is the thread; and the process only to the thread provides virtual memory resources, global variables, and so on. So, for the site and the process, so we can understand:

  • When the process has only one thread, the process can be considered equivalent to threads.

  • When the process has multiple threads that share the resources of the parent process (that is, share the same virtual memory resources and global variables, etc.). These resources when a context switch is not required to modify.

  • Further, the thread has its own private data, such as stack and registers, etc., these context switches also need to be saved.

In summary, a thread context switch, there are two cases:

  • Before and after the two threads belong to different processes, because the resources are not shared, so just switching process is the same process context switching;

  • Before and after the two threads belong to the same process, because virtual memory is shared, so when switching, virtual memory, these resources remain intact, only need to switch the thread private data, registers, etc. do not share data.

Interrupt context switching?

Interrupting the normal scheduling and execution process will interrupt the process. When interrupted by other processes, the need to preserve the current state of the process, after the interruption, the process can still recover from the original run state.

Interrupt context switching process does not involve user mode. So, even interrupting the process interrupted by a user is in the process state does not need to save and restore the process of user mode virtual memory resources, global variables, and so on. Interrupt context, in fact, only the kernel mode includes a state necessary to execute the interrupt service routine, including CPU registers, the kernel stack, a hardware interrupt parameters.

 

summary

According to the test report Tsuna, each context switch requires tens of nanoseconds to microseconds of CPU time, this time is still considerable.

No matter what kind of a context switch caused a scene, you should know:

  1. CPU context switching, is one of the core functions of Linux systems to ensure normal work, we do not need to pay special attention under normal circumstances.

  2. But excessive context switching, will consume CPU time on saving and restoring registers, the kernel stack and virtual memory and other data, thus shortening the time course actually runs, leading to a substantial decline in the overall performance of the system.

Guess you like

Origin www.cnblogs.com/JaminXie/p/10984379.html