Search code examples
linuxwindowsmacosoperating-systemscheduling

Is using a hardware timer the only way to implement process scheduling?


I have read that an operating system implements scheduling using the following method:

  • The CPU must have a hardware timer that fires an interrupt every X milliseconds.
  • Once the timer interrupt fires, the timer interrupt handler will execute instructions that will make the CPU start executing another process (or another thread I guess I should say).

My question is: Is the method I just described the only way to implement scheduling?


Solution

  • No. Basically there are two basic methods of implementing multithreading in an operating system:

    1) Preemtive Multitasking

    With preemtive multitasking you can usw interrupt source to trigger your task switch. Most of the time one does task switching inside the timer ISR(Interrupt Service Routine) in case a long running task is executed and no other hardware events have happened. In case other hardware events have happened one might also do a task switch to blocking threads with higher priority to allow handling of hardware events.

    2) Cooperative Multitasking

    In cooperative Multitasking the operating system switches threads whenever a system call is executed. This can either be a special system call that allows an application to explicitly trigger a task switch (like Yield used in early multitasking systems like Windows 3.11, classical Mac OS, etc.). One can also implement cooperative multitasking completely inside user mode.

    Today most operating systems take a hybrid approach - they react to hardware events (in case a long running thread never calls the systems routines and no other I/O happens this would be the timer) but they may also switch tasks in a cooperative way whenever applications perform syscalls or call system supplied libraries.