1 |
1026 |
ivang |
@c
|
2 |
|
|
@c COPYRIGHT (c) 1988-2002.
|
3 |
|
|
@c On-Line Applications Research Corporation (OAR).
|
4 |
|
|
@c All rights reserved.
|
5 |
|
|
@c
|
6 |
|
|
@c schedule.t,v 1.13 2002/01/17 21:47:47 joel Exp
|
7 |
|
|
@c
|
8 |
|
|
|
9 |
|
|
@c
|
10 |
|
|
@c This figure is not included:
|
11 |
|
|
@c Figure 17-1 RTEMS Task State Transitions
|
12 |
|
|
@c
|
13 |
|
|
|
14 |
|
|
@chapter Scheduling Concepts
|
15 |
|
|
|
16 |
|
|
@cindex scheduling
|
17 |
|
|
@cindex task scheduling
|
18 |
|
|
|
19 |
|
|
@section Introduction
|
20 |
|
|
|
21 |
|
|
The concept of scheduling in real-time systems
|
22 |
|
|
dictates the ability to provide immediate response to specific
|
23 |
|
|
external events, particularly the necessity of scheduling tasks
|
24 |
|
|
to run within a specified time limit after the occurrence of an
|
25 |
|
|
event. For example, software embedded in life-support systems
|
26 |
|
|
used to monitor hospital patients must take instant action if a
|
27 |
|
|
change in the patient's status is detected.
|
28 |
|
|
|
29 |
|
|
The component of RTEMS responsible for providing this
|
30 |
|
|
capability is appropriately called the scheduler. The
|
31 |
|
|
scheduler's sole purpose is to allocate the all important
|
32 |
|
|
resource of processor time to the various tasks competing for
|
33 |
|
|
attention. The RTEMS scheduler allocates the processor using a
|
34 |
|
|
priority-based, preemptive algorithm augmented to provide
|
35 |
|
|
round-robin characteristics within individual priority groups.
|
36 |
|
|
The goal of this algorithm is to guarantee that the task which
|
37 |
|
|
is executing on the processor at any point in time is the one
|
38 |
|
|
with the highest priority among all tasks in the ready state.
|
39 |
|
|
|
40 |
|
|
There are two common methods of accomplishing the
|
41 |
|
|
mechanics of this algorithm. Both ways involve a list or chain
|
42 |
|
|
of tasks in the ready state. One method is to randomly place
|
43 |
|
|
tasks in the ready chain forcing the scheduler to scan the
|
44 |
|
|
entire chain to determine which task receives the processor.
|
45 |
|
|
The other method is to schedule the task by placing it in the
|
46 |
|
|
proper place on the ready chain based on the designated
|
47 |
|
|
scheduling criteria at the time it enters the ready state.
|
48 |
|
|
Thus, when the processor is free, the first task on the ready
|
49 |
|
|
chain is allocated the processor. RTEMS schedules tasks using
|
50 |
|
|
the second method to guarantee faster response times to external
|
51 |
|
|
events.
|
52 |
|
|
|
53 |
|
|
@section Scheduling Mechanisms
|
54 |
|
|
|
55 |
|
|
@cindex scheduling mechanisms
|
56 |
|
|
|
57 |
|
|
RTEMS provides four mechanisms which allow the user
|
58 |
|
|
to impact the task scheduling process:
|
59 |
|
|
|
60 |
|
|
@itemize @bullet
|
61 |
|
|
@item user-selectable task priority level
|
62 |
|
|
@item task preemption control
|
63 |
|
|
@item task timeslicing control
|
64 |
|
|
@item manual round-robin selection
|
65 |
|
|
@end itemize
|
66 |
|
|
|
67 |
|
|
Each of these methods provides a powerful capability
|
68 |
|
|
to customize sets of tasks to satisfy the unique and particular
|
69 |
|
|
requirements encountered in custom real-time applications.
|
70 |
|
|
Although each mechanism operates independently, there is a
|
71 |
|
|
precedence relationship which governs the effects of scheduling
|
72 |
|
|
modifications. The evaluation order for scheduling
|
73 |
|
|
characteristics is always priority, preemption mode, and
|
74 |
|
|
timeslicing. When reading the descriptions of timeslicing and
|
75 |
|
|
manual round-robin it is important to keep in mind that
|
76 |
|
|
preemption (if enabled) of a task by higher priority tasks will
|
77 |
|
|
occur as required, overriding the other factors presented in the
|
78 |
|
|
description.
|
79 |
|
|
|
80 |
|
|
@subsection Task Priority and Scheduling
|
81 |
|
|
|
82 |
|
|
@cindex task priority
|
83 |
|
|
|
84 |
|
|
The most significant of these mechanisms is the
|
85 |
|
|
ability for the user to assign a priority level to each
|
86 |
|
|
individual task when it is created and to alter a task's
|
87 |
|
|
priority at run-time. RTEMS provides 255 priority levels.
|
88 |
|
|
Level 255 is the lowest priority and level 1 is the highest.
|
89 |
|
|
When a task is added to the ready chain, it is placed behind all
|
90 |
|
|
other tasks of the same priority. This rule provides a
|
91 |
|
|
round-robin within priority group scheduling characteristic.
|
92 |
|
|
This means that in a group of equal priority tasks, tasks will
|
93 |
|
|
execute in the order they become ready or FIFO order. Even
|
94 |
|
|
though there are ways to manipulate and adjust task priorities,
|
95 |
|
|
the most important rule to remember is:
|
96 |
|
|
|
97 |
|
|
@itemize @code{ }
|
98 |
|
|
@item @b{The RTEMS scheduler will always select the highest
|
99 |
|
|
priority task that is ready to run when allocating the processor
|
100 |
|
|
to a task.}
|
101 |
|
|
@end itemize
|
102 |
|
|
|
103 |
|
|
@subsection Preemption
|
104 |
|
|
|
105 |
|
|
@cindex preemption
|
106 |
|
|
|
107 |
|
|
Another way the user can alter the basic scheduling
|
108 |
|
|
algorithm is by manipulating the preemption mode flag
|
109 |
|
|
(@code{@value{RPREFIX}PREEMPT_MASK}) of individual tasks. If preemption is disabled
|
110 |
|
|
for a task (@code{@value{RPREFIX}NO_PREEMPT}), then the task will not relinquish
|
111 |
|
|
control of the processor until it terminates, blocks, or
|
112 |
|
|
re-enables preemption. Even tasks which become ready to run and
|
113 |
|
|
possess higher priority levels will not be allowed to execute.
|
114 |
|
|
Note that the preemption setting has no effect on the manner in
|
115 |
|
|
which a task is scheduled. It only applies once a task has
|
116 |
|
|
control of the processor.
|
117 |
|
|
|
118 |
|
|
@subsection Timeslicing
|
119 |
|
|
|
120 |
|
|
@cindex timeslicing
|
121 |
|
|
@cindex round robin scheduling
|
122 |
|
|
|
123 |
|
|
Timeslicing or round-robin scheduling is an
|
124 |
|
|
additional method which can be used to alter the basic
|
125 |
|
|
scheduling algorithm. Like preemption, timeslicing is specified
|
126 |
|
|
on a task by task basis using the timeslicing mode flag
|
127 |
|
|
(@code{@value{RPREFIX}TIMESLICE_MASK}). If timeslicing is enabled for a task
|
128 |
|
|
(@code{@value{RPREFIX}TIMESLICE}), then RTEMS will limit the amount of time the task
|
129 |
|
|
can execute before the processor is allocated to another task.
|
130 |
|
|
Each tick of the real-time clock reduces the currently running
|
131 |
|
|
task's timeslice. When the execution time equals the timeslice,
|
132 |
|
|
RTEMS will dispatch another task of the same priority to
|
133 |
|
|
execute. If there are no other tasks of the same priority ready
|
134 |
|
|
to execute, then the current task is allocated an additional
|
135 |
|
|
timeslice and continues to run. Remember that a higher priority
|
136 |
|
|
task will preempt the task (unless preemption is disabled) as
|
137 |
|
|
soon as it is ready to run, even if the task has not used up its
|
138 |
|
|
entire timeslice.
|
139 |
|
|
|
140 |
|
|
@subsection Manual Round-Robin
|
141 |
|
|
|
142 |
|
|
@cindex manual round robin
|
143 |
|
|
|
144 |
|
|
The final mechanism for altering the RTEMS scheduling
|
145 |
|
|
algorithm is called manual round-robin. Manual round-robin is
|
146 |
|
|
invoked by using the @code{@value{DIRPREFIX}task_wake_after}
|
147 |
|
|
directive with a time interval of @code{@value{RPREFIX}YIELD_PROCESSOR}.
|
148 |
|
|
This allows a task to give up the
|
149 |
|
|
processor and be immediately returned to the ready chain at the
|
150 |
|
|
end of its priority group. If no other tasks of the same
|
151 |
|
|
priority are ready to run, then the task does not lose control
|
152 |
|
|
of the processor.
|
153 |
|
|
|
154 |
|
|
@subsection Dispatching Tasks
|
155 |
|
|
|
156 |
|
|
@cindex dispatching
|
157 |
|
|
|
158 |
|
|
The dispatcher is the RTEMS component responsible for
|
159 |
|
|
allocating the processor to a ready task. In order to allocate
|
160 |
|
|
the processor to one task, it must be deallocated or retrieved
|
161 |
|
|
from the task currently using it. This involves a concept
|
162 |
|
|
called a context switch. To perform a context switch, the
|
163 |
|
|
dispatcher saves the context of the current task and restores
|
164 |
|
|
the context of the task which has been allocated to the
|
165 |
|
|
processor. Saving and restoring a task's context is the
|
166 |
|
|
storing/loading of all the essential information about a task to
|
167 |
|
|
enable it to continue execution without any effects of the
|
168 |
|
|
interruption. For example, the contents of a task's register
|
169 |
|
|
set must be the same when it is given the processor as they were
|
170 |
|
|
when it was taken away. All of the information that must be
|
171 |
|
|
saved or restored for a context switch is located either in the
|
172 |
|
|
TCB or on the task's stacks.
|
173 |
|
|
|
174 |
|
|
Tasks that utilize a numeric coprocessor and are
|
175 |
|
|
created with the @code{@value{RPREFIX}FLOATING_POINT} attribute
|
176 |
|
|
require additional operations during a context switch. These
|
177 |
|
|
additional operations
|
178 |
|
|
are necessary to save and restore the floating point context of
|
179 |
|
|
@code{@value{RPREFIX}FLOATING_POINT} tasks. To avoid unnecessary save and restore
|
180 |
|
|
operations, the state of the numeric coprocessor is only saved
|
181 |
|
|
when a @code{@value{RPREFIX}FLOATING_POINT} task is dispatched and that task was not
|
182 |
|
|
the last task to utilize the coprocessor.
|
183 |
|
|
|
184 |
|
|
@section Task State Transitions
|
185 |
|
|
|
186 |
|
|
@cindex task state transitions
|
187 |
|
|
|
188 |
|
|
Tasks in an RTEMS system must always be in one of the
|
189 |
|
|
five allowable task states. These states are: executing, ready,
|
190 |
|
|
blocked, dormant, and non-existent.
|
191 |
|
|
|
192 |
|
|
A task occupies the non-existent state before a
|
193 |
|
|
@code{@value{DIRPREFIX}task_create} has been
|
194 |
|
|
issued on its behalf. A task enters the
|
195 |
|
|
non-existent state from any other state in the system when it is
|
196 |
|
|
deleted with the @code{@value{DIRPREFIX}task_delete}
|
197 |
|
|
directive. While a task occupies
|
198 |
|
|
this state it does not have a TCB or a task ID assigned to it;
|
199 |
|
|
therefore, no other tasks in the system may reference this task.
|
200 |
|
|
|
201 |
|
|
When a task is created via the @code{@value{DIRPREFIX}task_create} directive
|
202 |
|
|
it enters the dormant state. This state is not entered through
|
203 |
|
|
any other means. Although the task exists in the system, it
|
204 |
|
|
cannot actively compete for system resources. It will remain in
|
205 |
|
|
the dormant state until it is started via the @code{@value{DIRPREFIX}task_start}
|
206 |
|
|
directive, at which time it enters the ready state. The task is
|
207 |
|
|
now permitted to be scheduled for the processor and to compete
|
208 |
|
|
for other system resources.
|
209 |
|
|
|
210 |
|
|
@ifset use-ascii
|
211 |
|
|
@example
|
212 |
|
|
@group
|
213 |
|
|
+-------------------------------------------------------------+
|
214 |
|
|
| Non-existent |
|
215 |
|
|
| +-------------------------------------------------------+ |
|
216 |
|
|
| | | |
|
217 |
|
|
| | | |
|
218 |
|
|
| | Creating +---------+ Deleting | |
|
219 |
|
|
| | -------------------> | Dormant | -------------------> | |
|
220 |
|
|
| | +---------+ | |
|
221 |
|
|
| | | | |
|
222 |
|
|
| | Starting | | |
|
223 |
|
|
| | | | |
|
224 |
|
|
| | V Deleting | |
|
225 |
|
|
| | +-------> +-------+ -------------------> | |
|
226 |
|
|
| | Yielding / +----- | Ready | ------+ | |
|
227 |
|
|
| | / / +-------+ <--+ \ | |
|
228 |
|
|
| | / / \ \ Blocking | |
|
229 |
|
|
| | / / Dispatching Readying \ \ | |
|
230 |
|
|
| | / V \ V | |
|
231 |
|
|
| | +-----------+ Blocking +---------+ | |
|
232 |
|
|
| | | Executing | --------------> | Blocked | | |
|
233 |
|
|
| | +-----------+ +---------+ | |
|
234 |
|
|
| | | |
|
235 |
|
|
| | | |
|
236 |
|
|
| +-------------------------------------------------------+ |
|
237 |
|
|
| Non-existent |
|
238 |
|
|
+-------------------------------------------------------------+
|
239 |
|
|
@end group
|
240 |
|
|
@end example
|
241 |
|
|
@end ifset
|
242 |
|
|
|
243 |
|
|
@ifset use-tex
|
244 |
|
|
@c @page
|
245 |
|
|
@example
|
246 |
|
|
@image{states,,3in}
|
247 |
|
|
@c @group
|
248 |
|
|
@c +-------------------------------------------------------------+
|
249 |
|
|
@c | Non-existent |
|
250 |
|
|
@c | +-------------------------------------------------------+ |
|
251 |
|
|
@c | | | |
|
252 |
|
|
@c | | | |
|
253 |
|
|
@c | | Creating +---------+ Deleting | |
|
254 |
|
|
@c | | -------------------> | Dormant | -------------------> | |
|
255 |
|
|
@c | | +---------+ | |
|
256 |
|
|
@c | | | | |
|
257 |
|
|
@c | | Starting | | |
|
258 |
|
|
@c | | | | |
|
259 |
|
|
@c | | V Deleting | |
|
260 |
|
|
@c | | +-------> +-------+ -------------------> | |
|
261 |
|
|
@c | | Yielding / +----- | Ready | ------+ | |
|
262 |
|
|
@c | | / / +-------+ <--+ \ | |
|
263 |
|
|
@c | | / / \ \ Blocking | |
|
264 |
|
|
@c | | / / Dispatching Readying \ \ | |
|
265 |
|
|
@c | | / V \ V | |
|
266 |
|
|
@c | | +-----------+ Blocking +---------+ | |
|
267 |
|
|
@c | | | Executing | --------------> | Blocked | | |
|
268 |
|
|
@c | | +-----------+ +---------+ | |
|
269 |
|
|
@c | | | |
|
270 |
|
|
@c | | | |
|
271 |
|
|
@c | +-------------------------------------------------------+ |
|
272 |
|
|
@c | Non-existent |
|
273 |
|
|
@c +-------------------------------------------------------------+
|
274 |
|
|
@c @end group
|
275 |
|
|
@end example
|
276 |
|
|
@end ifset
|
277 |
|
|
|
278 |
|
|
@ifset use-html
|
279 |
|
|
@html
|
280 |
|
|
|
281 |
|
|
@end html
|
282 |
|
|
@end ifset
|
283 |
|
|
|
284 |
|
|
A task occupies the blocked state whenever it is
|
285 |
|
|
unable to be scheduled to run. A running task may block itself
|
286 |
|
|
or be blocked by other tasks in the system. The running task
|
287 |
|
|
blocks itself through voluntary operations that cause the task
|
288 |
|
|
to wait. The only way a task can block a task other than itself
|
289 |
|
|
is with the @code{@value{DIRPREFIX}task_suspend} directive.
|
290 |
|
|
A task enters the blocked state due to any of the following conditions:
|
291 |
|
|
|
292 |
|
|
@itemize @bullet
|
293 |
|
|
@item A task issues a @code{@value{DIRPREFIX}task_suspend} directive
|
294 |
|
|
which blocks either itself or another task in the system.
|
295 |
|
|
|
296 |
|
|
@item The running task issues a @code{@value{DIRPREFIX}message_queue_receive}
|
297 |
|
|
directive with the wait option and the message queue is empty.
|
298 |
|
|
|
299 |
|
|
@item The running task issues an @code{@value{DIRPREFIX}event_receive}
|
300 |
|
|
directive with the wait option and the currently pending events do not
|
301 |
|
|
satisfy the request.
|
302 |
|
|
|
303 |
|
|
@item The running task issues a @code{@value{DIRPREFIX}semaphore_obtain}
|
304 |
|
|
directive with the wait option and the requested semaphore is unavailable.
|
305 |
|
|
|
306 |
|
|
@item The running task issues a @code{@value{DIRPREFIX}task_wake_after}
|
307 |
|
|
directive which blocks the task for the given time interval. If the time
|
308 |
|
|
interval specified is zero, the task yields the processor and
|
309 |
|
|
remains in the ready state.
|
310 |
|
|
|
311 |
|
|
@item The running task issues a @code{@value{DIRPREFIX}task_wake_when}
|
312 |
|
|
directive which blocks the task until the requested date and time arrives.
|
313 |
|
|
|
314 |
|
|
@item The running task issues a @code{@value{DIRPREFIX}region_get_segment}
|
315 |
|
|
directive with the wait option and there is not an available segment large
|
316 |
|
|
enough to satisfy the task's request.
|
317 |
|
|
|
318 |
|
|
@item The running task issues a @code{@value{DIRPREFIX}rate_monotonic_period}
|
319 |
|
|
directive and must wait for the specified rate monotonic period
|
320 |
|
|
to conclude.
|
321 |
|
|
@end itemize
|
322 |
|
|
|
323 |
|
|
A blocked task may also be suspended. Therefore,
|
324 |
|
|
both the suspension and the blocking condition must be removed
|
325 |
|
|
before the task becomes ready to run again.
|
326 |
|
|
|
327 |
|
|
A task occupies the ready state when it is able to be
|
328 |
|
|
scheduled to run, but currently does not have control of the
|
329 |
|
|
processor. Tasks of the same or higher priority will yield the
|
330 |
|
|
processor by either becoming blocked, completing their
|
331 |
|
|
timeslice, or being deleted. All tasks with the same priority
|
332 |
|
|
will execute in FIFO order. A task enters the ready state due
|
333 |
|
|
to any of the following conditions:
|
334 |
|
|
|
335 |
|
|
@itemize @bullet
|
336 |
|
|
|
337 |
|
|
@item A running task issues a @code{@value{DIRPREFIX}task_resume}
|
338 |
|
|
directive for a task that is suspended and the task is not blocked
|
339 |
|
|
waiting on any resource.
|
340 |
|
|
|
341 |
|
|
@item A running task issues a @code{@value{DIRPREFIX}message_queue_send},
|
342 |
|
|
@code{@value{DIRPREFIX}message_queue_broadcast}, or a
|
343 |
|
|
@code{@value{DIRPREFIX}message_queue_urgent} directive
|
344 |
|
|
which posts a message to the queue on which the blocked task is
|
345 |
|
|
waiting.
|
346 |
|
|
|
347 |
|
|
@item A running task issues an @code{@value{DIRPREFIX}event_send}
|
348 |
|
|
directive which sends an event condition to a task which is blocked
|
349 |
|
|
waiting on that event condition.
|
350 |
|
|
|
351 |
|
|
@item A running task issues a @code{@value{DIRPREFIX}semaphore_release}
|
352 |
|
|
directive which releases the semaphore on which the blocked task is
|
353 |
|
|
waiting.
|
354 |
|
|
|
355 |
|
|
@item A timeout interval expires for a task which was blocked
|
356 |
|
|
by a call to the @code{@value{DIRPREFIX}task_wake_after} directive.
|
357 |
|
|
|
358 |
|
|
@item A timeout period expires for a task which blocked by a
|
359 |
|
|
call to the @code{@value{DIRPREFIX}task_wake_when} directive.
|
360 |
|
|
|
361 |
|
|
@item A running task issues a @code{@value{DIRPREFIX}region_return_segment}
|
362 |
|
|
directive which releases a segment to the region on which the blocked task
|
363 |
|
|
is waiting and a resulting segment is large enough to satisfy
|
364 |
|
|
the task's request.
|
365 |
|
|
|
366 |
|
|
@item A rate monotonic period expires for a task which blocked
|
367 |
|
|
by a call to the @code{@value{DIRPREFIX}rate_monotonic_period} directive.
|
368 |
|
|
|
369 |
|
|
@item A timeout interval expires for a task which was blocked
|
370 |
|
|
waiting on a message, event, semaphore, or segment with a
|
371 |
|
|
timeout specified.
|
372 |
|
|
|
373 |
|
|
@item A running task issues a directive which deletes a
|
374 |
|
|
message queue, a semaphore, or a region on which the blocked
|
375 |
|
|
task is waiting.
|
376 |
|
|
|
377 |
|
|
@item A running task issues a @code{@value{DIRPREFIX}task_restart}
|
378 |
|
|
directive for the blocked task.
|
379 |
|
|
|
380 |
|
|
@item The running task, with its preemption mode enabled, may
|
381 |
|
|
be made ready by issuing any of the directives that may unblock
|
382 |
|
|
a task with a higher priority. This directive may be issued
|
383 |
|
|
from the running task itself or from an ISR.
|
384 |
|
|
|
385 |
|
|
A ready task occupies the executing state when it has
|
386 |
|
|
control of the CPU. A task enters the executing state due to
|
387 |
|
|
any of the following conditions:
|
388 |
|
|
|
389 |
|
|
@item The task is the highest priority ready task in the
|
390 |
|
|
system.
|
391 |
|
|
|
392 |
|
|
@item The running task blocks and the task is next in the
|
393 |
|
|
scheduling queue. The task may be of equal priority as in
|
394 |
|
|
round-robin scheduling or the task may possess the highest
|
395 |
|
|
priority of the remaining ready tasks.
|
396 |
|
|
|
397 |
|
|
@item The running task may reenable its preemption mode and a
|
398 |
|
|
task exists in the ready queue that has a higher priority than
|
399 |
|
|
the running task.
|
400 |
|
|
|
401 |
|
|
@item The running task lowers its own priority and another
|
402 |
|
|
task is of higher priority as a result.
|
403 |
|
|
|
404 |
|
|
@item The running task raises the priority of a task above its
|
405 |
|
|
own and the running task is in preemption mode.
|
406 |
|
|
|
407 |
|
|
@end itemize
|