简体   繁体   English

如何在新的操作系统中实现计时?

[英]How to implement time counting in a new operating system?

i have to implement in a operating system, the function sleep(). 我必须在操作系统中实现功能sleep()。 Which is at the moment, not exisiting in the previous mentioned system. 目前,这在前面提到的系统中不存在。 The problem is, I have to count the elapsed time to wake the sleeping thread up. 问题是,我必须计算经过的时间才能唤醒睡眠线程。

How should i relize this? 我应该如何处理呢? Do i have to count the CPU Ticks or is there another way? 我是否需要计算CPU时钟数或还有其他方法吗? Are CPU Ticks not dependend on the CPU frequency which is different for each CPU? CPU标记是否不依赖于每个CPU不同的CPU频率?

I have to implement the function in the language C. 我必须用C语言实现该功能。

the time function doesn't exist either 时间函数也不存在

Thank you in advance! 先感谢您!

Typically, such functionality is provided by a hardware timer interrupt, (and its associated driver), that manages a 'tick count' and a delta-queue of 'Thread Control Block' pointers, (pTCB). 通常,此类功能由硬件计时器中断(及其相关的驱动程序)提供,该中断管理“滴答计数”和“线程控制块”指针(pTCB)的增量队列。 The pTCP's for sleeping threads are stored in the queue ordered by interval expiry tick count. 睡眠线程的pTCP存储在按时间间隔到期滴答计数排序的队列中。 The timer interrupt increments the tick count and checks it agains the expiry count of the item at the head of the queue. 计时器中断会增加滴答计数,并再次检查它在队列开头的项的到期计数。

When a thread requests a sleep, the thread pTCB is taken out of the set of ready threads, the expiry-count calculated and the pTCB inserted into the timer queue. 当线程请求睡眠时,将线程pTCB从准备好的线程集中取出,计算到期时间,并将pTCB插入计时器队列。 When the pTCB reaches the end of the queue, and it's expiry tick has arrived, it is popped and added back to the set of ready threads so that it may be set running. 当pTCB到达队列的末尾并且到达了到期时钟时,它将弹出并添加回准备好的线程集中,以便可以将其设置为运行。

It totally depends on your platform/OS. 这完全取决于您的平台/操作系统。 It has to provide you some time-like information, eg ticks. 它必须为您提供一些类似时间的信息,例如刻度线。 Otherwise it is just impossible. 否则,这是不可能的。

Converting ticks to seconds of course requires additional information. 当然,将刻度转换为秒还需要其他信息。 Again, this can be supplied by your platform. 同样,这可以由您的平台提供。 Or you have to find it out by other means (manual, configure it yourself, ...). 或者,您必须通过其他方式找到它(手动,自行配置,...)。

The easiest and most common way to do that in operating systems is to set up a timer interrupt at a static frequency, then build a timer framework on top of that, then use that timer framework to fire off wakeups for your sleeping threads. 在操作系统中最简单,最常见的方法是在静态频率上设置计时器中断,然后在该频率上构建计时器框架,然后使用该计时器框架为睡眠线程触发唤醒。

A good paper that discusses various data structures for how to do it efficiently is here . 中有关如何有效地做到这一点的各种数据结构良好的纸在这里 I recommend from my own experience scheme 7. It's quite easy to implement and performs wonderfully. 我从我自己的体验方案7中推荐。它很容易实现并且表现出色。

You can find a fast implementation with a good API here . 您可以在此处找到具有良好API的快速实现。 But I'm biased, because I wrote it. 但是我有偏见,因为我写了它。

If you don't want a timer interrupt with a static frequency it becomes much harder to implement a nice timer facility with good performance. 如果您不希望使用静态频率中断计时器,则难以实现性能良好的良好计时器功能。 I've done a few experiments, but I'd recommend you to start with simple timer interrupt with a static frequency. 我已经做过一些实验,但建议您从具有固定频率的简单计时器中断开始。 Once you start doing dynamic timers you need to exactly understand the tradeoffs you are prepared to make. 一旦开始使用动态计时器,就需要准确地了解您准备进行的权衡。

you can use time() : 您可以使用time()

time_t t = time();

while(time() < t + sleepDuration);

You may use the CPUs Time Stamp Counter ( TSC ) to get counter values for time keeping. 您可以使用CPU时间戳计数器( TSC )来获取用于计时的计数器值。 See Chapter 16.12.1 of "Intel® 64 and IA-32 Architectures Software Developer's Manual" . 请参阅“英特尔 ®64 和IA-32架构软件开发人员手册”的第16.12.1章。

The TSC is a low level counter which may provide counter values independent of CPU speed: TSC是一个低电平计数器,可以提供与CPU速度无关的计数器值:

"The time stamp counter in newer processors may support an enhancement, referred to as invariant TSC. Processor's support for invariant TSC is indicated by CPUID.80000007H:EDX[8]. “更新的处理器中的时间戳计数器可以支持一种称为不变TSC的增强功能。处理器对不变TSC的支持由CPUID.80000007H:EDX [8]指示。

The invariant TSC will run at a constant rate in all ACPI P-, C--, and T-states. 在所有ACPI P,C和T状态下,不变TSC将以恒定速率运行。 This is the architectural behavior moving forward. 这是前进的架构行为。 On processors with invariant TSC support, the OS may use the TSC for wall clock timer services (instead of ACPI or HPET timers). 在具有不变TSC支持的处理器上,OS可以将TSC用于壁钟计时器服务(而不是ACPI或HPET计时器)。 TSC reads are much more efficient and do not incur the overhead associated with a ring transition or access to a platform resource." TSC读取效率更高,并且不会产生与环转换或访问平台资源相关的开销。”

However, for the implementation of sleep() alike functionality you should look into timer hardware like HPET , ACPI , and alike. 但是,对于类似sleep()的功能的实现,您应该研究计时器硬件,例如HPETACPI等。 See "Intel 64® and IA-32 Architectures Software Developer's Manual, Volume 3B: System Programming Guide, Part 2" and "IA-PC HPET (High Precision Event Timers) Specification " for details. 有关详细信息,请参见“ Intel64®和IA-32体系结构软件开发人员手册,第3B卷:系统编程指南,第2部分”“ IA-PC HPET(高精度事件计时器)规范

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM