Who schedules the scheduler in OS - Isn't it a chicken and egg scenario?
Asked Answered
S

2

15

Who schedules the scheduler?

Which is the first task created and how is this first task created? Isn't any resource or memory required for it? isn't like a chicken and egg scenario?

Isn't scheduler a task? Does it get the CPU at the end of each time slice to check which task needs to be given CPU?

Are there any good links which makes a person think and understand deeply all these concepts rather than spilling out some theory which needs to be byhearted?

Strabismus answered 12/2, 2010 at 8:10 Comment(1)
There is not really a generic answer. You would need to look at a specific OS implementation. However, @Ira's answer is probably applicable to most OSes.Melanous
W
17

The scheduler is scheduled by

  • an (external) event such as an interrupt, (disk done, mouse click, timer tick)
  • or an internal event (such as the completion of a thread, the signalling by a thread that it needs to wait for something, or the signalling of a thread that it has released a resource, or a trap caused by a thread doing something illegal like division by zero)

In short, it is triggered by any event that might require that the set of tasks to be run and/or the priorities of those tasks to be reevaluated. The scheduler decides which task(s) run next, and passes control to the next task.

Typically, this "scheduling" of the scheduler is caused by the code associated with a hardware interrupt, or code associated with a system call.

While you can think of the scheduler as being a real thread, in practice it doesn't need to be implemented that way... because it is executed with higher priority than any other task. Sophisticated OSes may in fact set aside a special thread that is the scheduler, and mark it busy when the scheduler gets control. That makes it pretty, but the bogus thread isn't scheduled by the scheduler

One can have multiple schedulers: the highest priority one (e.g., the one we just described), and other schedulers which really are threads, and are run like other user tasks. Such lower priority schedulers tend to be used to manage actions which occur at much longer intervals, such as background jobs.

Wernsman answered 12/2, 2010 at 8:38 Comment(2)
Agreed: The scheduler is not a task, it is a procedure that is invoked at 'scheduling points'.Salaried
I used to take the hardware timer interrupt as the main source to trigger the scheduler function. The schuduler function is responsible for all the task switch details, such as context swith and priority adjustment. But now I find that this would mean that task switch timing is predictable. Ira's answer is very good.Photooffset
I
2

it is usually invoked periodically by a timed CPU interrupt

Increate answered 28/2, 2018 at 7:22 Comment(1)
I'm not sure whether this really adds to the question... there already is an answer mentioning interrupts and the question is from 2010.Riehl

© 2022 - 2024 — McMap. All rights reserved.