抢占和上下文切换的区别 [英] difference between Preemption and context switch

查看:28
本文介绍了抢占和上下文切换的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一点介绍,

我目前正在编写一个小的(读取微小的)RTOS 内核,它应该是内核中的大部分内容的整体.但是,我找不到关于下面列出的一些事情的太多信息,这会很有帮助,除此之外,它实际上不是某种大学项目,而是我自愿做的事情.

I am currently writing a small (read tiny) RTOS kernel, well it's supposed to be monolithic with most stuff in the kernel. However I can't find much information on a few things listed below, It would be a lot helpful and besides this, it isn't actually some kind of university project but something I'm doing at my own will.

回答所有问题的一个更好的选择是,如果你能向我推荐一个免费提供的 RTOS(甚至是一本免费的书),最好是用于 arm 的,它实现了用户空间并且是可抢占的(但并不复杂)像Linux).到目前为止,Linux 有一些我见过的最糟糕的文档(我确实尝试从 linux 代码中找出问题,但只有大量定义分散在一百万个文件和函数挂钩中,名称很奇怪,每个版本的东西都被重命名了有时搬家……)

A better alternative to answering all the questions would be if you could refer to me a freely available RTOS (or even a free book) for arm preferably which implement userspace and are preemptible (but not complex like linux). Linux has some of the worst documentation I've seen till now (I did try figuring things out from linux code but there are just a tons of defines scattered through a million files and function hooks with wierd names and stuff getting renamed every version also getting moved sometimes...)

  1. 抢占"和上下文切换"有什么区别?

  1. What is the difference between "preemption" and "context switch" ?

抢占式和非抢占式内核之间的主要区别是什么?程序员需要做哪些工作才能使内核具有抢占性?

What are the key differences between a preemptive and nonpreemptive kernel ? What all work is required from a programmer to make the kernel preemptive ?

如何创建和使用用户模式?

How to create and work with user mode ?

ARM 文档说在用户模式下,任何切换到特权模式的指令都将被视为未定义指令.

ARM docs say that in user mode, any instruction switching to a privileged mode will be treated as undefined instruction.

如果是这样,用户空间程序使用内核代码的唯一方法是系统调用?

If so, the only way for a userspace program to use kernel code is syscalls ?

内核如何响应或与用户空间交互?

How does a kernel respond or interact with userspace then ?

这是否意味着启动后(在简单系统中)唯一的内核线程将是空闲线程?

Does that mean the only kernel thread after booting (in a simple system) would be the idle thread ?

如果切换到用户进程时内核代码和数据所在的页面没有映射,那么在系统调用或中断时,内核代码如何执行而不映射到虚拟地址空间?

If the Page where kernel code and data resides is unmapped when switching to a user process, then on a syscall or interrupt, how does the kernel code execute without being mapped in the virtual address space ?

可抢占内核"是否仅意味着内核的设计方式是在内核代码执行期间进行上下文切换是安全的?或者是否需要做更多的工作?

Does a 'preemptible kernel' only mean that the kernel was designed in a way that it would be safe to have context switch during execution of kernel code ? or does it require more work to be done if any ?

哦,如果这里不允许出现这样的多个问题,抱歉,找不到任何相关信息.

Oh and if such multiple questions are not allowed here, sorry, couldn't find anything about that.

推荐答案

正如 Mat 所写,这可能是不合理的范围.但是,我会尽量关注问题的总和,就像关注一个合理范围的问题一样,希望这能帮助您开始研究.

As Mat wrote, this is probably unreasonably scoped. However, I'll try to devote as much attention to the sum of the questions as I would to one reasonably scoped question, in the hopes that this will help you to begin researching.

1 抢占"和上下文切换"有什么区别?

1 What is the difference between "preemption" and "context switch" ?

抢占是在不涉及进程的情况下中断进程的行为.在这种情况下,这可能意味着将触发计时器中断.这个词来自抢占的法律概念:主张或购买的行为或权利 就您的目的而言,这意味着当定时器中断触发时,中断服务例程 (ISR) 优先于先前运行的代码.这根本不需要涉及内核;您可以让代码在任何会抢先运行的 ISR 中运行.

Preemption is the act of interrupting a process without its involvement. In this context, that probably means a timer interrupt will fire. The word comes from a legal concept of preemption: the act or right of claiming or purchasing before or in preference to others. For your purposes, that means that when the timer interrupt fires, that the interrupt service routine (ISR) has preference over the code which was previously running. This doesn't necessarily need to involve a kernel at all; you can have code running in any ISR which will run preemptively.

上下文切换是当操作系统代码(抢占式运行)在一个进程或线程的上下文与另一个进程或线程的上下文之间改变处理器状态(寄存器、模式和堆栈)时发生的情况.处理器的状态可能在一个线程中的某一行代码处.它将有寄存器中的临时数据、特定内存区域的堆栈指针以及其他状态信息.抢占式操作系统可以存储此状态(到静态内存或进程的堆栈中)并加载前一个进程的状态.这称为上下文切换.

