sgoel
sgoel

Reputation: 1

Practical ways of implementing preemptive scheduling without hardware support?

I understand that using Hardware support for implementing preemptive scheduling is great for efficiency.

I want to know, What are practical ways we can do preemptive scheduling without taking support from hardware? I think one of way is Software Timers.

Also, Other way in multiprocessor system is using the one processor acting as master keep looking at slave processor's processor.

Consider, I'm fine with non-efficient way.

Please, elaborate all ways you think / know can work. Also, preferably but not necessarily works for single processor system.

Upvotes: 0

Views: 742

Answers (1)

In order to preempt a process, the operating system has to somehow get control of the CPU without the process's cooperation. Or viewed from the other perspective: The CPU has to somehow decide to stop running the process's code and start running the operating system's code.

Just like processes can't run at the same time as other processes, they can't run at the same time as the OS. The CPU executes instructions in order, that's all it knows. It doesn't run two things at once.

So, here are some reasons for the CPU to switch to executing operating system code instead of process code:

  • A hardware device sends an interrupt to this CPU - such as a timer, a keypress, a network packet, or a hard drive finishing its operation.
  • The software running on a different CPU sends an inter-processor interrupt to this CPU.
  • The running process decides to call a function in the operating system. Depending on the CPU architecture, it could work like a normal call, or it could work like a fake interrupt.
  • The running process executes an instruction which causes an exception, like accessing unmapped memory, or dividing by zero.
  • Some kind of hardware debugging interface is used to overwrite the instruction pointer, causing the CPU to suddenly execute different code.
  • The CPU is actually a simulation and the OS is interpreting the process code, in which case the OS can decide to stop interpreting whenever it wants.

If none of the above things happen, OS code doesn't run. Most OSes will re-evaluate which process should be running, when a hardware event occurs that causes a process to be woken up, and will also use a timer interrupt as a last resort to prevent one program hogging all the CPU time.

Generally, when OS code runs, it has no obligation to return to the same place it was called from. "Preemption" is simply when the OS decides to jump somewhere other than the place it was called from.

Upvotes: 3

Related Questions