what is context in linux

青灯夜游
Release: 2022-11-10 20:09:31
Original
2000 people have browsed it

In Linux, context is also called CPU context. It is the environment that the CPU must rely on before running any task, including CPU registers and program counters; context switching is to first change the CPU context of the previous task (that is, CPU registers and program counter) are saved, then load the context of the new task into these registers and program counter, and finally jump to the new location pointed to by the program counter to run the new task.

what is context in linux

#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

1. What is context?

Linux is a multi-tasking operating system that supports tasks far larger than the number of CPUs to run at the same time. Of course, these tasks are not actually running at the same time. The system allocates the CPU to them in turn in a short period of time, giving users the illusion that many tasks are running at the same time.

Before each task is run, the CPU needs to know where the task is loaded and where it starts to run. In other words, the system needs to set up the CPU registers and program counter (Program Counter, PC) in advance. But extremely fast memory

  • Program counter: used to store the location of the instruction being executed by the CPU, or the location of the next instruction to be executed

  • To sum up, we have the answer

What is context:

The context we usually talk about is also called the CPU context, which is the environment that the CPU must rely on before running any task, including CPU registers and program counters

Context switching:

That is to first save the CPU context (that is, CPU registers and program counter) of the previous task, then load the context of the new task to these registers and program counter, and finally jump to the new location pointed to by the program counter and run the new task. Task.

2. Detailed introduction of context switching

According to the different CPU switching running tasks, it can be divided into Process context switching,

Thread context switching

,Interrupt context switching# #Let’s first understand the knowledge points involved in the following two context switchesSystem calls, process running state

The running state of the process:Linux divides the running space of the process into

kernel space

and user space

according to the privilege level. The process states running in these two spaces are called kernel state and user state respectively. Kernel space (Ring 0): Has the highest permissions and can directly access all resources (read files ,)

  • Allocating memory, IO operations, creating child processes... are all kernel operations. This also shows that when IO operations are frequent, the System parameters will be very high.

    • User space (Ring 3): can only access restricted resources, cannot directly access hardware devices such as memory, and must make system calls Only by entering the kernel can you access these privileged resources

  • Typical user space programs include: Shells, databases, web servers, PHP programs, Java programs...

    • When you use the top command to view the CPU in the Linux system, you can see two items, user and system, which correspond to user mode and kernel. The cpu resources occupied by the state

  • are as above. Our web service is running in the
user state

. It has no permission for the io of the file. When it needs to read When fetching a file, it involves system call

##System call:

The transition from user mode to kernel mode needs to be completed through system calls. For example, when viewing a file, multiple system calls need to be executed: open, read, write, close, etc. The process of system call is as follows:

  • Save the original user mode instruction location in the CPU register;

  • In order to execute the kernel code, the CPU register needs to be updated to the new location of the kernel mode instruction, and finally jumps to the kernel mode to run the kernel task;

  • After the system call is completed, the CPU registers need to restore the originally saved user state, and then switch to the user space to continue running the process;

#So, a system call The process actually involves two CPU context switches.

Process context switch?

  • #The process execution is terminated, and the CPU it previously used will be released, and the next waiting time slice will be taken out from the ready queue. process;

  • #When the time slice of a process is exhausted, it will be suspended by the system and switch to other processes waiting for the CPU to run;

  • #Because a process requires relatively large system resources (such as insufficient memory), the process will be suspended at this time, and the system will schedule other processes for execution;

  • When a process with a higher priority (system operation process) requires a time slice, in order to ensure that the process with a higher priority can be executed, the current process will be suspended ;

  • If there is a sleep function in the current process, it will also be suspended;

Context switching of threads?

For the operating system, the thread is the smallest execution unit, and the process is the smallest resource management unit. To put it bluntly, the so-called task call in the kernel is actually scheduled by the thread; and the process only provides resources such as virtual memory and global variables to the thread. Therefore, for the scene and the process, we can understand it this way:

  • #When the process has only one thread, the process can be considered to be equal to the thread.

  • #When a process has multiple threads, these threads will share the resources of the parent process (that is, share the same resources such as virtual memory and global variables). These resources do not need to be modified during context switching.

  • #In addition, threads also have their own private data, such as stacks and registers, which also need to be saved during context switching.

To sum up, there are two situations of thread context switching:

  • Two before and after Threads belong to different processes, because resources are not shared, so the switching process is the same as process context switching;

  • The two threads before and after belong to the same process, because Virtual memory is shared, so when switching, resources such as virtual memory remain unchanged. Only the thread's private data, registers and other non-shared data need to be switched.

Interrupt context switch?

#Interrupt handling will interrupt the normal scheduling and execution of the process. When interrupting other processes, the current state of the process needs to be saved. After the interruption is over, the process can still resume running from its original state.

Interrupt context switching does not involve the user mode of the process. Therefore, even if the interrupt process interrupts a process that is in user mode, there is no need to save and restore user mode resources such as virtual memory and global variables of this process. The interrupt context actually only includes the state necessary for the execution of the kernel mode interrupt service program, including CPU registers, kernel stack, hardware interrupt parameters, etc.

Summary

According to Tsuna’s test report, each context switch requires tens of nanoseconds to several microseconds of CPU time. This time Still quite impressive.

No matter which scenario causes context switching, you should know:

  • CPU context switching is It is one of the core functions that ensures the normal operation of the Linux system. Under normal circumstances, we do not need to pay special attention to it.

  • #However, excessive context switching will consume CPU time in saving and restoring data such as registers, kernel stacks, and virtual memory, thus shortening the actual process of the process. The running time causes the overall performance of the system to drop significantly.

Related recommendations: "Linux Video Tutorial"

The above is the detailed content of what is context in linux. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template