简体   繁体   English

什么是可重入内核

[英]what is a reentrant kernel

什么是可重入内核?

Much simpler answer: 更简单的答案:

Kernel Re-Entrance 内核重入

If the kernel is not re-entrant, a process can only be suspended while it is in user mode. 如果内核不可重入,则只能在用户模式下暂停进程。 Although it could be suspended in kernel mode, that would still block kernel mode execution on all other processes. 虽然它可以在内核模式下暂停,但仍会阻止所有其他进程上的内核模式执行。 The reason for this is that all kernel threads share the same memory. 原因是所有内核线程共享相同的内存。 If execution would jump between them arbitrarily, corruption might occur. 如果执行会在它们之间任意跳转,则可能会发生损坏。

A re-entrant kernel enables processes (or, to be more precise, their corresponding kernel threads) to give away the CPU while in kernel mode. 重入内核使得进程(或者更确切地说,它们对应的内核线程)能够在内核模式下放弃CPU。 They do not hinder other processes from also entering kernel mode. 它们不会阻碍其他进程进入内核模式。 A typical use case is IO wait. 典型的用例是IO等待。 The process wants to read a file. 该过程想要读取文件。 It calls a kernel function for this. 它为此调用了一个内核函数。 Inside the kernel function, the disk controller is asked for the data. 在内核函数内,磁盘控制器被要求提供数据。 Getting the data will take some time and the function is blocked during that time. 获取数据需要一些时间,并且在此期间功能被阻止。 With a re-entrant kernel, the scheduler will assign the CPU to another process (kernel thread) until an interrupt from the disk controller indicates that the data is available and our thread can be resumed. 使用可重入内核,调度程序将CPU分配给另一个进程(内核线程),直到来自磁盘控制器的中断指示数据可用并且我们的线程可以恢复。 This process can still access IO (which needs kernel functions), like user input. 此进程仍然可以访问IO(需要内核函数),如用户输入。 The system stays responsive and CPU time waste due to IO wait is reduced. 系统保持响应,并且由于IO等待而浪费的CPU时间减少。

This is pretty much standard for today's desktop operating systems. 这是当今桌面操作系统的标准配置。

Kernel pre-emption 内核优先

Kernel pre-emption does not help in the overall throughput of the system. 内核抢占无助于系统的整体吞吐量。 Instead, it seeks for better responsiveness. 相反,它寻求更好的响应能力。

The idea here is that normally kernel functions are only interrupted by hardware causes: Either external interrupts, or IO wait cases, where it voluntarily gives away control to the scheduler. 这里的想法是,通常内核函数只会被硬件原因中断:外部中断或IO等待情况,它会自动放弃对调度程序的控制。 A pre-emptive kernel instead also interrupts and suspends kernel functions just like it would interrupt processes in user mode. 先发制人的内核也可以中断和挂起内核函数,就像在用户模式下中断进程一样。 The system is more responsive, as processes eg handling mouse input, are woken up even while heavy work is done inside the kernel. 系统响应更快,因为即使在内核内部进行大量工作时,也会唤醒进程,例如处理鼠标输入。

Pre-emption on kernel level makes things harder for the kernel developer: The kernel function cannot be suspended only voluntarily or by interrupt handlers (which are somewhat a controlled environment), but also by any other process due to the scheduler. 内核级别的抢占使得内核开发人员更难:内核函数不能只是自愿挂起,也不能由中断处理程序(在某种程度上是受控环境)暂停,而是由于调度程序而导致的任何其他进程。 Care has to be taken to eg avoid deadlocks: A thread locks resource A but needing resource B is interrupted by another thread which locks resource B, but then needs resource A. 必须注意避免死锁:线程锁定资源A但是需要资源B被另一个锁定资源B的线程中断,但是需要资源A.

Take my explanation of pre-emption with a grain of salt. 用一粒盐解释我的先发制人。 I'm happy for any corrections. 我很高兴任何更正。

