CTXSW(9)                     OpenBSD Kernel Manual                    CTXSW(9)

NAME
     mi_switch, cpu_switch - switch to another process context

SYNOPSIS
     void
     mi_switch(void);

     void
     cpu_switch(void);

DESCRIPTION
     The mi_switch() 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 mi_switch() can be enu-
     merated as follows:

           1.   From within sleep(9) and tsleep(9) when the current process
                voluntarily relinquishes the CPU to wait for some resource to
                become available.

           2.   After handling a trap (e.g., a system call or device
                interrupt) when the kernel prepares a return to user-mode exe-
                cution.  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 commu-
                nicated by the machine-independent scheduling routines by
                calling the machine-dependent need_resched(void).

           3.   In the signal handling code (see issignal(9)) if a signal is
                delivered that causes a process to stop.

     mi_switch() records the amount of time the current process has been run-
     ning in the process structure and checks this value against the CPU time
     limits allocated to the process (see getrlimit(2)). Exceeding the soft
     limit results in a SIGXCPU signal to be posted to the process, while ex-
     ceeding the hard limit will cause a SIGKILL. For a process which accumu-
     lated longer than 10 minutes of cpu time it's nice level is raised to 4.
     After these administrative tasks are done, mi_switch() hands over control
     to the machine dependent routine cpu_switch(void), which will perform the
     actual process context switch.

     cpu_switch() will make a choice amongst the processes which are ready to
     run from a priority queue data-structure.  The priority queue consists of
     an array qs[NQS] of queue header structures each of which identifies a
     list of runnable processes of equal priority (see <sys/proc.h>). A single
     word whichqs containing a bit mask identifying non-empty queues assists
     in selecting a process quickly.  cpu_switch() must remove the first pro-
     cess from the list on the queue with the highest priority (lower indices
     in qs indicate higher priority), and assign the address of its process
     structure to the global variable curproc. If no processes are available
     on the run queues, cpu_switch() shall go into an ``idle'' loop.  The idle
     loop must allow interrupts to be taken that will eventually cause pro-
     cesses to appear again on the run queues.  The variable curproc should be
     NULL while cpu_switch() waits for this to happen.

     Note that mi_switch() and thus cpu_switch() should be called at
     splhigh(9).

SEE ALSO
     spl(9), tsleep(9), wakeup(9)

OpenBSD 2.8                    November 24, 1996                             1