A context switch is what happens when the OS code (running preemptively) alters the state of the processor (the registers, mode, and stack) between one process or thread's context and another. The state of the processor may be at a certain line of code in a one thread. It will have temporary data in registers, a stack pointer at a certain region of memory, and other state information. A preemptive OS can store this state (either to static memory or onto the processes' stack) and load the state of a previous process. This is known as a context switch.

2 抢占式和非抢占式内核之间的主要区别是什么?程序员需要做哪些工作才能使内核具有抢占性?

2 What are the key differences between a preemptive and nonpreemptive kernel ? What all work is required from a programmer to make the kernel preemptive ?

在抢占式内核中,可以在任意两条汇编指令(称为序列点")之间触发中断.在非抢占式内核中,正在运行的进程必须调用 yield() 函数以允许其他线程运行.抢占式内核更复杂,但提供了更好的并发错觉.非抢占式内核可以通过 setjmp.h 非常简单地完成,但每个线程必须定期调用 yield() 否则其他线程将无法运行.

In a preemptive kernel, an interrupt can fire in between any two assembly instructions (known as 'sequence points'). In a non-preemptive kernel, the running process must call a yield() function to allow the other threads to run. Preemptive kernels are more complex, but provide a better illusion of concurrency. Non-premptive kernels can be done very simply with setjmp.h, but each thread must regularly call yield() or the other threads will not run.

yield() 之类的函数被调用时,处理器的状态会自动存储.当您想让操作系统抢占时,您必须手动存储此信息.

When a function like yield() is called, the state of the processor is stored automatically. When you want to make your OS preemptive, you must store this information manually.

3 如何创建和使用用户模式?

3 How to create and work with user mode ?

ARM 文档说在用户模式下,任何切换到特权模式的指令都将被视为未定义指令.

ARM docs say that in user mode, any instruction switching to a privileged mode will be treated as undefined instruction.

正确.但是,他们也说任何中断都会自动以特权模式运行.在 ARM 系统上,您可以使用 svc 指令来生成软件中断.然后,SVC 代码(您操作系统的一部分)将能够在特权模式下运行.

Correct. However, they also say that any interrupt will run in privileged mode automatically. On an ARM system, you can use the svc instruction to generate a software interrupt. The SVC code (part of your OS) will then be able to run in privileged mode.

4 如果是这样,用户空间程序使用内核代码的唯一方法是系统调用?

4 If so, the only way for a userspace program to use kernel code is syscalls ?

正确.至少,这是唯一安全或正确的方法.

Correct. At least, this is the only safe or correct way.

5 那么内核如何响应用户空间或与用户空间交互?

5 How does a kernel respond or interact with userspace then ?

在 ARM 上,SVC 指令可以获得 8 位值.这可用于生成 256 个系统调用,例如 yield、启用中断、禁用中断或任何您需要的.如果需要,您也可以选择创建共享内存或消息传递交互机制.

On ARM, the SVC instruction can get an 8-bit value. This can be used to generate 256 syscalls such as yield, enable interrupts, disable interrupts, or whatever you need. You may also choose to create a shared memory or message passing interaction mechanism if you need that.

6 这是否意味着启动后(在简单系统中)唯一的内核线程将是空闲线程?

6 Does that mean the only kernel thread after booting (in a simple system) would be the idle thread ?

这完全取决于您如何设计系统.如果您选择仅在所有线程创建后才启动内核,这可能会更简单 - 这样您就无需担心动态分配线程.或者,您可以从空闲线程开始,稍后添加其他线程(通过远程 shell?我认为您希望至少有一个用户线程持续运行...)

That depends entirely on how you design your system. It's probably simpler if you choose to start your kernel only after all your threads have been created - that way you don't need to worry about dynamically allocating threads. Or, you can start with the idle thread and add other threads later (through a remote shell? I think you'd want at least one user thread running consistently...)

7 如果切换到用户进程时内核代码和数据所在的页面没有映射,那么在系统调用或中断时,内核代码如何执行而不映射到虚拟地址空间?

7 If the Page where kernel code and data resides is unmapped when switching to a user process, then on a syscall or interrupt, how does the kernel code execute without being mapped in the virtual address space ?

正如内核模式代码在特权模式下运行,即使代码之前在用户模式下执行,内核模式代码也会从主堆栈指针 (MSP) 运行,即使进程代码使用不同的地址空间.

Just as kernel mode code runs in privileged mode even if the code was previously executing in user mode, so will kernel mode code run from the main stack pointer (MSP) even if the process code was using a different address space.

8 抢占式内核"是否仅意味着内核的设计方式是在内核代码执行期间进行上下文切换是安全的?或者是否需要做更多的工作(如果有)?

8 Does a 'preemptible kernel' only mean that the kernel was designed in a way that it would be safe to have context switch during execution of kernel code ? or does it require more work to be done if any ?

我认为这意味着内核可以抢占用户代码,而不是内核本身可以被抢占.中断内核的任何事情都是困难和不寻常的.这将需要更多的工作,我正在努力了解您为什么想要它.

I think that means that the kernel can preempt the user code, not that the kernel itself can be preempted. It would be difficult and unusual for anything to interrupt the kernel. That would require more work, and I'm struggling to see why you'd want it.

这篇关于抢占和上下文切换的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