linux system context switch -CPU

linux is a multi-tasking operating system able to run multiple applications at the same time, the number of far more than the cup nuclear program book, in fact, not run, but turns cpu resources allocated to the implementation of these programs.

cpu registers and program counter

  cpu cpu registers are built in small-capacity memory, but fast. The program counter is used to store the location cpu command is being executed, or the location of the next instruction to be executed. They are cpu before running any task,

  We must rely on the environment, that is, we are talking about cpu context.

CPU context switching

  cpu context switch is a first front cpu context saved task, and then loading the new task context to the register and the program counter, the new position of the program counter jumps to the last referred to, new task.

  Here that the task is a process or thread (of course, with hardware communication tasks). These preserved context, will be stored in the system kernel, and load come in again when the task is re-scheduled for execution,

  This would ensure that the original task of the state is not affected, so the task still looks continuous operation.

Process context switch

  linux will follow the privilege level, the process of running space into kernel space and user space. Referring the FIG.

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

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

                                

   Can be said that the process can run in user space, it can also run in kernel space. Process running in user space, the process is called user mode, and kernel space into time, known as the kernel mode processes.

   Transition from user mode to kernel mode, is required by the system call to be completed. For example, when we view the contents of the file, you need to repeatedly open system call is completed.

     1. Call the open () to open the file

     2. Call read () reads the contents of the file, and call write () to write the contents of the standard output

     3. Invoke close () closes the file

   At this time the CPU context switch occurs, the original command position cpu registers the user mode, it needs to be saved. Then the code to perform a kernel mode, cpu registers need to be updated to the new location of kernel mode instruction.

   And finally jump to the kernel mode running kernel tasks. After the system call, cpu registers need to restore the original state saved by the user, and then switch to the user space, continue the process. A system call occurs

   Two CPU context switch. But during the call system, and does not involve virtual memory resources processes such as user mode, it will not switch routine. We usually say that with the process context switch is not the same.

   Process context switch, refers to switching from one process to another process to run. The system calls the process has been running in the same process. System call process is commonly called privileged mode switching, rather than context switching.

Process context switch calls with the difference between systems

   Is managed by the kernel process scheduling, switching process can only take place in kernel mode, so the context of the process include resource user space virtual memory, stack, global variables, and so not only, also includes the kernel stack,

   Status registers kernel.

   Therefore, on-line text-step process of switching more than a system call: Before you save the current state of the kernel process and cpu registers, we need to first survive the process virtual memory, stack, etc.;

   After loading the kernel mode and the next process, also you need to refresh the user stack and virtual memory processes. 

 

                                  

 

When will the process is scheduled to run on the CPU

   1. In order to ensure that all fair scheduling processes can be obtained, cpu time is divided into time slices of a section, each process is alternately assigned to the time slice. Thus, when a process time slice is exhausted, the system will be suspended.

     Switch to another is waiting for the process to run cpu.

   2. Insufficient system resources in the process (such as insufficient memory), to wait for resources to meet before they can run, this time the process will be suspended by the system scheduling other processes running.

   3. When the process) when such a method would hang on their own initiative, will naturally be rescheduled by sleep (.

   4. When a higher priority process is running, in order to ensure the operation of high-priority process, the current process is suspended, there is a high-priority process to run.

   5. When a hardware interrupt occurs, the process will be interrupted on the cpu suspended in favor of the implementation of the kernel interrupt service routine.

Thread context switching

   The biggest difference between threads and processes switches that thread is the basic unit of scheduling, and the process is the basic unit of resource owners. To put it plainly, the so-called task scheduling core, in fact, the object of scheduling is the thread;

   The process only to the thread provides virtual memory resources, global variables, and so on. Therefore, the corresponding threads and processes, so we can understand:

    When the process has only one thread, the process will be equal to the task threads.

    When the process has multiple threads that share the same virtual memory and global variables and other resources. These resources when a context switch is not required to modify.

    Further thread has its own private data, such as stack and registers, which also need to be saved during a context switch.

   Thread context switches may be divided into two types:

    First, before and after the two threads belong to different processes, this time, because the resources are not shared, so the switching process just the same process context switch.

    The second, before and after the two threads belong to the same process, this time, because virtual memory is shared, so when switching, virtual memory, these resources remain intact, the word need to switch private data threads, registers, etc.

    Do not share data.

Interrupt context switching

  In order to respond quickly to events hardware , scheduling and interrupt the normal execution process will interrupt the process in favor of calling an interrupt handler, event response equipment. And when interrupted by other processes, it is necessary to save the current state of the process down,

  At the end of this interruption, the process can still recover from the original run state.

  With different process context, interrupt context switching process does not involve user mode. Therefore, even if the process is interrupted by a user mode, you do not need to save and restore the process's virtual memory, global user variables

  State resources. Interrupt message on the line, in fact, comprises only the kernel mode interrupt service routine must state, including cpu registers, the kernel stack, a hardware interrupt parameters.

  On a same cpu, the interrupt handler has a higher priority than the process , so do not interrupt context switching and process context switch occurs simultaneously. Similarly, due to the interruption it will interrupt the normal process of scheduling and execution,

  So most of the interrupt handler is short and sweet, as quickly as possible in order to end the execution.

  With the same process context switch, interrupt context switching also need to consume cpu resources, the switching frequency can lead to too much cpu resource consumption. Even seriously degrade overall system performance.

to sum up

  cpu context switch, is to ensure that one of the core functions of a working linux system, we do not need to pay special attention under normal circumstances. However, excessive context switching, will cpu time spent in the register, and a virtual kernel stack memory

  And so on to save and restore data in order to shorten the process of running real time, resulting in a significant decline in overall system performance.

    

Guess you like

Origin www.cnblogs.com/aaronzheng/p/11545019.html