How Scheduler is called in Linux

别等时光非礼了梦想. 提交于 2019-12-07 18:32:35

问题


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???


回答1:


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



回答2:


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.




回答3:


I think in case of round robin algorithm followed for scheduling some process some timer is being programmed based on time slice value which is going to invoke scheduler after that time slice gets over using interrupt functionality. and that preempt the current executing thread save its context and process ready queue figure out which thread to run next restore its context from thread stack and set PC value based on new thread's previous preempt value now new thread starts running. https://en.wikipedia.org/wiki/Programmable_interval_timer



来源:https://stackoverflow.com/questions/17522658/how-scheduler-is-called-in-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!