Embedded RTOS Tasks

Download as pdf or txt
Download as pdf or txt
You are on page 1of 42

CS512 Embedded RTOS

Embedded RTOS Tasks


C.-Z. Yang http://syslab.cse.yzu.edu.tw/~czyang

YZUCSE SYSLAB

Real-time embedded applications


Simple software applications are typically designed to run sequently. However, this scheme is inappropriate for real-time embedded applications. These applications generally handle multiple inputs and outputs within tight time constraints.
They must be designed for concurrency.

YZUCSE SYSLAB [email protected]

CS512 Embedded RTOS

Definition of Tasks
A task is an independent thread of execution that can compete with other concurrent tasks for processor execution time. A task is schedulable.

YZUCSE SYSLAB [email protected]

CS512 Embedded RTOS

A task and its associated data structures

YZUCSE SYSLAB [email protected]

CS512 Embedded RTOS

System tasks
Initialization or startup task Idle task Logging task Exception-handling task Debug agent task

YZUCSE SYSLAB [email protected]

CS512 Embedded RTOS

Task states

YZUCSE SYSLAB [email protected]

CS512 Embedded RTOS

Ready state
In this state, the task actively competes with all other ready tasks for the processors execution time. The kernels scheduler uses the priority of each task to determine which task to move to the running state.

YZUCSE SYSLAB [email protected]

CS512 Embedded RTOS

An example (255=lowest, 0=highest)

YZUCSE SYSLAB [email protected]

CS512 Embedded RTOS

Running state
On a single-processor system, only one task can run at a time. When a task is preempted by a higher priority task, it moves to the ready state. It also can move to the blocked state.
Making a call that requests an unavailable resource Making a call that requests to wait for an event to occur Making a call to delay the task for some duration
CS512 Embedded RTOS

YZUCSE SYSLAB [email protected]

Blocked state
CPU starvation occurs when higher priority tasks use all of the CPU execution time and lower priority tasks do not get to run. The cases when blocking conditions are met
A semaphore token for which a task is waiting is released A message, on which the task is waiting, arrives in a message queue A time delay imposed on the task expires

YZUCSE SYSLAB [email protected]

CS512 Embedded RTOS

10

Typical task operations


Creating and deleting tasks Controlling task scheduling Obtaining task information

YZUCSE SYSLAB [email protected]

CS512 Embedded RTOS

11

Task creation and deletion


Two common approaches
Two system calls: first to create and then to move to the ready state One systems calls: All done in a system call

User-configurable hooks
A mechanism to execute programmer-supplied functions at the time of specific kernel events

Premature deletion
May get memory or resource leaks

YZUCSE SYSLAB [email protected]

CS512 Embedded RTOS

12

Task scheduling
The scheduling can be handled automatically. Many kernels also provide a set of API calls that allows developers to control the state changes.
Manual scheduling

YZUCSE SYSLAB [email protected]

CS512 Embedded RTOS

13

Task Scheduling
Scheduling:
Select the most deserving process to run based on system-specific policy

Also called CPU scheduling


Since tasks complete for the CPU execution

All most RTOSes support the preemptive

priority-based scheduling algorithm


CS512 Embedded RTOS

YZUCSE SYSLAB [email protected]

14

Policy Considerations
Policy can control/influence
CPU utilization Average time a process waits for service Average amount of time to complete a job

Could strive for any of the following



YZUCSE SYSLAB [email protected]

Equitability Favor very short or long jobs Meet priority requirements Meet deadlines
CS512 Embedded RTOS 15

When the Scheduler is Invoked?


The scheduler is run from several points within the kernel
Run after putting the current process onto a wait queue (when wait for some events) The current process becomes blocked Run at the end of a system call Just before a process is returned to user mode from system mode When the system timer has just set the current processes counter to zero. The current process run out of its time quota Process exits The current process exits Run when a more higher priority becomes ready The current process is preempted

YZUCSE SYSLAB [email protected]

CS512 Embedded RTOS

16

Well-known Task Scheduling Algorithms in RTOSes


Priority-based scheduling algorithm Priority-based round-robin scheduling algorithm EDF (Earliest-Deadline-First) scheduling algorithm RM (Rate-Monotonic) scheduling algorithm
YZUCSE SYSLAB [email protected]

CS512 Embedded RTOS

17

Context Switch (1/2)


Context switch
Also called task switch or process switch Occurred when a scheduler switches from one task to another

Although each process can have its own address space, all processes have to share

the CPU registers

Kernel ensure that each such register is loaded with the value it had when the process was suspended
CS512 Embedded RTOS 18

YZUCSE SYSLAB [email protected]

Context Switch (2/2)


