系统调用是否涉及上下文开关?

发布于 2025-02-08 12:50:59 字数 282 浏览 1 评论 0原文

我正在阅读系统呼叫上的Wikipedia页面,但我无法调和一些在那里做的陈述。

在底部,它说:“系统调用通常不需要上下文切换到另一个过程;而是在调用该过程的上下文中执行。”

但是,在顶部,它说“ [...]通过系统调用请求服务的应用程序通常是通过中断引发的。中断[...]将控制传递给内核[然后],然后内核执行了一个调用程序没有直接控制的特定指令集”。

在我看来,如果中断“传递到内核”,那意味着内核是“另一个过程”,正在执行,因此发生上下文开关。因此,Wikipedia页面似乎存在矛盾。我的理解在哪里错?

I am reading the wikipedia page on system calls and I cannot reconcile a few of the statements that are made there.

At the bottom, it says that "A system call does not generally require a context switch to another process; instead, it is executed in the context of whichever process invoked it."

Yet, at the top, it says that "[...] applications to request services via system calls, which are often initiated via interrupts. An interrupt [...] passes control to the kernel [and then] the kernel executes a specific set of instructions over which the calling program has no direct control".

It seems to me that if the interrupt "passes control to the kernel," that means that the kernel, which is "another process," is executing and therefore a context switch happened. Therefore, there seems to be a contradiction in the wikipedia page. Where is my understanding wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

上课铃就是安魂曲 2025-02-15 12:51:00

您的理解是错误的,因为内核不是一个单独的过程。内核坐在共享内存区域的RAM中。通常,它位于虚拟地址空间的上半部分。

当使用系统调用调用内核时,它不一定使用中断。在X86-64上,它可以使用特定的处理器指令(syscall)直接调用。该指令使处理器跳到了特殊寄存器中存储的地址。

Syscalls不一定涉及完整的上下文开关。它们必须涉及用户模式到内核模式上下文开关。大多数情况下,内核每个过程都有内核堆栈。当没有系统调用活跃时,此堆栈大多是未使用的,并且空无一人,因为它在其中存储任何东西都没有意义。

由于内核可以使用它们,还需要保存寄存器。我不知道其他处理器,但是X86-64确实具有TSS允许自动化用户模式到内核模式堆栈开关。仍需要手动保存寄存器。

最后,在通过系统调用输入内核时,实际上有一个必要的部分上下文开关,但这并不涉及切换整个过程。由于已经保留了交换寄存器和内核堆栈的临时存储空间,因此由于内核不需要触摸页面表,因此涉及的开销要少得多。交换页面表通常涉及缓存管理和一些缓存冲洗,以使其保持一致。

Your understanding is wrong because the kernel isn't a separate process. The kernel is sitting in RAM in shared memory areas. Typically, it sits in the top half of the virtual address space.

When the kernel is invoked with a system call, it is not necessarily using an interrupt. On x86-64, it is invoked directly using a specific processor instruction (syscall). This instruction makes the processor jump to the address stored in a special register.

Syscalls don't necessarily involve a full context switch. They must involve a user mode to kernel mode context switch. Most often, kernels have a kernel stack per process. This stack is mostly unused and empty when no system call is active as it then makes no sense to have anything stored in it.

The registers also need to be saved since the kernel can use them. I don't know for other processors but x86-64 does have the TSS allowing for automated user mode to kernel mode stack switch. The registers still need to be saved manually.

In the end, there is actually a necessary partial context switch when entering the kernel through a system call but it doesn't involve switching the whole process. Since the temporary storage for swapped registers and the kernel stack are already reserved, it involves much less overhead as the kernel doesn't need to touch the page tables. Swapping page tables often involves cache managing and some cache flushing to make it consistent.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文