multitasking, the timer and early preemtion

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
sigler
Posts: 15
Joined: Wed Oct 31, 2007 11:37 am

multitasking, the timer and early preemtion

Post 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
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re: multitasking, the timer and early preemtion

Post 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.
User avatar
bewing
Member
Member
Posts: 1401
Joined: Wed Feb 07, 2007 1:45 pm
Location: Eugene, OR, US

Post 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.
Post Reply