Real-Time Operating Systems - Download Now PowerPoint
Document Sample


Real-Time Operating Systems
Read Chapter 6
(David E. Simon, An Embedded Software Primer)
Read Chapter 1/ Chapter 2 (Labrosse)
What, Why, How?
• What does real time mean?
• What is an OS?
• Why would I need an OS?
• Why would I need a real time OS?
• How do we use an OS on embedded
systems?
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 2
Is This a Real Time System?
• Consider the following case:
– A human operator must regulate the temperature of a
system with a +/- 5 degrees F
– The temperature adjusts the current intensity to regulate
the temperature.
– The oven has a huge inertia: its temperature varies by at
most 2 degrees per hour
– The union imposes 2 coffee breaks a day
– The coffee break was negotiated to last one hour!
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 3
Is This a Real Time System? (2)
• Consider the following case:
– Consider a system that can achieve 10 Mflops
– We want to use this machine for 24-hour
weather prediction.
– The atmosphere is respectively divided in
longitude and latitude in areas of 200 miles X
200 miles. Measures are made at different
altitudes
– It takes 100 billion floating operations to solve
the Navier-Stockes equations.
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 4
Is This a Real Time System? (3)
• Consider the following case:
– Consider the problem on previous slide.
– The resolution of 200 miles is too coarse
– Suppose we double the resolution in 4
dimensions.
– Volume of data increases 16 fold, so does
execution time
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 5
What Is an O.S?
• A piece of software
• It provides tools to manage (for embedded
systems)
– Processes, (or tasks)
– Memory space
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 6
What Is an Operating System?
• What? It is a program (software) that acts as
an intermediary between a user of a
computer and the computer hardware.
• Why? Make the use of a computer
CONVENIENT and EFFICIENT.
• How?
• We have one semester to answer this
question.
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 7
What Is an Operating System?
For an Embedded System
• Provides software tools for a convenient
and prioritized control of tasks.
• Provides tools for task (process)
synchronization.
• Provides a simple memory management
system
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 8
Convenient and Efficient?
(General Purpose)
• Convenient :
– Simplicity of use
– Messy or complicated details are hidden
– User is unaware that he is using an OS
• Efficient
– Resources are optimally used
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 9
Abstract View of A System
(General Purpose)
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 10
Layered View of a Computer System
Services
Requests
• A computer system consists of
– hardware
– system programs (Blurry frontier)
– application programs
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 11
Abstract View of A System
(Embedded System)
Application
OS
Hardware
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 12
Operating System Definitions
(General Purpose)
• Resource allocator – manages and
allocates resources
• Control program – controls the
execution of user programs and
operations of I/O devices
• Kernel – the one program running at all
times (all else being application
programs)
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 13
Multiprogramming
CPU
• Objective: Better control of tasks
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 14
Dual-Mode Operation
• Sharing system resources requires operating
system to ensure that an incorrect program or
poorly behaving human cannot cause other
programs to execute incorrectly
• OS must provide hardware support to differentiate
between at least two modes of operations
1. User mode – execution done on behalf of a user
2. Monitor mode (also kernel mode or system mode) –
execution done on behalf of operating system
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 15
Dual-Mode Operation (Cont.)
• Mode bit added to computer hardware to
indicate the current mode: monitor (0) or
user (1)
• When an interrupt or fault occurs hardware
switches to monitor mode
Interrupt/fault
kernel user
set user mode
Privileged instructions can be issued only in kernel mode
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 16
Process/Task Concept
• An operating system executes a variety
of programs:
– Batch system – jobs
– Time-shared systems – user programs or
tasks
• Similar terms job, process, task (ES)
almost interchangeably
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 17
Process/Task Concept (Cont.)
• Process – a program in execution; process
execution must progress in sequential
fashion
• A process includes:
– program counter
– stack
– data section
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 18
Example of Processes
The Process Model
PC
CPU
• Multiprogramming of four programs
• Conceptual model of 4 independent, sequential processes
• Only one program active at any instant
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 19
Multitasking
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 20
Key Difference on Embedded
Systems
• General Purpose OS: processes may be
initiated by:
– Different users
– Different application
• Embedded system: tasks are part of a
unique application
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 21
Process/Task State
• Bottom line: keep track of which
process/task runs (has the CPU)
• As a process executes, it changes state (in
some states, process/task does need CPU)
– new: The process is being created
– running: Instructions are being executed
– waiting: The process is waiting for some event
to occur
– ready: The process is waiting to be assigned to
a process
– terminated: The process has finished execution
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 22
Diagram of Process State
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 23
Diagram of Task State
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 24
Process Control Block (PCB)
Information associated with each process
• Process state
• Program counter
• CPU registers
• CPU scheduling information
• Memory-management information
• Accounting information
• I/O status information
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 25
Process Control Block (PCB)
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 26
CPU Switch From Process to Process:
Context Switch
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 27
Ready Queue And Various I/O Device Queues
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 28
CPU Scheduler
• What? This is a prime OS function that decides
which process/task will run
• Why? The application is made of multiple
processes/tasks with possibly different time
constraints. CPU scheduler implements this
prioritization
• How? On embedded systems, priorities assigned
by the programmer are the main criteria
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 29
Scheduler
• Non preemptive (kernel): the process/task
relinquishes VOLUNTARILY the CPU.
• Preemptive (kernel): OS preempts the
CPU from a task and takes control of the
CPU after a scheduled event or an
interrupt .
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 30
Example:
(http://www.netrino.com/Publications/Glossary/RMA.html)
• P1 = 50ms, C1= 25ms (CPU uti. = 50%)
• P2 = 100ms, C2= 40ms (CPU uti. = 40%)
• Looks like total CPU utilization is less than 100%:
can these tasks be scheduled real time? (not
missing their deadlines, next cycle start)
• How to schedule these and not miss deadlines?
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 31
Example (cont’d):
(http://www.netrino.com/Publications/Glossary/RMA.html)
• Case 1: Priority(Task1) > Priority(Task2)
• Case 2: Priority(Task2) > Priority(Task1)
• P1 = 50ms, C1= 25ms (CPU uti. = 50%)
• P2 = 100ms, C2= 40ms (CPU uti. = 40%)
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 32
Question:
• If I assign priorities such that most frequent
tasks get highest priorities, does my
scheduling GUARANTEE meeting the
deadlines?
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 33
Answer: Rate Monotonic
Scheduling (RMS)
• Given
– m periodic events
– event i occurs within period Pi and requires at most Ci
seconds
– Tasks are independent (do not have to synchronize)
– Tasks are prioritized based on highest rate. Highest
priority goes first
– Preemptive scheduling is used
• Then the load can m only be handled if and only if
i
P m(2
C (1/m) -1)
i =1 i
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 34
RMS: Example
• Let a system with 3 tasks T1, T2, and T3:
• T1 requires 3 ms every 10ms
• T2 requires 5 ms every 15 ms
• T3 requires 1 ms every 5 ms
• Can these tasks be scheduled in real time?
(Deadline is next time cycle)
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 35
If a Task Can Be Interrupted..
• We get problems:
– Shared data problem
– Non reentrant functions
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 36
Reentrant Function
• A function F is reentrant if it can be invoked
by multiple processes/tasks that can execute
F CORRECTLY and concurrently.
• In other words, F is correctly used by
multiple processes/tasks at the same time.
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 37
Reentrant Function (2)
• In general, if a function modifies only local
variables (usually stored on stack), it is reentrant.
• Example: non reentrant function
int temp; /* global variable */
void swap(int *x, int *y){
temp = *x;
*x = *y;
*y = temp;
}
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 38
Dealing With Non Reentrant F.
• Modify functions to make them reentrant
• If not possible, treat them as critical regions
(sections) and enforce mutual exclusion
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 39
Enforcing Mutual Exclusion
• Disable/Enable interrupts:
– OS_ENTER_CRITICAL()
– OS_EXIT_CRITICAL()
• Using Test-and-Set function
• Disable/Enable scheduling:
– OSSchedLock()
– OSSchedUnlock()
• Using semaphores
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 40
Hardware Support (1)
• Implementation of Test-And-Set Instruction
– Test-and Set must be an atomic operation (either
completely executed or not executed at all)
Boolean Test-and-Set(int *lock){
int register; Test-and-Set atomically:
register = *lock; 1) Sets lock to 1
2) Tests whether the lock was open (0)
lock = 1;
return(register = = 0);
}
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 41
Mutual Exclusion Using Test-And-Set
System variables: Lock = 0;
while (1){ while (1){
While (!Test-And-Set(&Lock)); While (!Test-And-Set(&Lock));
Critical Region Critical Region
Lock = 0; Lock = 0;
Remainder Remainder
} /* end while (1) */ } /* end while (1) */
Mutual Exclusion ? Yes
Bounded Waiting ? Yes
Progress ? Yes
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 42
Semaphore (Cont’d)
• Down function: (Wait)
if (S <= 0)
sleep();
S = S – 1;
• Up function: (Signal)
S = S + 1;
wake up some process
Mutexes are binary semaphores
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 43
Mutual Exclusion Using Semaphores mC
System variables: Lock = 0;
while (1){ while (1){
OSSemPend(SharedDataSem,0,&err); OSSemPend(SharedDataSem,0,&err);
Critical Region Critical Region
OSSemPost(SharedDataSem); OSSemPost(SharedDataSem);
Remainder Remainder
} /* end while (1) */ } /* end while (1) */
Mutual Exclusion ? Yes
Bounded Waiting ? Yes
Progress ? Yes
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 44
Semaphores for Scheduling:
unilateral rendezvous
• Consider a task that waits for some event
that can be generated by another task or
some interrupt.
– Solution 1: use a flag variable
– Solution 2: use a semaphore
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 45
Semaphores for Scheduling:
unilateral rendezvous (2)
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 46
Semaphores Scheduling:
Startup Barrier
• Consider two tasks that must start some
work at the same time.
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 47
Intertask Communication
(IPC !)
Process/Task Communication
• Necessary and useful for multi-tasking
systems and/or in a distributed system
• Design and implementation issues :
– How to establish links among processes ?
– Link’s connectivity : how processes are linked ?
How many of them should be linked
– Link’s capability: how many messages can be
passed ?
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 49
Link’s Connectivity (How to link?)
• Direct : each process that wants to send or receive a
message must explicitely name the recipient or sender or
sender of the communication
Send(P,message) Receive(Q,message)
Example of Producer/Consumer Problem
Process Producer Process Consumer
Repeat Repeat
…….. receive(producer,nextp);
Produce an item in nextp; ……
…….. consume the item in nextp
Send(consumer,nextp); ……..
Until false Until false
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 50
Link’s Connectivity (How to link?)
• Indirect :
– messages are sent to and received from mailboxes (ports).
Send(A,message) Receive(A,message)
– Each mailbox has a unique id.
– Two processes may communicate only if they have a shared
mailbox.
– A mailbox may be owned by a process or by the system
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 51
Link’s capability
• Zero capability
• Bounded capability
• Unbounded capability
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 52
Semantics of send
• Normal situation
– The sending process continues (its execution)
• Asynchronous communication
– The sending process suspends until an
acknowledgement comes back :
• Remote procedure call
• Synchronous communication
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 53
Semantics of send
(Know what the OS offers!)
• Abnormal situations
– What happens if a link (say, a mailbox) is full
when the process want to send a message
• Delay the process until mailbox has room
• The sending process continues as if the message has
been sent (the current message is lost)
• The sending process continues while the current
message replaces one of the old messages in the
mailbox
• The sending process terminates (run-time error!)
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 54
Semantics of send
• Abnormal situations (2)
– What happens if the receiver/mailbox does not
exist when a process sends a message to it ?
• The sending process continues as the message has
been sent. The current message is lost
• The sending process terminates (run-time error!)
– What happens if a message is lost by the system?
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 55
Semantics of receive
• Normal situations
• Abnormal situations
– What happens if a link (say, a mailbox) is empty
when a process wants to receive a message from
it ?
• The receiving process is delayed until the mailbox has
some message
• The receiving process receives a nil message
• The receiving process receives a copy of an old
message which was in the mailbox
• The receiving process terminates (run-time error!)
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 56
Semantics of receive
• Abnormal situations (2)
– What happens if the sender/mailbox does not
exist when a process wants to receive a message
from it ?
• The receiving process receives a nil message
• The receiving process terminates (run-time error!)
The semantics design is an open-ended problem.
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 57
Unix IPC
• Pipes
– Circular buffer
– Similar to a file buffer, but without a physical
file
• Message queues
• Shared memory
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 58
mC IPC
• Semaphores
• Mailbox
• Message queues
Embedded Systems Development Software Architectures S.Biaz (12/5/2011) 59
Get documents about "