0

Do priority of the processes constitute to preemption when they are in Kernel mode?.

Say there are two processes with priorities 3 and 5. It is quiet obvious that the priority 5 process can be preempted by priority 3 process in user mode. Can that happen when they both are in kernel mode?

What are the other chances of a kernel preemption other than the waiting for IO, syscall takes long time, occurrence of an interrupt to the same CPU etc.

1 Answer 1

0

All scheduling decisions happen in kernel mode: when a process is running in user mode, its next scheduling point won’t happen before the kernel takes control again.

Process priorities come into play in a variety of ways when scheduling, but there is one explicit path where higher priority processes can influence scheduling. When a task becomes runnable, the check_preempt_curr hook is called, and if the newly-runnable task has a higher priority than the currently-running task, the hook requests a reschedule. This doesn’t immediately cause a reschedule, but it marks that the next time a reschedule is possible, it should be considered. This will typically happen on exit to userspace (see for example exit_to_usermode_loop on x86), so not long after. The reschedule might also not reschedule the newly-runnable task, if another task is more “deserving” of running when the scheduler runs.

Preemption can happen whenever a preemption point is reached, or on low-latency kernels, whenever the kernel is running code that’s not in a critical section.

See the various preemption configuration options, the __schedule function’s documentation, and the completely fair scheduler’s documentation for details.

2
  • I read when the process is about to move to user mode from kernel mode(i.e; exiting a system call), the kernel may schedule and preempt for another process. What if my application calls the system call within a lock(user space lock/mutex) ? Or else if my application never calls a system call, it just loops in a while(1)? For these above two cases where is the opportunity for kernel to preempt my application either in user/kernel mode?
    – Franc M
    May 11, 2020 at 11:59
  • User space locks won’t affect the kernel; they’ll only affect the processes which know about the lock. Any interrupt also involves a user-kernel-user transition, and therefore a scheduling opportunity; even on a system with no activity, the timer interrupt will fire at regular intervals (except on tickless CPUs, but that’s a very specific setup with its own constraints). May 11, 2020 at 12:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.