Thus, each task has its own context
running
The state of the CPU registers required for tasks When a task running, its context is highly dynamic The context of a task is stored in its process descriptor

Operations
Save the context of the current process Load the context of new process If have page table, update the page table entries Flush those TLB entries that belonged to the old process

YZUCSE SYSLAB [email protected]

CS512 Embedded RTOS

19

Operations for task scheduling

YZUCSE SYSLAB [email protected]

CS512 Embedded RTOS

20

Task scheduling operations


Suspend and Resume
For debugging purposes Suspend a high-priority task so that lower priority task can execute

Delay a task
Allow manual scheduling Wait for an external condition that does not have an associated interrupt
Polling: wait up after a set time to check a specified condition or event had occurred

YZUCSE SYSLAB [email protected]

CS512 Embedded RTOS

21

Task scheduling operations


Restart
Begin the task as if it had not been previously executing Useful during debugging or when initializing a task after a catastrophic error

Get and Set Priority


Control task scheduling manually Helpful during a priority inversion

Preemption lock
A pairs of calls used to disable and enable preemption in applications Useful if a task is in a critical section of code

YZUCSE SYSLAB [email protected]

CS512 Embedded RTOS

22

Obtaining Task Information


Useful for debugging and monitoring Get ID: obtain a tasks ID Get TCB: obtain a tasks TCB by its ID
Only a snapshot of the task context

YZUCSE SYSLAB [email protected]

CS512 Embedded RTOS

23

Typical task structure


Two kinds of tasks
Run to completion Endless loop

YZUCSE SYSLAB [email protected]

CS512 Embedded RTOS

24

CS512 Embedded RTOS

Processes in Linux

YZUCSE SYSLAB

Process Descriptor in Linux


task_struct data structure
Process State Scheduling Information Inter-Process Communication Times and Timers Tty

Fs

tty_struct: keep track of tty associated with the process fs_struct: keep track of current directory files_struct: pointers to file descriptors mm_struct: pointer to memory area signal_struct: record signals recived
CS512 Embedded RTOS 26

Files

Virtual memory Signal

YZUCSE SYSLAB [email protected]

Process States in Linux


Process states in Linux is stored in state field of task_struct, i.e., process descriptor Running
TASK_RUNNING The process is either running (it is the current process executing a CPU) or waiting to be executed TASK_INTERRUPTIBLE
waiting processes can be interrupted by signals

Suspended/Blocked

TASK_UNINTERRUPIBLE
waiting processes are waiting directly on hardware conditions and cannot be interrupted by signals
YZUCSE SYSLAB [email protected]

CS512 Embedded RTOS

27

Process States in Linux


Stopped
TASK_STOPPED
The process receives an appropriate signal (SIGSTOP, SIGSTP, SIGTTIN or SIGTTOU) Or when the process is being monitored (debugged) by another process using the ptrace system call and has passed control to the monitoring process

Zombie

TASK_ZOMBIE Process execution is terminated, but the parent process has not yet issued a wait()-like system call
YZUCSE SYSLAB [email protected]

CS512 Embedded RTOS

28

Kernel Threads in Linux


System processes in Linux run only in Kernel Mode Kernel thread
Called Kernel thread Process 0 Process 1 keventd: executes the tasks in the qt_context task queue kapm: handles the events related to the APM (Advanced Power Management) kswapd: perform memory reclaiming kflushd (or bdflush): flush dirty buffers to disk to reclaim memory kupdated: flush old dirty buffers to disk to reduce risks of filesystem inconsistencies ksoftirqd: runs the tasklets
CS512 Embedded RTOS 29

YZUCSE SYSLAB [email protected]

Process 0
The ancestor of all processes, also called

swapper process

Created from scratch during the initialization phase by the start_kernel() function Execute the cpu_idle() function after having created the init process Process 0 is selected by the scheduler only when there are no TASK_RUNNING tasks
YZUCSE SYSLAB [email protected]

CS512 Embedded RTOS

30

Process 1
Also called init process Created by process 0 by calling the kernel_thread() function Process 1 creates and monitors the activity of all processes that implement the outer layer of the operating system
For example, routinely issue wait() system to get ride of all zombie processes
CS512 Embedded RTOS

Since it executes the init() function that completes the initialization of the kernel

YZUCSE SYSLAB [email protected]

31

Process Creation in Linux (1/6)


Unix relies on process creation to satisfy user requests

The shell creates a new process to execute the user request

Traditional Unix duplicated resources owned by the parent to child

Requires copy the entire address space of the parent However, child may not need so much resources, especially child issues an immediate execve()
CS512 Embedded RTOS 32

YZUCSE SYSLAB [email protected]