All Unix kernels are reentrant. 所有Unix内核都是可重入的。 This means that several processes may be executing in Kernel Mode at the same time. 这意味着可以在内核模式下同时执行多个进程。 Of course, on uniprocessor systems, only one process can progress, but many can be blocked in Kernel Mode when waiting for the CPU or the completion of some I/O operation. 当然,在单处理器系统上,只有一个进程可以进行,但是在等待CPU或完成某些I / O操作时,许多进程可以在内核模式下被阻塞。 For instance, after issuing a read to a disk on behalf of a process, the kernel lets the disk controller handle it and resumes executing other processes. 例如,在代表进程向磁盘发出读取之后,内核让磁盘控制器处理它并继续执行其他进程。 An interrupt notifies the kernel when the device has satisfied the read, so the former process can resume the execution. 当设备满足读取时,中断通知内核,因此前一个进程可以恢复执行。

One way to provide reentrancy is to write functions so that they modify only local variables and do not alter global data structures. 提供重入的一种方法是编写函数,以便它们只修改局部变量而不改变全局数据结构。 Such functions are called reentrant functions . 这些函数称为可重入函数。 But a reentrant kernel is not limited only to such reentrant functions (although that is how some real-time kernels are implemented). 但是,可重入内核并不仅限于这种可重入函数(尽管这是实现某些实时内核的方式)。 Instead, the kernel can include nonreentrant functions and use locking mechanisms to ensure that only one process can execute a nonreentrant function at a time. 相反,内核可以包含非重量函数并使用锁定机制来确保一次只能有一个进程执行非重入函数。

If a hardware interrupt occurs, a reentrant kernel is able to suspend the current running process even if that process is in Kernel Mode. 如果发生硬件中断,即使该进程处于内核模式,重入内核也能够暂停当前正在运行的进程。 This capability is very important, because it improves the throughput of the device controllers that issue interrupts. 此功能非常重要,因为它可以提高发出中断的设备控制器的吞吐量。 Once a device has issued an interrupt, it waits until the CPU acknowledges it. 一旦设备发出中断,它就会等待CPU确认。 If the kernel is able to answer quickly, the device controller will be able to perform other tasks while the CPU handles the interrupt. 如果内核能够快速应答,则设备控制器将能够在CPU处理中断时执行其他任务。

Now let's look at kernel reentrancy and its impact on the organization of the kernel. 现在让我们看一下内核重入及其对内核组织的影响。 A kernel control path denotes the sequence of instructions executed by the kernel to handle a system call, an exception, or an interrupt. 内核控制路径表示内核为处理系统调用,异常或中断而执行的指令序列。

In the simplest case, the CPU executes a kernel control path sequentially from the first instruction to the last. 在最简单的情况下,CPU从第一条指令到最后一条指令顺序执行内核控制路径。 When one of the following events occurs, however, the CPU interleaves the kernel control paths : 但是,当发生以下事件之一时,CPU会交错内核控制路径:

A process executing in User Mode invokes a system call, and the corresponding kernel control path verifies that the request cannot be satisfied immediately; 在用户模式下执行的进程调用系统调用,相应的内核控制路径验证不能立即满足请求; it then invokes the scheduler to select a new process to run. 然后它调用调度程序来选择要运行的新进程。 As a result, a process switch occurs. 结果,发生了过程切换。 The first kernel control path is left unfinished, and the CPU resumes the execution of some other kernel control path. 第一个内核控制路径未完成,CPU恢复执行其他一些内核控制路径。 In this case, the two control paths are executed on behalf of two different processes. 在这种情况下,两个控制路径代表两个不同的进程执行。

The CPU detects an exception-for example, access to a page not present in RAM-while running a kernel control path. CPU在运行内核控制路径时检测异常 - 例如,访问RAM中不存在的页面。 The first control path is suspended, and the CPU starts the execution of a suitable procedure. 第一个控制路径被挂起,CPU开始执行合适的过程。 In our example, this type of procedure can allocate a new page for the process and read its contents from disk. 在我们的示例中,此类过程可以为进程分配新页面并从磁盘读取其内容。 When the procedure terminates, the first control path can be resumed. 当过程终止时,可以恢复第一个控制路径。 In this case, the two control paths are executed on behalf of the same process. 在这种情况下,两个控制路径代表相同的过程执行。

