Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OS: does the process scheduler runs in separate process

I have few doubts about how operating system works.

Scheduler: Does the scheduler runs in a separate process(like any other process). What exactly happens at the time of swapping in new process(i know the processor registers and memory tables are updated, my question is how they are updated. Can we write a program to update the registers(sc, pc) to point to a different process).

like image 569
sujith Avatar asked Aug 02 '12 01:08

sujith


People also ask

How does process scheduler work?

It selects processes from the queue and loads them into memory for execution. Process loads into the memory for CPU scheduling. The primary objective of the job scheduler is to provide a balanced mix of jobs, such as I/O bound and processor bound. It also controls the degree of multiprogramming.

Is scheduler a process?

A scheduler is a type of system software that allows you to handle process scheduling. Three types of the scheduler are 1) Long term 2) Short term 3) Medium-term. Long term scheduler regulates the program and select process from the queue and loads them into memory for execution.

What is the role of scheduler in operating system?

The purpose of the scheduler, is to choose processes from the list of ready processes". Dispatcher id: the component of the Operating System that dispatches the ready process to the processor, so that it can be executed.

How does scheduler help in switching between processes?

Short Term Scheduler: This is the change of ready state to running state of the process. It selects a process from the multiple processes that are in ready state in order to execute it and also allocates the CPU to one of them.


1 Answers

The process schedule could feasibly run in a separate process, but such a design would be very inefficient since you would have to swap from one process to the scheduling process (which would then have to make several system calls to the kernel) and then back to the new process, as opposed to just placing the scheduler in the kernel where you will not need system calls nor need to swap contexts more than once. Therefore, the scheduler is generally in the exclusive realm of the kernel.

Here are the steps that occur:

  1. The scheduler determines which process will run in the next time slot (through various different algorithms).

  2. The scheduler tells the Memory Managing Unit (MMU) to use the page table for the next process to run (this is done by setting a register to point to the table).

  3. The scheduler programs the Programmable Interrupt Timer (PIT) to generate an interrupt after N clock cycles.

  4. The scheduler restores the state of the registers from when the process was last running (or sets them to default values for new processes)

  5. The scheduler jumps to the address of the last instruction that was not executed in the process.

  6. After N clock cycles, an interrupt occurs and the operating system recognizes it as caused by the PIT, which is registered to be handled by the scheduler.

  7. The scheduler saves the state of the registers (including stack pointer, etc) and grabs the program counter of where the interrupt occured (and saves it as the address to jump to next time around) and then goes back to step 1.

This is just one example of how it can be done, and many of the low level details are architecture specific. Essentially all the registers (the program state) can be saved to any place in RAM (say a linked list of structures that represent processes each having space for the registers, etc) and the virtual address space (defined by page tables) can be arbitrarily swapped out.

So essentially your question:

"Can we write a program to update the registers to point to a different process?"

is simply stated, yet the answer is correct. We sure can.

like image 119
Dougvj Avatar answered Nov 03 '22 00:11

Dougvj