系统调用和上下文切换

发布于 2025-01-04 18:03:29 字数 457 浏览 2 评论 0原文

我很抱歉在已经有人问过这个问题的情况下问这个问题,但我无法从他们那里得到澄清。所以我问以下相关问题来了解系统调用(模式切换)和上下文切换之间的区别

  • 为什么说当上下文切换时系统调用不需要上下文切换 必须保存进行调用的过程,然后重新加载。难道只是因为根据上下文切换的定义,必须要切换到另一个进程吗?

  • 当进行系统调用时,内核在“用户上下文”中执行是什么意思?

  • 根据维基百科文章:http://en.wikipedia.org/wiki/Context_switch

系统调用不需要上下文切换,但它取决于操作系统,并且在系统调用期间可能会发生上下文切换。我想知道当系统调用时发生上下文切换时会发生什么。有什么例子吗?

I am sorry to ask this question when it has already been asked but I couldn't get a clarity from them. So I am asking the following related questions to get the difference between system call (mode-switch) and context switch

  • Why is it said that the system call doesn't require context switch when the context of the
    process making the call has to be saved and then reloaded. Is it just because according to the definition of context switch a switch has to be made to another process.

  • What does it mean that when a system call is made the kernel executes in "user context".

  • According to the wikipedia article : http://en.wikipedia.org/wiki/Context_switch

a context switch is not necessary for system call but it depends on the operating system and a context switch might occur during a system call. I am wondering what would happen in the case when the context switch takes place at the time of system call. Any examples?

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

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

发布评论

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

评论(2

守不住的情 2025-01-11 18:03:29

您需要了解线程/进程上下文有多个部分,一个部分与执行直接相关,并保存在 CPU 中以及 CPU 使用的内存中的某些系统表(例如页表)中,另一个部分是执行所需的。操作系统,用于簿记(考虑各种 ID、句柄、操作系统特定的特殊权限、网络连接等)。

完整的上下文切换将涉及交换这两者,旧的当前线程/进程消失一段时间,新的当前线程/进程进入一段时间。这就是线程/进程调度的本质。

现在,系统调用彼此之间有很大不同。

考虑一些简单的事情,例如,请求当前日期和时间的系统调用。 CPU从用户模式切换到内核模式,保留用户模式寄存器值,执行一些内核代码以获取必要的数据,将其存储在调用者可以访问的内存或寄存器中,恢复用户模式寄存器值并返回。这里没有太多的上下文切换,只有模式、用户和内核之间转换所需的内容。

现在考虑一个系统调用,该调用涉及阻塞调用者直到发生某些事件或数据可用。操作互斥体和读取文件就是此类系统调用的示例。在这种情况下,内核被迫保存调用者的完整上下文,将其标记为阻塞,以便调度程序在事件或数据到达之前无法运行它,并加载另一个就绪线程/进程的上下文,以便它可以运行。

这就是系统调用与上下文切换的关系。

内核在用户或进程的上下文中执行意味着每当内核代表某个进程或用户工作时,它都必须考虑该用户/进程的上下文,例如当前进程/线程/用户 ID、当前目录、区域设置、各种资源(例如文件)的访问权限,所有这些东西,在不同的进程/线程/用户之间可能会有所不同。

如果进程具有单独的地址空间,则地址空间也是进程上下文的一部分。因此,当内核需要访问进程的内存(以读/写文件数据或网络数据包)时,它必须有权访问进程的地址空间,IOW,它必须位于其上下文中(它不然而,这意味着内核必须加载完整的上下文才能访问特定地址空间中的内存)。

有帮助吗?

You need to understand that a thread/process context has multiple parts, one, directly associated with execution and is held in the CPU and certain system tables in memory that the CPU uses (e.g. page tables), and the other, which is needed for the OS, for bookkeeping (think of the various IDs, handles, special OS-specific permissions, network connections and such).

A full context switch would involve swapping both of these, the old current thread/process goes away for a while and the new current thread/process comes in for a while. That's the essence of thread/process scheduling.

Now, system calls are very different w.r.t. each other.

Consider something simple, for example, the system call for requesting the current date and time. The CPU switches from the user to kernel mode, preserving the user-mode register values, executes some kernel code to get the necessary data, stores it either in the memory or registers that the caller can access, restores the user-mode register values and returns. There's not much of context switch in here, only what's needed for the transition between the modes, user and kernel.

Consider now a system call that involves blocking of the caller until some event or availability of data. Manipulating mutexes and reading files would be examples of such system calls. In this case the kernel is forced to save the full context of the caller, mark it as blocked so the scheduler can't run it until that event or data arrives, and load the context of another ready thread/process, so it can run.

That's how system calls are related to context switches.

Kernel executing in the context of a user or a process means that whenever the kernel does work on behalf of a certain process or user it has to take into consideration that user's/process's context, e.g. the current process/thread/user ID, the current directory, locale, access permissions for various resources (e.g. files), all that stuff, that can be different between different processes/threads/users.

If processes have individual address spaces, the address spaces is also part of the process context. So, when the kernel needs to access memory of a process (to read/write file data or network packets), it has to have access to the process' address space, IOW, it has to be in its context (it doesn't mean, however, that the kernel has to load the full context just to access memory in a specific address space).

Is that helpful?

何必那么矫情 2025-01-11 18:03:29

当用户只想访问仅适合内核模式的内容时,就会发生模式切换

mode switch occurs when a user only wants to access things which are only suitable to a kernel mode

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