Linux Context , Interrupts 和 Context Switching 說明【轉】
轉自:http://blog.csdn.net/tianlesoftware/article/details/6461207
一. 進程Context 定義
當一個進程在執行時, CPU的所有寄存器中的值、進程的狀態以及堆棧中的內容,比如各個變量和數據,包括所有的寄存器變量、進程打開的文件、內存信息等。這些信息被稱為該進程的上下文(Context).
一個進程的Context可以分為三個部分:用戶級上下文、寄存器上下文以及系統級上下文:
(1)用戶級上下文: 正文、數據、用戶堆棧以及共享存儲區;
(2)寄存器上下文: 通用寄存器、程序寄存器(IP)、處理器狀態寄存器(EFLAGS)、棧指針(ESP);
當內核需要切換到另一個進程時,它需要保存當前進程的所有狀態,即保存當前進程的上下文,以便在再次執行該進程時,能夠必得到切換時的狀態執行下去。在Linux中,當前進程上下文均保存在進程的任務數據結構中。在發生中斷時,內核就在被中斷進程的上下文中,在內核態下執行中斷服務例程。但同時會保留所有需要用到的資源,以便中繼服務結束時能恢復被中斷進程 的執行。
在linux 的Vmstat 命令中的system下,有interrupts 和 context switching 選項。
[[email protected] ~]# vmstat 2
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------
r b swpd free buff cache si so bi bo in cs us sy id wa st
0 0 23524 13660 62952 651628 0 0 6 14 45 34 0 1 99 0 0
1 0 23524 13660 62952 651656 0 0 0 0 93 62 0 0 100 0 0
其中的in 表示interrupts, cs 表示contxt switching。
in: The number of interrupts per second, including the clock.
cs: The number of context switches per second.
Unix vmstat 命令
http://blog.csdn.net/tianlesoftware/archive/2010/04/08/5464408.aspx
二. Interrupts and Context Switching
To drastically simplify how computers work, you could say that computers do nothing more that shuffle bits (i.e. 1s and 0s) around. All computer data is based on these binary digits, which are represented in computers as voltages (5 V for a 1 and 0 V for a 0), and these voltages are physically manipulated through transistors, circuits, etc... When you get into the guts of a computer and start looking at how they work, it seems amazing how many operations it takes to do something simple, like addition or multiplication. Of course, computers have gotten a lot smaller and thus a lot faster, to the point where they can perform millions of these operations per second, so it still feels fast. The processor is performing these operations in a serial fashion - basically a single-file line of operations.
1.1 what is Context switching
This single-file line could be quite inefficent and there are times when you want a computer to be processing many different things at once, rather than one thing at a time. For example, most computers rely on peripherals for input, but those peripherals are often much slower than the processor itself. For instance, when a program needs some data, it may have to read that data from the hard drive first. This may only take a few milliseconds, but the CPU would be idle during that time - quite inefficient. To improve efficiency, computers use multitasking. A CPU can still only be running one process at a time, but multitasking gets around that by scheduling which tasks will be running at any given time. The act of switching from one task to another is called Context Switching.
--CPU 一次只能運行一個進程,在multitasking 下, 當CPU 處理的進行需要從硬盤讀取一些數據時,雖然這個時間也是按miliseconds來計算,但是這個時間還是要比CPU 運算的時間長,所以在CPU 的這個等待的時間裏,可以將當前處理的task 切換出去,讓CPU 去處理其他的task。 這個過程就叫作Context switch。
Ironically, the act of context switching adds a fair amount of overhead to the computing process. To ensure that the original running program does not lose all its progress, the computer must first save the current state of the CPU in memory before switching to the new program. Later, when switching back to the original, the computer must load the state of the CPU from memory. Fortunately, this overhead is often offset by the efficiency gained with frequent context switches.
--為了保證之前的程序沒有丟失它的進程, 電腦必須先在內存中保存當前CPU的狀態,然後才能進行task switch。
當要切換到之前的task時, 將之前保存在內存中的cpu狀態裝載到CPU就可以了。
1.2 Context switch: steps
In a switch, the state of the first process must be saved somehow, so that, when the scheduler gets back to the execution of the first process, it can restore this state and continue.
The state of the process includes all the registers that the process may be using, especially the program counter, plus any other operating system specific data that may be necessary. This data is usually stored in a data structure called a process control block (PCB), or switchframe.
-- process 中的信息保存在一個叫PCB的數據結構中。
In order to switch processes, the PCB for the first process must be created and saved. The PCBs are sometimes stored upon a per-process stack in kernel memory (as opposed to the user-mode stack), or there may be some specific operating system defined data structure for this information.
--在進行switch processes時,必須先創建當前運行process的PCB 並將該PCB 保存到kernel memory中。
Since the operating system has effectively suspended the execution of the first process, it can now load the PCB and context of the second process. In doing so, the program counter from the PCB is loaded, and thus execution can continue in the new process. New processes are chosen from a queue or queues. Process and thread priority can influence which process continues execution, with processes of the highest priority checked first for ready threads to execute.
-- 將根據進程的優先級從隊列中選擇裝載哪個進程去運行。
1.3 What is Interrupt?
If you can do context switches frequently enough, the computer appears to be doing many things at once (even though the CPU is only processing a single task at any given time). Signaling the CPU to do a context switch is often accomplished with the use of a command called an Interrupt. For the most part, the computers we‘re all using are Interrupt driven, meaning that running processes are often interrupted by higher-priority requests, forcing context switches.
-- 雖然CPU 一次只能處理一個進程,但只要context switch 足夠頻繁,我們還是可以感覺CPU 同時在處理多個tasks. 而CPU 根據指令去進行context switch 的過程就叫做Interrupt.
Interrupt 可以是有高優先級的進程申請,或者是強制的context switches。
From:http://kaedrin.com/weblog/archive/001657.html
三. Interrupt Context 與 Process Context
處理器總處於以下三種狀態之一:
1.內核態,運行於Process context,內核代表進程運行於內核空間;
2.內核態,運行於Interrupt Context,內核代表硬件運行於內核空間;
3.用戶態,運行於用戶空間。
用戶空間的應用程序,通過系統調用,進入內核空間。這個時候用戶空間的進程要傳遞很多變量、參數的值給內核,內核態運行的時候也要保存用戶進程的一些寄存 器值、變量等。
進程上下文: 可以看作是用戶進程傳遞給內核的這些參數以及內核要保存的那一整套的變量和寄存器值和當時的環境等。
硬件通過觸發信號,導致內核調用中斷處理程序,進入內核空間。這個過程中,硬件的一些變量和參數也要傳遞給內核,內核通過這些參數進行中斷處理。 中斷上下文: 可以看作就是硬件傳遞過來的這些參數和內核需要保存的一些其他環境(主要是當前被打斷執行的進程環境)。
3.1 Interrupt Context
When executing an interrupt handler or bottom half, the kernel is in interrupt context. Recall that process context is the mode of operation the kernel is in while it is executing on behalf of a process -- for example, executing a system call or running a kernel thread. In process context, the current macro points to the associated task. Furthermore, because a process is coupled to the kernel in process context(因為進程是以進程上文的形式連接到內核中的), process context can sleep or otherwise invoke the scheduler.
Interrupt context, on the other hand, is not associated with a process. The current macro is not relevant (although it points to the interrupted process). Without a backing process(由於沒有進程的背景), interrupt context cannot sleep -- how would it ever reschedule?(否則怎麽再對它重新調度?) Therefore, you cannot call certain functions from interrupt context. If a function sleeps, you cannot use it from your interrupt handler -- this limits the functions that one can call from an interrupt handler.(這是對什麽樣的函數可以在中斷處理程序中使用的限制)
Interrupt context is time critical because the interrupt handler interrupts other code. Code should be quick and simple. Busy looping is discouraged. This is a very important point; always keep in mind that your interrupt handler has interrupted other code(possibly even another interrupt handler on a different line!). Because of this asynchronous nature, it is imperative(必須) that all interrupt handlers be as quick and as simple as possible. As much as possible, work should be pushed out from the interrupt handler and performed in a bottom half, which runs at a more convenient time.
The setup of an interrupt handler‘s stacks is a configuration option. Historically, interrupt handlers did not receive their own stacks. Instead, they would share the stack of the process that they interrupted[1]. The kernel stack is two pages in size; typically, that is 8KB on 32-bit architectures and 16KB on 64-bit architectures. Because in this setup interrupt handlers share the stack, they must be exceptionally frugal(必須非常節省) with what data they allocate there. Of course, the kernel stack is limited to begin with, so all kernel code should be cautious.
[1] A process is always running. When nothing else is schedulable, the idle task runs.
Early in the 2.6 kernel process, an option was added to reduce the stack size from two pages down to one, providing only a 4KB stack on 32-bit systems. This reduced memory pressure because every process on the system previously needed two pages of nonswappable kernel memory. To cope with(應對) the reduced stack size, interrupt handlers were given their own stack, one stack per processor, one page in size. This stack is referred to as the interrupt stack(這個棧就程為中斷棧). Although the total size of the interrupt stack is half that of the original shared stack, the average stack space available is greater because interrupt handlers get the full page of memory to themselves.
Your interrupt handler should not care what stack setup is in use or what the size of the kernel stack is. Always use an absolute minimum amount of stack space.
3.2 Process Context
One of the most important parts of a process is the executing program code. This code is read in from an executable file and executed within the program‘s address space. Normal program execution occurs in user-space. When a program executes a system call or triggers an exception, it enters kernel-space. At this point, the kernel is said to be "executing on behalf of the process" and is in process context. When in process context, the current macro is valid[7]. Upon exiting the kernel, the process resumes execution in user-space, unless a higher-priority process has become runnable in the interim(過渡期), in which case the scheduler is invoked to select the higher priority process.
[7] Other than process context there is interrupt context, In interrupt context, the system is not running on behalf of a process, but is executing an interrupt handler. There is no process tied to interrupt handlers and consequently no process context.
System calls and exception handlers are well-defined interfaces into the kernel. A process can begin executing in kernel-space only through one of these interfaces -- all access to the kernel is through these interfaces.
3.3 Process Context 和 Interrupt Context 區別
1. The main difference between process context and interrupt context is that process context can block, while interrupt context can’t.
2.Interrupt context mean that there will not be scheduling taking place until and unless that task ends while in the process context every task gets the CPU time
整理自網絡
-------------------------------------------------------------------------------------------------------
Blog: http://blog.csdn.NET/tianlesoftware
Email: [email protected]
DBA1 群:62697716(滿); DBA2 群:62697977(滿) DBA3 群:62697850(滿)
DBA 超級群:63306533(滿); DBA4 群: 83829929 DBA5群: 142216823
DBA6 群:158654907 聊天 群:40132017 聊天2群:69087192
--加群需要在備註說明Oracle表空間和數據文件的關系,否則拒絕申請
Linux Context , Interrupts 和 Context Switching 說明【轉】