490 likes | 657 Views
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.
E N D
ECE 424 Embedded Systems Design Operating System Overview Chapter 7 NingWeng
Operating System • Abstractions • Uninterrupted Computation: No Interrupts • Infinite Memory: just an illusion. • Simple I/O: Avoid dealing directly withdevices (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 ECE 424
Service Call Design Patterns ECE 424
Tasks & Threads & Process ECE 424
RTOS States Transitions ECE 424
Nonpreemptive FIFO Scheduling ECE 424
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
Fragmented Heap ECE 424
Power of two heap ECE 424
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 ECE 424
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 ECE 424
Mutual Exclusion/Synchronization • Goal: to serialized atomic access to share resources ECE 424
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 Process P2 . . chin = getchar(); . . chin = getchar(); chout = chin; chout = chin; putchar(chout); . . 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 MachineInstructions • 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 ECE 424
Semaphore • Binary • Couting ECE 424
Execution Environment • Program • Libraries • Kernel subsystems • Hardware
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. ECE 424
Service, Driver and Device ECE 424
Scatter Gather Structures ECE 424
Direct Memory Access (DMA) • What, why and how/ ECE 424
Transmit Descriptor Ring ECE 424
Network Stack and Device Driver ECE 424
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 ECE 424
Files Open and Read ECE 424
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 ECE 424
Brownout Events ECE 424
Real Time ECE 424
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 ECE 424
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