A hardware interrupt occurs while the CPU is running a kernel control path with the interrupts enabled. CPU正在运行启用了中断的内核控制路径时发生硬件中断。 The first kernel control path is left unfinished, and the CPU starts processing another kernel control path to handle the interrupt. 第一个内核控制路径未完成,CPU开始处理另一个内核控制路径来处理中断。 The first kernel control path resumes when the interrupt handler terminates. 中断处理程序终止时,第一个内核控制路径恢复。 In this case, the two kernel control paths run in the execution context of the same process, and the total system CPU time is accounted to it. 在这种情况下,两个内核控制路径在同一进程的执行上下文中运行,并且计算总系统CPU时间。 However, the interrupt handler doesn't necessarily operate on behalf of the process. 但是,中断处理程序不一定代表进程运行。

An interrupt occurs while the CPU is running with kernel preemption enabled, and a higher priority process is runnable. CPU在运行并启用内核抢占时发生中断,并且可以运行更高优先级的进程。 In this case, the first kernel control path is left unfinished, and the CPU resumes executing another kernel control path on behalf of the higher priority process. 在这种情况下,第一个内核控制路径未完成,并且CPU代表优先级较高的进程继续执行另一个内核控制路径。 This occurs only if the kernel has been compiled with kernel preemption support. 只有在使用内核抢占支持编译内核时才会发生这种情况。

These information available on http://jno.glas.net/data/prog_books/lin_kern_2.6/0596005652/understandlk-CHP-1-SECT-6.html 这些信息可在http://jno.glas.net/data/prog_books/lin_kern_2.6/0596005652/understandlk-CHP-1-SECT-6.html上找到

More On http://linux.omnipotent.net/article.php?article_id=12496&page=-1 更多关于http://linux.omnipotent.net/article.php?article_id=12496&page=-1

The kernel is the core part of an operating system that interfaces directly with the hardware and schedules processes to run. 内核是操作系统的核心部分,它直接与硬件连接并调度要运行的进程。

Processes call kernel functions to perform tasks such as accessing hardware or starting new processes. 进程调用内核函数来执行访问硬件或启动新进程等任务。 For certain periods of time, therefore, a process will be executing kernel code. 因此,在某些时间段内,进程将执行内核代码。 A kernel is called reentrant if more than one process can be executing kernel code at the same time. 如果多个进程可以同时执行内核代码,则内核称为可重入 "At the same time" can mean either that two processes are actually executing kernel code concurrently (on a multiprocessor system) or that one process has been interrupted while it is executing kernel code (because it is waiting for hardware to respond, for instance) and that another process that has been scheduled to run has also called into the kernel. “同时”可能意味着两个进程实际上同时执行内核代码(在多处理器系统上)或者一个进程在执行内核代码时被中断(因为它等待硬件响应,例如)并且计划运行的另一个进程也调用了内核。

A reentrant kernel provides better performance because there is no contention for the kernel. 可重入内核提供更好的性能,因为内核没有争用 A kernel that is not reentrant needs to use a lock to make sure that no two processes are executing kernel code at the same time. 不可重入的内核需要使用来确保没有两个进程同时执行内核代码。

A reentrant function is one that can be used by more than one task concurrently without fear of data corruption. 可重入函数是可以由多个任务同时使用而不用担心数据损坏的函数。 Conversely, a non-reentrant function is one that cannot be shared by more than one task unless mutual exclusion to the function is ensured either by using a semaphore or by disabling interrupts during critical sections of code. 相反,非重入函数是一个不能由多个任务共享的函数,除非通过使用信号量或在代码的关键部分禁用中断来确保对函数的互斥。 A reentrant function can be interrupted at any time and resumed at a later time without loss of data. 可以在任何时间中断可重入函数,并在以后恢复,而不会丢失数据。 Reentrant functions either use local variables or protect their data when global variables are used. 可重入函数使用局部变量或在使用全局变量时保护其数据。

A reentrant function: 可重入函数:

Does not hold static data over successive calls
Does not return a pointer to static data; all data is provided by the caller of the function
Uses local data or ensures protection of global data by making a local copy of it
Must not call any non-reentrant functions

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM