睡眠如何在操作系统级别实现? [英] How is sleep implemented at the OS level?

查看:16
本文介绍了睡眠如何在操作系统级别实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是对 sleep(time in ms) 是如何在 C 库中或基本上在操作系统级别实现感兴趣...

I am just interested how sleep(time in ms) is implemented in a C library or basically at the OS level...

我猜……

  1. 可能是基于处理器的速度,你做一个 nop 的 while 循环(我不确定睡眠时间是否准确)...
  2. 处理器中的任何特殊寄存器,您在其中写入一些值,处理器会在指定的时间内停止(这将非常低效,因为处理器甚至无法运行其他程序).

有什么线索吗?大概C库源码可以解释一下吧?我不太关心C"是如何实现它的……我只是想知道sleep()"函数是如何实现的.

Any clues? Probably C library source code can explain? I am not too particular about how "C" is implementing it... I am just wondering in general how the "sleep()" function is implemented.

推荐答案

Sleep() 在操作系统级别实现.当任务/线程/进程处于睡眠状态时,处理器不会旋转.该特定线程被放置在待处理队列中(线程尚未准备好运行),直到时间到期,此时线程将被放置在准备运行队列中.

Sleep() is implemented at the OS level. The processor doesn't spin when a task/thread/process is sleeping. That particular thread is put on a pending queue (the thread isn't ready to run) until the time has expired at which point the thread will be placed on the ready to run queue.

同时,其他准备运行的线程也会运行.

In the meantime, other threads that are ready to run will be run.

只有当没有线程准备好运行时,操作系统才会进入空闲线程,这通常会发出指令以关闭(或无论如何进入低功耗状态)处理器,直到发生硬件中断.

Only if no threads are ready to run will the OS go into the idle thread, which in generally issues instructions to shutdown (or put into a low-power state anyway) the processor until an hardware interrupt occurs.

仅对于一个非常简单的系统(如最简单的嵌入式系统),Sleep() 实际上可能只是一个繁忙的等待循环来实现.

Only for a very simple system (like the most simple of embedded systems), might Sleep() actually be implemented as nothing more than a busy wait loop.

任何操作系统教科书,例如Tanenbaum 的现代操作系统" 将详细介绍这一点 - 几乎其中的任何一个(甚至是旧的、便宜的、用过的).

Any operating system textbook, such as "Modern Operating Systems" by Tanenbaum will cover this in great detail - pretty much any of them (even an old, cheap, used one).

这篇关于睡眠如何在操作系统级别实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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