How does the scheduler preemption works?

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
luxianos
Posts: 4
Joined: Thu Feb 09, 2012 8:28 pm

How does the scheduler preemption works?

Post by luxianos »

Hi, I have a question. I have searching for a lot of hours in internet but I did not find an answer.

When a preemptive scheduler is working in the OS, it choices a process to run, but how the control returns to the scheduler at the specified time?

Thanks for your answers, sorry for my poor english.
User avatar
thepowersgang
Member
Member
Posts: 734
Joined: Tue Dec 25, 2007 6:03 am
Libera.chat IRC: thePowersGang
Location: Perth, Western Australia
Contact:

Re: How does the scheduler preemption works?

Post by thepowersgang »

Short answer: the timer interrupt fires.

Long answer: When the task is started, you schedule an interrupt for t+some time (e.g. 10ms) and if the task does not yield the processor in that time, the timer will fire, forcing the task switch. Otherwise, the timer is reset for the next task.
Kernel Development, It's the brain surgery of programming.
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc
luxianos
Posts: 4
Joined: Thu Feb 09, 2012 8:28 pm

Re: How does the scheduler preemption works?

Post by luxianos »

Thanks. But, who checks the timer? i.e.: the processor is running a process (or thread), so, in what moment checks the timer to fire it? My knowledges about the architecture of the processor are basics for now, maybe there is something that I forgotten.
User avatar
bubach
Member
Member
Posts: 1223
Joined: Sat Oct 23, 2004 11:00 pm
Location: Sweden
Contact:

Re: How does the scheduler preemption works?

Post by bubach »

The basics of it all: You set up the timer interrupt in your kernel code, so when it fires (how often this happens depends on your clock setting) you'll be back in the kernel and can change the stack to another tasks. Making the iret jump to that new task instead.
"Simplicity is the ultimate sophistication."
http://bos.asmhackers.net/ - GitHub
TylerH
Member
Member
Posts: 285
Joined: Tue Apr 13, 2010 8:00 pm
Contact:

Re: How does the scheduler preemption works?

Post by TylerH »

luxianos wrote:Thanks. But, who checks the timer? i.e.: the processor is running a process (or thread), so, in what moment checks the timer to fire it? My knowledges about the architecture of the processor are basics for now, maybe there is something that I forgotten.
It sounds like you need to read about interrupts. The timer fires an interrupt after the specified length of time.
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: How does the scheduler preemption works?

Post by bluemoon »

You can also force a reschedule on some API ( like blocking IO or IPC signals ). It's up to the design of proper code path.
linguofreak
Member
Member
Posts: 510
Joined: Wed Mar 09, 2011 3:55 am

Re: How does the scheduler preemption works?

Post by linguofreak »

luxianos wrote:Thanks. But, who checks the timer? i.e.: the processor is running a process (or thread), so, in what moment checks the timer to fire it? My knowledges about the architecture of the processor are basics for now, maybe there is something that I forgotten.
The timer itself fires the interrupt. The processor doesn't check anything at all. It's like an alarm clock: you set it and go do something else (like working or sleeping) while you wait for it to go off. You don't need to check it 'cause you'll hear it when it rings. Likewise, the processor tells the timer to interrupt it after a specified amount of time, and can then go and do anything without needing to check the timer: The timer will send it a signal when the arranged time comes.
luxianos
Posts: 4
Joined: Thu Feb 09, 2012 8:28 pm

Re: How does the scheduler preemption works?

Post by luxianos »

Thanks!! I am understanding now. :wink:
Post Reply