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

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

问题描述

有一个小前奏,

我目前正在写一小(读微小)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(甚至是免费的书)为手臂preferably 它们实现用​​户空间,而且preemptible(但并不复杂,如Linux)。 Linux有一些我见过至今最糟糕的文档(我曾尝试从Linux code盘算的事情了,但也有刚刚更名为每一个通过吨一百万个文件散落定义和功能与挂钩奇怪的名字之类的东西版本也越来越有时感动......)

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. 之间有什么preemption和上下文切换有什么区别?

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

什么是preemptive和非preemptive内核之间的主要区别?从程序员需要什么样的一切工作,使内核preemptive?

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.

如果是这样,用户空间程序使用的内核code的唯一途径是系统调用?

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 ?

如果切换时,用户进程,然后在系统调用或中断,其中内核code和数据所在的页面未映射,如何内核code执行,而不在虚拟地址映射空间?

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 ?

做了'preemptible内核仅仅意味着内核设计的方式,这将是安全的内核code的执行过程中有上下文切换?还是需要更多的工作要做,如果有的话?

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.

推荐答案

作为垫写道,这可能是不合理的范围。不过,我会尽量投入尽可能多的关注到的问题的总和我想给一个合理的范围内的问题,在希望这将帮助你开始研究。

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是什么preemption和上下文切换?

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

preemption是打断过程中没有其参与的行为。在这种情况下,这可能意味着一个定时器中断将闪光。这个词来自 preemption 的一个法律概念:行为或声称的权利或购买前或preference给他人。的你的目的,这意味着当定时器中断火灾,该中断服务程序(ISR)拥有超过code这是$ preference p $ pviously运行。这不一定需要涉及在所有内核;你可以有code运行中,将pemptively运行$ P $任何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.

一个上下文切换是OS时code(运行preemptively)一种过程或线程的上下文和另一个之间改变所述处理器的状态(寄存器,模式,和叠层)时会发生什么。处理器的状态可以是在$ C $的c在一个线程一定行。它将具有在寄存器临时数据,在存储器的特定区域一个堆栈指针,和其他状态信息。一个preemptive OS可以存储这种状态下(无论是静态存储器或到过程'栈)和加载previous进程的状态。这被称为上下文切换。

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什么是preemptive和非preemptive内核之间的主要区别?从程序员需要什么样的一切工作,使内核preemptive?

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 ?

在一个preemptive内核,一个中断可以触发任何两个汇编指令(称为序列点)之间。在一个非preemptive内核,运行过程中必须调用收益率()功能,允许其他线程运行。 preemptive内核是比较复杂的,但提供的并发性更好的错觉。非premptive内核可以非常简单地使用 SETJMP.H 做,但每个线程都必须定期调用收益率()或其他线程将无法运行。

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.

当像函数产量()被调用时,处理器的状态被自动存储。如果你想使你的操作系统preemptive,您必须手动存储这些信息。

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 code(你的操作系统的一部分)将可以在特权模式下运行。

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如果是这样,对于一个用户空间程序使用的内核code的唯一方式是系统调用?

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系统调用,如产量,能中断时,禁止中断,或任何你需要的。您也可以选择创建一个共享内存或消息传递相互作用的机理,如果你需要的。

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。如果在那里内核code和数据切换到用户进程时,然后在系统调用或中断,如何内核code执行,而不在虚拟地址空间被映射所在是映射的页面?

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 ?

正如内核模式code在特权模式下运行,即使在code是$ P $在用户模式下pviously执行,所以将内核模式code运行从主堆栈指针(MSP)连如果过程code,采用不同的地址空间。

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难道一个'preemptible内核仅仅意味着内核设计的方式,这将是安全的内核code的执行过程中有上下文切换?还是需要更多的工作,如果有做?

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 ?

我认为这意味着内核可以preempt用户code,而不是内核本身可以是preempted。这将是困难的和不寻常的任何东西来中断内核。这将需要更多的工作,而我挣扎明白为什么你想要它。

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.

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

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