davidcallanan / os-series

803 stars 118 forks source link

sleep/delay/wait function? #47

Closed MoistCoder closed 2 years ago

MoistCoder commented 2 years ago

How do I create a function that waits for x ms?

rualark commented 2 years ago

Hi. I created such a function here in my fork of this example OS: https://github.com/rualark/SimpleOsDemo/blob/master/src/impl/kernel/nanosleep/nanosleep.c

There are actually several approaches to do it. I implemented (i think) the most simple one without creating interrupt handler. I also created a more complex but more precise nanosleep function, which allows very precise time delays, and it also does not use interrupts:

https://github.com/rualark/SimpleOsDemo/blob/master/src/impl/kernel/nanosleep/nanosleep_exact.c

In real world using interrupt handlers is better, because it allows CPU to do some other work. But in this case there is no other work.