Page 1 of 1
How does the scheduler preemption works?
Posted: Thu Feb 09, 2012 8:37 pm
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.
Re: How does the scheduler preemption works?
Posted: Thu Feb 09, 2012 8:51 pm
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.
Re: How does the scheduler preemption works?
Posted: Thu Feb 09, 2012 10:09 pm
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.
Re: How does the scheduler preemption works?
Posted: Thu Feb 09, 2012 10:24 pm
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.
Re: How does the scheduler preemption works?
Posted: Thu Feb 09, 2012 10:42 pm
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.
Re: How does the scheduler preemption works?
Posted: Fri Feb 10, 2012 12:42 am
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.
Re: How does the scheduler preemption works?
Posted: Fri Feb 10, 2012 2:38 am
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.
Re: How does the scheduler preemption works?
Posted: Fri Feb 10, 2012 1:28 pm
by luxianos
Thanks!! I am understanding now.