1

As per my understanding when the kernel finds out that the current running process should be striped of the CPU, it enables the need_resched flag. The flag is then checked before returning to user space, and if the flag enabled the kernel initiates call for schedule(). However I have noticed that within sys_sched_yield() routine we don't use the need_resched flag but explicitly call the schedule() routine. Why?

triple fault
  • 13,410
  • 8
  • 32
  • 45
  • `sys_sched_yield` is intended for rescheduling(it is syscall actually). Because it is called from user space, kernel can immediately perform scheduling instead of setting `need_resched` flag. – Tsyvarev Jun 28 '15 at 21:29
  • I know that sys_sched_yield() is a system call implementation. need_resched can't be accessed from user mode so your second sentence doesn't really have a meaning. – triple fault Jun 28 '15 at 21:34
  • You say that `need_resched can't be accessed from user mode`, but expect `sys_sched_yield` to check `need_resched` flag. Why? – Tsyvarev Jun 28 '15 at 22:41
  • sys_sched_yield is a method within sched.c it runs in kernel mode. Dont confuse system call wrapper functions and kernel code. – triple fault Jun 28 '15 at 22:42
  • sys_sched_yield is a method within sched.c, it runs in kernel mode **and** it implements system call `sched_yield`. There is no confusion here. Every system call is implemented as kernel code. – Tsyvarev Jun 30 '15 at 14:00

1 Answers1

2

As described here, need_resched flag is used for mark process externally: within interrupt by scheduler_tick() or when process is awoken by other process with try_to_wake_up(). Flag is needed, because the process itself may be not ready for rescheduling, so rescheduling is deffered until safe quiescent state.

If the process itself requests rescheduling, via schedule() call in kernel space, or via sched_yield() system call in user space, kernel performs request immediately. It is assumed that the process knows what it does. In the last case(when sched_yield() is called in user space), kernel knows that it is always safe to rescheduling while process in the user space.

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153