0

I have some questions about scheduling/multitasking in computer operating systems. If the details are platform-specific, I'm talking about Linux running on x86.

I understand that the scheduler is the component of the operating system that distributes threads of execution to physical processing cores (assuming no hyperthreading.) My question is, How does the scheduler tell a core to stop executing code from wherever in memory it currently is, and start executing code from a different point in memory? It seems like some kind of interrupt that the scheduler sends to the processing core, but AFAIK interupts eventually return to the original thread of execution. (as in they "interrupt" the conversation, not change the subject.)

I would read the Linux scheduler code but my understanding of C isn't that great.

1 Answer 1

1

The timer interrupt is set up by the operating system, so execution will return to the operating system. This is my understanding of the process and may lack the finer details.

  1. the OS loads up memory with CPU state information for the program to be executed
  2. the OS sets up an interrupt timer that says "give this interrupt handler a poke in 50uS" (or whatever the time slice is)
  3. the OS tells the CPU to "switch" to the program via a context switch and saves the state of the current OS task
  4. the CPU executes the program
  5. the interrupt is triggered when time runs out causing the CPU to unload the current program state to memory and launch the interrupt handler
  6. the interrupt handler essentially sets up and performs another context switch or simple branch (this time without any interrupt needed) to the OS scheduler.
  7. the scheduler looks for the next task ready to run and goes back to step 1.
3
  • Linux uses Completely fair scheduling (CFS) since 2007. It is described in detail at CFS: Completely fair process scheduling in Linux | Opensource.com
    – DavidPostill
    May 22, 2022 at 15:41
  • Okay! That makes sense. Follow up question: How is CPU usage measured? If a CPU core has to be constantly executing code, does that mean that CPU usage is based on the number of time slices that the core has executed in a given amount of time? May 22, 2022 at 15:42
  • @LennonMcLean The CPU will not have tasks running all the time. Many will be waiting on hard disk accesses, things to be drawn, inputs to happen. In that case the scheduler will simply put the core into a low power state for a time slice instead of wasting clock cycles. The amount of time in sleep as opposed to working is measurable. see superuser.com/a/41226/19943
    – Mokubai
    May 22, 2022 at 15:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .