什么是睡眠背后的算法()? [英] What's the algorithm behind sleep()?

查看:117
本文介绍了什么是睡眠背后的算法()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在有件事情我一直在想:怎么是在睡眠()实施

Now there's something I always wondered: how is sleep() implemented ?

如果它是所有关于使用API​​的操作系统,那么如何是API做?

If it is all about using an API from the OS, then how is the API made ?

这一切都归结为使用特殊机器code在CPU上?这是否CPU需要一个特殊的协处理器或其他小玩意儿,没有这些你不能有睡眠()?

Does it all boil down to using special machine-code on the CPU ? Does that CPU need a special co-processor or other gizmo without which you can't have sleep() ?

睡眠()最有名的化身是在C(更准确,其附带的C编译器,如GNU的libc中的库),虽然几乎每一种语言今天都有相当,但睡在执行一些语言(想想击)不是我们所要找的在这个问题...

The best known incarnation of sleep() is in C (to be more accurate, in the libraries that come with C compilers, such as GNU's libc), although almost every language today has its equivalent, but the implementation of sleep in some languages (think Bash) is not what we're looking at in this question...

编辑:读了一些答案后,我看到该进程被放置在等待队列。从那里,我可以猜到两种选择,要么

After reading some of the answers, I see that the process is placed in a wait queue. From there, I can guess two alternatives, either

  1. 一个计时器被设置使得在内核唤醒过程在适当的时候,或者
  2. 每当内核是允许一个时间片,它轮询时钟来检查它是否是时候唤醒的过程。

这些问题的答案只提及替代1。因此,我问:请问这是怎么计时器的行为?如果它是一个简单的中断,使内核唤醒过程中,如何可以在内核要求定时器为我叫醒在140毫秒,所以我可以把过程运行状态?

The answers only mention alternative 1. Therefore, I ask: how does this timer behave ? If it's a simple interrupt to make the kernel wake the process, how can the kernel ask the timer to "wake me up in 140 milliseconds so I can put the process in running state" ?

推荐答案

更新,以问题表明了操作系统如何现代化工作的一些误解。

The "update" to question shows some misunderstanding of how modern OSs work.

内核是不是允许一个时间片。内核是给出了时间片用户进程的东西。 计时器未设置为唤醒沉睡的过程了 - 它被设置为停止当前运行的进程

The kernel is not "allowed" a time slice. The kernel is the thing that gives out time slices to user processes. The "timer" is not set to wake the sleeping process up - it is set to stop the currently running process.

在本质上,内核尝试通过停止是CPU上太长流程来公平地分配的CPU时间。对于一个简化的图片,让我们说,没有任何进程被允许使用的CPU超过2毫秒。因此,内核将设置定时器为2毫秒,让进程运行。当定时器触发的中断,内核得到控制。这样可以节省运行的进程当前状态(寄存器,指令指针等),并控制不返回到它。相反,另一种方法是从等待被定CPU的进程的列表拾取,并且被中断的过程进到队列的后部。

In essence, the kernel attempts to fairly distribute the CPU time by stopping processes that are on CPU too long. For a simplified picture, let's say that no process is allowed to use the CPU more than 2 milliseconds. So, the kernel would set timer to 2 milliseconds, and let the process run. When the timer fires an interrupt, the kernel gets control. It saves the running process' current state (registers, instruction pointer and so on), and the control is not returned to it. Instead, another process is picked from the list of processes waiting to be given CPU, and the process that was interrupted goes to the back of the queue.

在睡眠过程很简单的没有的的事情等着CPU队列。相反,它存储在睡眠队列。每当内核得到定时器中断,睡眠队列检查,其时间来到得到转移到等待CPU的进程队列。

The sleeping process is simply not in the queue of things waiting for CPU. Instead, it's stored in the sleeping queue. Whenever kernel gets timer interrupt, the sleep queue is checked, and the processes whose time have come get transferred to "waiting for CPU" queue.

这是当然,一个粗略的简化。这需要非常复杂的算法来确保安全性,公平性,平衡性,主次分明,prevent饥饿,做这一切快速的内存用于内核数据的最低金额。

This is, of course, a gross simplification. It takes very sophisticated algorithms to ensure security, fairness, balance, prioritize, prevent starvation, do it all fast and with minimum amount of memory used for kernel data.

这篇关于什么是睡眠背后的算法()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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