ppt

advertisement
ECE 424
Embedded Systems
Design
Operating System Overview
Chapter 7
Ning Weng
Operating System
• Abstractions
─ Uninterrupted Computation: No Interrupts
─ Infinite Memory: just an illusion.
─ Simple I/O: Avoid dealing directly with devices (simple writes/reads)
Uninterrupted computation
• Underlying mechanisms



Context switching
Scheduling
Protection
• Flavors of “process” - increasing complexity

Interrupt handlers, threads, processes
Infinite memory via virtual memory
•
Via virtual memory
Page mapping (avoids finding contiguous locations).

Demand paging (use more space than memory)
Slow DRAM lookup avoided with fast TLB
Protection by allowing only OS to modify page tables.

•
•
Simple I/O using system calls
•
•
•
•
For abstraction alone, I/O could be libraries.
For security, I/O handled by device drivers.
System calls, trap to kernel protection levels.
More expensive function call, because of privilege
escalation.
Four Types of Operating Systems
•
•
•
•
Single-user, single task - Designed to manage the computer so that
one user can effectively do one thing at a time. Ex: Apple iPhone
Single-user, multi-tasking - Type of operating system most use on
desktop and laptop computers today. Windows 7 and the MacOSX are
examples of OS that let a single user have several programs in
operation at the same time.
Multi-user - Allows many users to obtain the computer's resources
simultaneously. OS must make sure that each program being used has
sufficient and separate resources so that a problem with one user
doesn't affect the other users. Ex: Unix
Real-time operating system (RTOS) - Main task is to manage
computer’s resources so a particular operation executes in precisely
the same amount of time every time it occurs.
Embedded Operating Systems
Characteristics
• Designed to perform a dedicated function
• Real-Time Operating Systems (RTOS)
• Examples:
─ iOS, Android, Blackberry OS
─ VxWorks (Boeing 787 Dreamliner and many
spacecraft)
Service Calls
Ning Weng
ECE 424
8
Service Call Design Patterns
Ning Weng
ECE 424
9
Tasks & Threads & Process
Ning Weng
ECE 424
10
RTOS States Transitions
Ning Weng
ECE 424
11
Nonpreemptive FIFO Scheduling
Ning Weng
ECE 424
12
RR Scheduler with Priority & Preemption
Ning Weng
ECE 424
13
Memory Allocation
• Malloc() and Free()
─ Allocates and frees memory from system heap to an
application, respectively
• Problem with the allocation system:
─ Forgets sequence of memory and free allocations
• Can cause fragmentation (small contiguous
section + lots of free memory)
• Solution:
─ Allocator algorithm
• Maximize size of contiguous blocks over time
Fragmentation Example
Fragmented Heap
Ning Weng
ECE 424
16
Power of two heap
Ning Weng
ECE 424
17
Virtual Memory
• Memory Management Unit (MMU) manages
translation between code, data, and heap
process memory to physical memory
• Virtual addresses are unique to accessing
process
• Physical addresses are unique to the hardware
(i.e. ram)
Address Space Mapping
Ning Weng
ECE 424
19
Freeing and Swapping Memory
• Freeing Memory
─ Memory allocated by a task may not be automatically
freed upon deletion of that task
• Must keep track of all allocated memory
• Swapping Memory
─ Utilized in Linux to allocate more virtual memory to
applications than the total amount of physical memory
─ Rarely used in embedded systems
• Additional wear on system (normally based on
solid-state drives)
Clocks and Timers
• Synchronous Execution
─ Used to specify timeouts
• Sleep() and yield() are also used to delay execution
─ If time is long enough, task is de-scheduled and moved to ready
queue
• Asynchronous Execution
─ Callback functions – Indicates that a timeout has occurred to
other threads
• Callbacks with expiry time less than or equal to current time
count are called
• Can release semaphores
• Often called because of timer interrupt
– Interrupt – mechanism used to inform CPU that an
asynchronous event has occurred
Time of Today
• Time source
─ Real time Clock
• Hardware timer backup with battery
─ CPU when it is running
• Seed source
─ RTC
─ NTP (network time protocol)
─ Cellular radio network
Ning Weng
ECE 424
22
Mutual Exclusion/Synchronization
• Goal: to serialized atomic access to share resources
Ning Weng
ECE 424
23
Difficulties of
Concurrency
• Sharing of global resources
─ Writing a shared variable: the order of writes is important
─ Incomplete writes a major problem
• Optimally managing the allocation of resources
• Difficult to locate programming errors as results are not
deterministic and reproducible.
A Simple Example
void echo()
{
chin = getchar();
chout = chin;
putchar(chout);
}
A Simple Example:
On a Multiprocessor
Process P1
.
chin = getchar();
.
chout = chin;
putchar(chout);
.
.
Process P2
.
.
chin = getchar();
chout = chin;
.
putchar(chout);
.
Competition among
Processes for Resources
Three main control problems:
• Need for Mutual Exclusion
─ Critical sections
• Deadlock
• Starvation
Disabling Interrupts
• Uniprocessors only allow interleaving
• Interrupt Disabling
─ A process runs until it invokes an operating system service or
until it is interrupted
─ Disabling interrupts guarantees mutual exclusion
─ Will not work in multiprocessor architecture
Pseudo-Code
while (true) {
/*
/*
/*
/*
}
disable interrupts */;
critical section */;
enable interrupts */;
remainder */;
Special Machine
Instructions
• Compare&Swap Instruction
─ also called a “compare and exchange instruction”
• Exchange Instruction
Compare&Swap
Instruction
int compare_and_swap (int *word,
int testval, int newval)
{
int oldval;
oldval = *word;
if (oldval == testval) *word = newval;
return oldval;
}
Semaphore
• Semaphore:
─ An integer value used for signalling among processes.
• Only three operations may be performed on a
semaphore, all of which are atomic:
─ initialize,
─ Decrement (semWait)
─ increment. (semSignal)
OS Required Semaphore
Ning Weng
ECE 424
33
Semaphore
• Binary
• Couting
Ning Weng
ECE 424
34
Execution Environment
MATH
LIBRARY
APPLICATION (mpg123)
•
Program
•
Libraries
•
Kernel
subsystems
•
Hardware
STANDARD C
LIBRARY
Networking
Memory
Management
Filesystems
Device
Control
Process
Management
Character
Devices
Architecture
Dependent
Code
b
OPERATING
SYSTEM
Network
Subsystem
Memory
Manager
File System
Devices
Disk
Network Interfaces
Memory
CPU
35
Device Driver
• The driver controls the hardware and provides an
abstract interface to its capabilities.
• The driver ideally imposes no restrictions (or policy) on
how the hardware should be used by applications.
• Scatter gather list:
─ a mechanism defined and supported by OS to represent a list of
data is not physically contiguous.
Ning Weng
ECE 424
36
Service, Driver and Device
Ning Weng
ECE 424
37
Scatter Gather Structures
Ning Weng
ECE 424
38
Direct Memory Access (DMA)
• What, why and how/
Ning Weng
ECE 424
39
Transmit Descriptor Ring
Ning Weng
ECE 424
40
Network Stack and Device Driver
Ning Weng
ECE 424
41
Storage File System
• present logical (abstract) view
of files and directories
─ hide complexity of hardware
devices
• facilitate efficient use of
storage devices
─ optimize access, e.g., to disk
• support sharing
─ provide protection
Ning Weng
ECE 424
42
Files Open and Read
Ning Weng
ECE 424
43
Synchronization File System
• File write is asynchronous
─ Write call function does not block
• Data may not have been in disk
• Consolidating and scheduling writing requests for
performance
• Synchronization is required before
─ Shut down, restart or disk removal
─ Example: Stop a USB disk (safe remove)
• Two challenges of power interactions
─ Un-notified power removal
• Large capacitor and sensing circuits
─ Brownout
• Voltage drop
Ning Weng
ECE 424
44
Brownout Events
Ning Weng
ECE 424
45
Real Time
Ning Weng
ECE 424
46
Real Time
• Real time: required to complete its task on time
─ Usually have deterministic time bound
─ Hard or soft
• Features of RTOS
─ Scheduling, resource allocation, interrupt handing and etc
─ Example, VxWorks
Ning Weng
ECE 424
47
In a Hard RTOS
• Thread priorities can be set by the client
• Threads always run according to priority
• Kernel must be preemptible or bounded
• Interrupts must be bounded
• No virtual memory
In a Soft RTOS…
• Like a hard RTOS:
─ Priority scheduling, with no degradation
─ Low dispatch latency
─ Preemptible system calls
─ No virtual memory (or allow pages to be locked)
• Linux: guarantees about relative timing of tasks, no
guarantees about syscalls
Download