switch to another process context
() function implements the
machine-independent prelude to a process context switch. It is called from
only a few distinguished places in the kernel code as a result of the
principle of non-preemptable kernel mode execution. The three major uses of
() can be enumerated as follows:
- From within functions like
tsleep(9), when the current
process sleeps to wait for some resource to become available, or from
within functions like
yield(), when it
relinquishes the CPU to allow other processes to make progress.
- After handling a trap (e.g., a system call or device interrupt) when the
kernel prepares a return to user-mode execution. This case is typically
handled by machine-dependent trap-handling code after detection of a
change in the signal disposition of the current process, or when a higher
priority process might be available to run. The latter event is
communicated by the machine-independent scheduling routines by calling the
- In the signal handling code (see
sys/kern/kern_sig.c) if a signal is
delivered that causes a process to stop.
() records the amount of time the
current process has been running in the process structure and checks this
value against the CPU time limits allocated to the process (see
). Exceeding the
soft limit results in a
be posted to the process, while exceeding the hard limit will cause a
. After these administrative tasks
() chooses the next
process to run and hands over control to the machine dependent routine
(), which will perform the
actual process context switch.
() will save the context of the
old process and switch to the new one. A special case is when the old process
which means that the old process
has exited and doesn't need to be saved.
() and thus
() must be called while holding
the scheduler lock.