1

As far as I understand by reading some SO questions and the kernel docs, in Linux, a task (thread, process, or even a group of tasks) is allowed to execute without interruption during a minimum amount of time (3ms in my laptop according to /proc/sys/kernel/sched_min_granularity_ns), after which it can be stopped anytime if the load is high. If that's not the case and that CPU time has been exceeded, the task must be stopped to allow other tasks to progress.

But in my understanding, once the CPU is running a task nothing can stop it, which naturally gives birth to two very related questions:

  • how can a task be stopped? the only thing I can think of is by the OS sending some simple CPU interruption of some sort, or is there anything more complex involved here?
  • if the CPU is single-threaded and can only execute one task at a time, can tasks be stopped at all? because if the CPU is currently running a task, how could Linux possibly execute their own management routines to check if the task must be stopped?

1 Answer 1

4

Scheduling can take place whenever the kernel gains control. There are two ways for this to happen, and both of these can happen even with a single CPU:

  • interrupts, where a hardware event causes the CPU to switch to the corresponding interrupt handler (in the kernel);
  • system calls, where the running task calls the kernel to perform some work for it.

In both cases, before the kernel returns control to user space, it determines which runnable task should gain control — this can be the originally-interrupted task, but it doesn’t have to be.

Even if there isn’t much hardware activity, the timer interrupts ensure that the kernel gains control periodically.

0

You must log in to answer this question.

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