My own thought is that your
sleep() function shouldn't halt and loop at all; that's what your null process should be doing, instead. After all, the whole point of
sleep() - aside from having the process pause for a specified time - is to allow other processes to proceed while this process is waiting.
Rather, the
sleep() function ought to make a system call which yields to the scheduler, which according to most designs will
keep sleeping processes in a delta queue which counts down at each clock tick until the process is scheduled to awaken, at which point it goes back to the top of the regular scheduling queue (
not directly awakening the process, necessarily, as it still has to be handled according to priority).
For the queue itself, a simple
sleep_queue list data structure might look like this:
Code: Select all
struct sleep_node {
pid* sleeper;
uint64_t delta; // an offset in clock ticks from the preceding entries in the list
sleep_node* next;
} *sleep_queue;
This isn't a particularly good design, but it is a starting point.
Note that the kernel itself should not sleep directly at all; it makes no sense for it to, really. If it really needs to pause the system, it should schedule the null process, instead, though times when the system needs to do that are generally few and far between.
BTW, do you have an online repo (on Github, Sourceforge, wherever) where we could view your system code? It might help give us a better idea of what you are trying to do.