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