简体   繁体   中英

How to implement time counting in a new operating system?

i have to implement in a operating system, the function 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? Are CPU Ticks not dependend on the CPU frequency which is different for each CPU?

I have to implement the function in the language 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). The pTCP's for sleeping threads are stored in the queue ordered by interval expiry tick count. 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. 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.

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.

You can find a fast implementation with a good API here . 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_t t = time();

while(time() < t + sleepDuration);

You may use the CPUs Time Stamp Counter ( TSC ) to get counter values for time keeping. See Chapter 16.12.1 of "Intel® 64 and IA-32 Architectures Software Developer's Manual" .

The TSC is a low level counter which may provide counter values independent of CPU speed:

"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].

The invariant TSC will run at a constant rate in all ACPI P-, C--, and T-states. 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 reads are much more efficient and do not incur the overhead associated with a ring transition or access to a platform resource."

However, for the implementation of sleep() alike functionality you should look into timer hardware like HPET , ACPI , and alike. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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