Page 1 of 1

multitasking, the timer and early preemtion

Posted: Wed Nov 21, 2007 10:49 am
by sigler
Hello,

I've got multitasking working. Now I want to add that when a task wants to wait on something, it is preempted immediately, and another task continues.

One problem I see with this is that the first task would get only part of the timeslice, and the other task would get the remainder of the timeslice, so code like this in the timer interrupt:

Code: Select all

timer()
{
task->timeslice--;
}
wouldn't be precise anymore?

Let's say the timer ticks each ms, one could have

task->timeslice -= 1000

and when early preemption is done, one could query the pit for it's current counter value, to know exactly how much of the timeslice has been used and the remaining time left in the timeslice for the next task, and use those values for decrementing task->timeslice

Any thoughts.. ?
How is this usually done?

--
Sigurd Lerstad

Re: multitasking, the timer and early preemtion

Posted: Wed Nov 21, 2007 12:37 pm
by Candy
sigler wrote:How is this usually done?
It usually isn't done.

Search the web for the Completely Fair Scheduler from Linux, where they finally tackle this bit. They have iirc 5 exploits for you to test for fairness.

Posted: Wed Nov 21, 2007 5:29 pm
by bewing
In my OS, I'm expecting almost all tasks to block before their timeslice is up. So almost all tasks get started in the middle of a timeslice, too (and my timeslices are short). So, my scheduler just grants one extra timeslice for any app that gets started in the middle of a timeslice. It's fair enough, mathematically.