Process Creation in Linux (2/6)


Modern Unix kernel solves this by
Copy-on-Write: when either one tries to write on a physical page, the kernel then allocates a new page frame Lightweight process: all parent and child to share many per-process kernel DS vfork() system call: create a process that shares the memory address space of its parent

YZUCSE SYSLAB [email protected]

CS512 Embedded RTOS

33

Process Creation in Linux (3/6)


Lightweight processes are created in Linux by using clone() with four parameters.
fn: the function to be executed arg: points to data passed to the fn() flags
The low bye specifies the signal number to be sent to the parent when child terminate Remaining three types encode a group of flags

child_stack: specifies the User Mode stack pointer.


If 0, share with parent until Copy_on_Write is invoked However, much non-null if share the same address space as parent

YZUCSE SYSLAB [email protected]

CS512 Embedded RTOS

34

Process Creation in Linux (4/6)


Flags
CLONE_VM: share the memory descriptor and all Page Tables CLONE_FS:
Share the table that identifies the root and current working directories Share the bitmap (called umask) to make the initial file permission of a new file

CLONE_FILES: share the table that identifies the open files CLONE_PARENT: Set the parent of the child to the parent of the calling process CLONE_PID: share the PID (only used by Process 0) and used for a multiprocessor system CLONE_PTRACE: a parent is ptraced, also the child
YZUCSE SYSLAB [email protected]

CS512 Embedded RTOS

35

Process Creation in Linux (5/6)


CLONE_SIGHAND: share the table that identifies the signal handlers CLONE_THREAD: insert the child into the same thread group of the parent CLONE_SIGNAL: CLONE_SIGHAND + CLONE_THREAD
Send a signal to all threads of a multithreaded application

CLONE_VFORK: used by vfork() system call

YZUCSE SYSLAB [email protected]

CS512 Embedded RTOS

36

Process Creation in Linux (6/6)


Clone() is a wrapper function defined in C library that in turn uses a clone() system call
Clone() system call only have flags and child_stack parameters

Fork() system call is implemented by a clone() system call with


flags specifies SIGCHID and other are clear child_stack is 0

Vfork() is implemented by a clone() system call with

flags = a SIGCHID signal + CLONE_VM + CLONE_VFORK child_stack is 0


CS512 Embedded RTOS 37

YZUCSE SYSLAB [email protected]

Process Deletion in Linux (1/4)


Destroying Processes
Invoke exit() library function
Release the resources allocated by C library Invoke the _exit() system call

Kernel may force a process to die


Process received a specific signal Unrecoverable CPU exception

Both of above approaches invokes the do_exit() function


YZUCSE SYSLAB [email protected]

CS512 Embedded RTOS

38

Process Deletion in Linux (2/4)


Process termination is handled by the do_exit() function
PF_EXITING

Set the flag filed of the process descriptor as Remove, if necessary, the process descriptor from an IPC semaphore queue Remove, if necessary, the process descriptor from a dynamic timer queue Examine the processs data structure related to paging, filesystem, open file descriptors, and signal handling and remove each of them if no other processes are sharing them

YZUCSE SYSLAB [email protected]

CS512 Embedded RTOS

39

Process Deletion in Linux (3/4)


Decrement the resource counter of the modules used by the process Set the exit_code of the process descriptor to the process termination code Invoke the exit_notify() function to update the parenthood relationships of both the parent and child process
Equal to either the _exit() system call parameter or error code supplied by the kernel

Invoke the schedule() function to select a new process to run


YZUCSE SYSLAB [email protected]

All child processes becomes the children of another process or of the init process Set the state field of the process descriptor to TASK_ZOMBIE

CS512 Embedded RTOS

40

Process Deletion in Linux (4/4)


In Unix, a process can query kernel
Obtain the PID of its parent process The execution state of its children
When the child has terminated, its termination code tells the parent (via wait()-like system call ) if it has been carried out successfully

Thus, kernel are not allowed to discard data included in a process descriptor field right after the process termination

Must be saved until the parent process is notified Why the introduction of TASK_ZOMBIE state
CS512 Embedded RTOS 41

YZUCSE SYSLAB [email protected]

References
Qing Li and Caroline Yao, Real-Time Concepts for Embedded Systems, CMP Books, ISBN: 1-57820-124-1, 2003 D. P. Bovet and M. Cesati, Understanding the Linux Kernel, 2nd edition,, O'Reilly & Associates, 2002. Jean J. Labrosse, "MicroC OS II: The Real Time Kernel," ISBN: 1578201039, CMP Books, June 15, 2002.
YZUCSE SYSLAB [email protected]

CS512 Embedded RTOS

42

You might also like