source: rtems/doc/user/schedule.t @ 13fb305

4.104.114.84.95
Last change on this file since 13fb305 was 13fb305, checked in by Joel Sherrill <joel.sherrill@…>, on 10/06/99 at 17:50:07

Enabled images. They appear to work ok for PostScript? but not as well
for PDF -- at least according to ghostview.

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