In the previous section, we discussed OS – Processes. In that article we have seen what is process, the stages of process and the PCB(Process Control Block). Now, Let us discuss process scheduling in operating system (OS).

What is Process Scheduling in Operating System?

The process which selects another process on the base of some particular strategy by removing the running process from the CPU is called process scheduling in operating system. Process manager do this activity.

Multi-programming OS involves process scheduling because it is very essential. Such OS work in the following way,

  • At a time, it loads more than one process in executable memory.
  • Time multiplexing shares CPU between these processes.

Process Scheduling in operating system Queues:

PCBs in Process Scheduling Queues. Whereas, OS maintains a separate queue for each process state. In order to place PCBs of all processes in same execution state, OS uses a single queue. Current queue unlinks and move PCB to new state when process state changes. The system also unlinks PCB from current queue and moves to new state queue after change of its process state.

OS maintains following Important scheduling,

  • Job Queue: This queue keeps all processes of system.
  • Ready Queue: Ready queue keeps all the processes which are ready and waiting to execute in main memory.
  • Device Queues: Sometimes there is unavailability of I/O devices so a process can’t get the I/O. Then device queues keep these processes

Following figure shows the mechanism of queues:

Operating system uses the following policies to manage the queues:

  • First-Come, First-Served (FCFS) Scheduling
  • Shortest-Job-Next (SJN) Scheduling
  • Priority Scheduling
  • Shortest Remaining Time
  • Round Robin (RR) Scheduling
  • Multiple-Level Queues Scheduling

The OS scheduler determine moving processes from ready to run queue. Which can only have one entry per processor core on the system.

Two State Process Model:

process model
two state process model i.e running, not running

There are two states of process which relates this process model.

  • Running: A new process that enters in to the system upon it’s creation.
  • Non-Running: Queue keeps the processes that are waiting for execution and not running. Specific entry points is a particular process in the queue. The system uses Linked list to make a queue.

Use of Dispatcher:

Dispatcher forwards a process to waiting queue when a process is interrupted. It is discarded if it is completed or aborted. In either case, dispatcher selects a process from queue to execute.

Schedulers:

scheduling in operating system
long-term and short-term scheduling

Process scheduling in operating system is a major practice handled by special system software called schedulers. Their main task is following,

  • Select jobs
  • Submit into system
  • Decide which process to run

Listed below are the three types of schedulers,

  • Long-Term Scheduler
  • Short-Term Scheduler
  • Medium-Term Scheduler

Long-Term Scheduler:

It is also known as job scheduler. In order to process in the system the scheduler decides the programs to send..The scheduler then selects and loads a process from queue to memory for it’s execution. CPU scheduling is done by loading process into memory.

Its main task is to give a balanced mix of jobs both I/O and CPU bounded. Long term scheduler controls degree of multi programming. To stable the degree of multi programming, the average rate of process creation must be equal to average departure rate of processes leaving system.

Some systems don’t have long term scheduler especially time-sharing OS. When a process changes state from new to ready, we use long-term scheduler.

Short-Term Scheduler:

The second name of short-term scheduler is CPU scheduler. It increases the performance according to chosen set of criteria. It changes the state from ready to running state of process. CPU scheduler selects and allocates a ready process for it’s execution.

This scheduler determines ,which process to execute next. They are faster than long term schedulers.

Medium-Term Scheduler:

It is the part of swapping. As it in charge of handling the processes which are swapped out.

What is swapping?

An I/O request can make a running process suspended. This process can’t make further completion. In order to make space for other process, the suspended process moves to secondary storage. This process is called swapping. The process on which swapping takes place is called rolled out or swapped out process. To improve process mix swapping is necessary.

Medium term scheduler removes processes from memory. Also it increases the degree of multi programming.

 

Comparison among Schedulers:

Long-Term Scheduler:

  • It is a job scheduler.
  • It has less speed than short term scheduler.
  • This scheduler controls the degree of multi-programming.
  • Time sharing systems has no minimal long term scheduler.
  • It loads processes in memory after selecting from pool.

Short-Term Scheduler:

  • Basically, it is CPU scheduler.
  • It has more speed than both long and short-term schedulers.
  • Its control over degree of multi programming is low.
  • In time sharing systems it is minimal.
  • Short Term Scheduler selects those processes which are ready for their execution.

Medium-Term Scheduler:

  • It main purpose and use is process swapping.
  • Its speed lies between the speeds of both long and short-term schedulers.
  • Medium Term Scheduler also reduces the degree of multi-programming.
  • It is the part of time sharing systems.
  • Even more, Medium-term re-introduces the process into memory for execution.

Context Switching:

context switching
context switching illustration

The process execution can be resumed from the same point at a later time by storing and restoring the context of CPU in the PCB. This mechanism is called context switching. Multiple processes uses single CPU with the help of contact switch process. Multi programming operating system’s essential feature is context switching.

Process control block stores the state of current running process when scheduler switches CPU from one executing process to another. Then after this, the PCB loads the state of the next process and uses the process state to set the PC, registers, etc. . At this point, the second process can start executing.

It is necessary to first restore and then save the state of current running process. Therefore context switching is computationally an expensive process. In order to avoid context switching time we use two or more processor registers sets in some hardware. Following information is used for later use when process is switched,

  • Program Counter
  • Scheduling Information
  • Base and limit register
  • Currently used register
  • Changed State
  • I/O State Information
  • Accounting Information