]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - share/man/man9/mi_switch.9
This commit was generated by cvs2svn to compensate for changes in r69587,
[FreeBSD/FreeBSD.git] / share / man / man9 / mi_switch.9
1 .\"     $NetBSD: ctxsw.9,v 1.2 1996/12/02 00:11:31 tls Exp $
2 .\"
3 .\" Copyright (c) 1996 The NetBSD Foundation, Inc.
4 .\" All rights reserved.
5 .\"
6 .\" This code is derived from software contributed to The NetBSD Foundation
7 .\" by Paul Kranenburg.
8 .\"
9 .\" Redistribution and use in source and binary forms, with or without
10 .\" modification, are permitted provided that the following conditions
11 .\" are met:
12 .\" 1. Redistributions of source code must retain the above copyright
13 .\"    notice, this list of conditions and the following disclaimer.
14 .\" 2. Redistributions in binary form must reproduce the above copyright
15 .\"    notice, this list of conditions and the following disclaimer in the
16 .\"    documentation and/or other materials provided with the distribution.
17 .\" 3. All advertising materials mentioning features or use of this software
18 .\"    must display the following acknowledgement:
19 .\"        This product includes software developed by the NetBSD
20 .\"        Foundation, Inc. and its contributors.
21 .\" 4. Neither the name of The NetBSD Foundation nor the names of its
22 .\"    contributors may be used to endorse or promote products derived
23 .\"    from this software without specific prior written permission.
24 .\"
25 .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26 .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 .\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
29 .\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 .\" POSSIBILITY OF SUCH DAMAGE.
36 .\"
37 .\" $FreeBSD$
38 .\"
39 .Dd Nov 24, 1996
40 .Dt MI_SWITCH 9
41 .Os FreeBSD
42 .Sh NAME
43 .Nm mi_switch ,
44 .Nm cpu_switch
45 .Nd switch to another process context
46 .Sh SYNOPSIS
47 .Fd #include <sys/param.h>
48 .Fd #include <sys/proc.h>
49 .Ft void
50 .Fn mi_switch "void"
51 .Ft void
52 .Fn cpu_switch "struct proc *p"
53 .Sh DESCRIPTION
54 The
55 .Fn mi_switch
56 function implements the machine independent prelude to a process context
57 switch.
58 It is called from only a few distinguished places in the kernel
59 code as a result of the principle of non-preemtable kernel mode execution.
60 The three major uses of
61 .Nm
62 can be enumerated as follows:
63 .Bl -enum -offset indent
64 .It
65 from within
66 .Xr sleep 9 , and
67 .Xr tsleep 9
68 when the current process
69 voluntarily relinquishes the CPU to wait for some resource to become
70 available.
71 .It
72 after handling a trap
73 .Pq e.g. a system call, device interrupt
74 when the kernel prepares a return to user-mode execution.
75 This case is
76 typically handled by machine dependent trap-handling code after detection
77 of a change in the signal disposition of the current process, or when a
78 higher priority process might be available to run.
79 The latter event is
80 communicated by the machine independent scheduling routines by calling
81 the machine defined
82 .Fn need_resched .
83 .It
84 in the signal handling code
85 .Pq see Xr issignal 9
86 if a signal is delivered that causes a process to stop.
87 .El
88 .Pp
89 .Fn mi_switch
90 records the amount of time the current process has been running in the
91 process structure and checks this value against the CPU time limits
92 allocated to the process
93 .Pq see Xr getrlimit 2 .
94 Exceeding the soft limit results in a
95 .Dv SIGXCPU
96 signal to be posted to the process, while exceeding the hard limit will
97 cause a
98 .Dv SIGKILL .
99 After these administrative tasks are done,
100 .Fn mi_switch
101 hands over control to the machine dependent routine
102 .Fn cpu_switch ,
103 which will perform the actual process context switch.
104 .Pp
105 .Fn cpu_switch
106 will make a choice amongst the processes which are ready to run from a
107 priority queue data-structure.
108 The priority queue consists of an array
109 .Va qs[NQS]
110 of queue header structures each of which identifies a list of runnable
111 processes of equal priority
112 .Pq see Fa <sys/proc.h> .
113 A single word
114 .Va whichqs
115 containing a bit mask identifying non-empty queues assists in selecting
116 a process quickly.
117 .Fn cpu_switch
118 must remove the first process from the list on the queue
119 with the highest priority
120 .Po lower indices in Va qs
121 indicate higher priority
122 .Pc ,
123 and assign the address of its process structure to the global variable
124 .Dv curproc .
125 If no processes are available on the run queues,
126 .Fn cpu_switch
127 shall go into an
128 .Dq idle
129 loop.
130 The idle loop must allow interrupts to be taken that will eventually
131 cause processes to appear again on the run queues.
132 The variable
133 .Va curproc
134 should be
135 .Dv NULL
136 while
137 .Fn cpu_switch
138 waits for this to happen.
139 .Pp
140 Note that
141 .Fn mi_switch
142 and thus
143 .Fn cpu_switch
144 should be called at splhigh().
145 .Pp
146 .Sh SEE ALSO
147 .Xr issignal 9 ,
148 .Xr spl 9 ,
149 .Xr tsleep 9 ,
150 .Xr wakeup 9
151 .Pp