Ather Qadri
Ather Qadri

Reputation: 61

Who runs the scheduler in operating systems when CPU is given to user processes?

If there are 10 processes P1,P2...P10 and are scheduled using round robin policy by the scheduler to access the CPU. Now when Process P1 is using the CPU and the current time slice has expired, P1 needs to be preempted and P2 needs to scheduled. But since P1 is using the CPU, who preempts the P1 and schedules P2 ? We may Scheduler does this, but how does scheduler run when CPU is held by P1 ?

Upvotes: 6

Views: 1189

Answers (2)

Rohit
Rohit

Reputation: 1

Based on your Question P1 needs to be preempted and P2 needs to scheduled so there is a concept of CPU scheduler (CPU scheduler is the process of Operating System, that continuously watching the running process) responsibility to selects process among the processes in memory that are ready to execute, and allocates the CPU to one of them.

CPU scheduling is take place if a process:

  1. List item
  2. Switches from running to waiting state
  3. Switches from running to ready state
  4. Switches from waiting to ready
  5. Terminates

Dispatcher module gives control of the CPU to the process selected by the CPU scheduler;

Upvotes: -1

Fingolfin
Fingolfin

Reputation: 5533

It's exactly like jcoder said but let me elaborate (and make an answer instead of a comment)

Basically, when your OS boots, it initializes an interrupts vector where the CPU upon a given interrupt calls the appropriate interrupt handler.
The OS, also during boot time, will check for the available hardware and it'll detect that your board has x number of timers.

Timers are simply hardware circuits that tick using a given clock speed and they can be set to send an interrupt after a given time (each with a different precision usually, depending on its clock speed and other things)

After the OS detects the timers, it sets one of them, for example, to send an interrupt every 50 ms; now every 50 ms the CPU will stop whatever it's doing and invoke that interrupt handler, usually the scheduler code, which in turn will check what's the currently running process and make a decision to keep it or not depending on the scheduling policy.

The scheduler, like most of the OS actually, is a passive thing that acts only when there's some event.

Upvotes: 7

Related Questions