Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Scheduler is called in Linux

In operating system the scheduler is invoked after the system call api or after a hardware interrupt processing.

I am unable to get and found how and who calls the scheduler???

like image 884
Pawan Avatar asked Oct 22 '25 19:10

Pawan


2 Answers

The scheduler is invoked:

  1. With explicit blocking: mutex, semaphore, waitqueue, etc.
  2. If TIF_NEED_RESCHED flag is set, on the nearest possible occasion:
    • If the kernel is preemptible:
      • in syscall or exception context, at the next outmost preempt_enable()
      • in IRQ context, return from interrupt-handler to preemptible context
    • If the kernel is not preemptible:
      • cond_resched() call
      • explicit schedule() call
      • return from syscall or exception to user-space
      • return from interrupt-handler to user-space
like image 121
Alexey Shmalko Avatar answered Oct 26 '25 04:10

Alexey Shmalko


The scheduler will be invoked if the current thread/process is going to sleep/wait for some event/resource to be released.

In one of the cases of worker threads which executes the bottom half in the form of workqueues, it will run in a while loop and check if the workqueue list is empty. If found empty it will mark itself as TASK_INTERRUPTABLE, calls schedule() and then goes to sleep.

If the workqueque list is not empty the worker thread marks itself RUNNING and executes the deferred bottom halfs.

So in general schedule() is called by a task which wants to sleep and thus hands over the cpu to other processes/tasks.

like image 45
Pramod Gurav Avatar answered Oct 26 '25 05:10

Pramod Gurav



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!