Embedded Software Programming

Similar documents
Embedded Systems Programming

Scheduling Algorithm and Analysis

Tasks. Task Implementation and management

Chapter 13: I/O Systems

Chapter 13: I/O Systems. Chapter 13: I/O Systems. Objectives. I/O Hardware. A Typical PC Bus Structure. Device I/O Port Locations on PCs (partial)

What s An OS? Cyclic Executive. Interrupts. Advantages Simple implementation Low overhead Very predictable

Deadlocks. Thomas Plagemann. With slides from C. Griwodz, K. Li, A. Tanenbaum and M. van Steen

Device-Functionality Progression

Chapter 12: I/O Systems. I/O Hardware

Chapter 13: I/O Systems

Operating Systems: William Stallings. Starvation. Patricia Roy Manatee Community College, Venice, FL 2008, Prentice Hall

Module 12: I/O Systems

Chapter 13: I/O Systems. Operating System Concepts 9 th Edition

CSE 4/521 Introduction to Operating Systems. Lecture 24 I/O Systems (Overview, Application I/O Interface, Kernel I/O Subsystem) Summer 2018

Chapter 13: I/O Systems

Module 12: I/O Systems

Deadlocks. Tore Larsen. With slides from T. Plagemann, C. Griwodz, K. Li, A. Tanenbaum and M. van Steen

Chapter 13: I/O Systems

Chapter 12: I/O Systems

Chapter 13: I/O Systems

Chapter 12: I/O Systems. Operating System Concepts Essentials 8 th Edition

Concurrency: Deadlock and Starvation. Chapter 6

I/O Systems. Amir H. Payberah. Amirkabir University of Technology (Tehran Polytechnic)

More on Synchronization and Deadlock

Concurrency, Mutual Exclusion and Synchronization C H A P T E R 5

ECE519 Advanced Operating Systems

Input/Output Systems

Chapter 6 Concurrency: Deadlock and Starvation

Commercial Real-time Operating Systems An Introduction. Swaminathan Sivasubramanian Dependable Computing & Networking Laboratory

Last Class: Synchronization Problems. Need to hold multiple resources to perform task. CS377: Operating Systems. Real-world Examples

Concurrency: Deadlock and Starvation

Last Class: Monitors. Real-world Examples

Time Handling in Programming Language

What is the Race Condition? And what is its solution? What is a critical section? And what is the critical section problem?

Chapters 5 and 6 Concurrency

Chapter 13: I/O Systems

Embedded Systems Programming

Deadlock. Concurrency: Deadlock and Starvation. Reusable Resources

COS 318: Operating Systems. Deadlocks. Jaswinder Pal Singh Computer Science Department Princeton University

The Real-time Specification for Java

Semaphore. Originally called P() and V() wait (S) { while S <= 0 ; // no-op S--; } signal (S) { S++; }

Lecture 13 Input/Output (I/O) Systems (chapter 13)

OPERATING SYSTEMS. Prescribed Text Book. Operating System Principles, Seventh Edition. Abraham Silberschatz, Peter Baer Galvin and Greg Gagne

A Predictable RTOS. Mantis Cheng Department of Computer Science University of Victoria

AUTOBEST: A United AUTOSAR-OS And ARINC 653 Kernel. Alexander Züpke, Marc Bommert, Daniel Lohmann

CSE 4/521 Introduction to Operating Systems. Lecture 29 Windows 7 (History, Design Principles, System Components, Programmer Interface) Summer 2018

by I.-C. Lin, Dept. CS, NCTU. Textbook: Operating System Concepts 8ed CHAPTER 13: I/O SYSTEMS

CSC Operating Systems Spring Lecture - XII Midterm Review. Tevfik Ko!ar. Louisiana State University. March 4 th, 2008.

Embedded Systems. 6. Real-Time Operating Systems

Silberschatz and Galvin Chapter 12

Operating Systems. Deadlock. Lecturer: William Fornaciari Politecnico di Milano Homel.deib.polimi.

Multiprocessor and Real-Time Scheduling. Chapter 10

I/O Systems. 04/16/2007 CSCI 315 Operating Systems Design 1

Deadlock. A Bit More on Synchronization. The Deadlock Problem. Deadlock Characterization. Operating Systems 2/7/2005. CSC 256/456 - Spring

Concurrent activities in daily life. Real world exposed programs. Scheduling of programs. Tasks in engine system. Engine system

Ref: Chap 12. Secondary Storage and I/O Systems. Applied Operating System Concepts 12.1

The control of I/O devices is a major concern for OS designers

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

Real-Time Systems 1. Basic Concepts

Chapter 13: I/O Systems

Lecture 3: Concurrency & Tasking

Exam Review TexPoint fonts used in EMF.

Chapter 13: I/O Systems

Main Points of the Computer Organization and System Software Module

Embedded Systems. 5. Operating Systems. Lothar Thiele. Computer Engineering and Networks Laboratory

Techno India Batanagar Department of Computer Science & Engineering. Model Questions. Multiple Choice Questions:

Concurrency: Deadlock and Starvation. Chapter 6

QUESTION BANK UNIT I

Multimedia Systems 2011/2012

Process Management. Deadlock. Process Synchronization. Management Management. Starvation

CS450/550 Operating Systems

CS 571 Operating Systems. Midterm Review. Angelos Stavrou, George Mason University

Programming Languages for Real-Time Systems. LS 12, TU Dortmund

CS370 Operating Systems

What is Deadlock? Two or more entities need a resource to make progress, but will never get that resource. Examples from everyday life:

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy

2. Introduction to Software for Embedded Systems

Chapter 6: CPU Scheduling. Operating System Concepts 9 th Edition

Reference Model and Scheduling Policies for Real-Time Systems

IT 540 Operating Systems ECE519 Advanced Operating Systems

Deadlock Prevention. Deadlock can be prevented if 1 of the 4 conditions cannot hold

Module 1. Introduction:

CS370 Operating Systems Midterm Review

Operating Systems: Quiz2 December 15, Class: No. Name:

Operating Systems Comprehensive Exam. Spring Student ID # 3/16/2006

Lecture 3. Introduction to Real-Time kernels. Real-Time Systems

CSc33200: Operating Systems, CS-CCNY, Fall 2003 Jinzhong Niu December 10, Review

MARUTHI SCHOOL OF BANKING (MSB)

15: OS Scheduling and Buffering

UNIT:2. Process Management

Precedence Graphs Revisited (Again)

OPERATING SYSTEM CONCEPTS UNDERSTAND!!! IMPLEMENT!!! ANALYZE!!!

Midterm Exam Amy Murphy 19 March 2003

VI. Deadlocks. What is a Deadlock? Intended Schedule. Deadlock Problem (1) ???

VI. Deadlocks Operating Systems Prof. Dr. Marc H. Scholl DBIS U KN Summer Term

CS420: Operating Systems. Kernel I/O Subsystem

Midterm Exam. October 20th, Thursday NSC

Scheduling. The Basics

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

I/O AND DEVICE HANDLING Operating Systems Design Euiseong Seo

Transcription:

Embedded Software Programming Computer Science & Engineering Department Arizona State University Tempe, AZ 85287 Dr. Yann-Hang Lee yhlee@asu.edu (480) 727-7507

Event and Time-Driven Threads taskspawn (name, priority, options, stacksize, main, ) initialization Task initialization external trigger? start_time=time( ) ISR: to set/clear events Take actions and change system state computation Sleep(period - ( time( ) -start_time) ) 1

Multiple Events in One Thread void compute() { if (event1) then action1; if (event2) then action2; if (event3) then action3;. } void compute() { if (event1) then action1; else if (event2) then action2; else if (event3) then action3;. } or { } for (i=0, i<n, i++) if event[i] then action[i]; or { } for (i=0, i<n, i++) { if event[i] then { action[i]; break; } } 2

Real-time System Specification Logical correctness requirements: The computation produces correct outputs. Models of computation to describe inputs and computations Additional requirements on resource, security, reliability, etc. Finite state machine good for control logic and protocols, transition and activity Data flow modular computations that are triggered by the availability of input data. Temporal correctness requirements: The computation produces outputs at the right time When the computation can get started and should be completed 3

Specification Patterns Category Pattern Example Duration (stimuli and responses) Periodic Real-time order minimum duration maximum duration bounded recurrence bounded response bounded invariance The system has a minimum 'off' period of 120 seconds before it reenters the cranking mode. The system can only operate in engine cranking mode for no longer than 10 seconds at one time The ABS controller checks for wheel skidding every 10 milliseconds.") The detection of and response to rapid deceleration must occur within 0.015 seconds. If Error 502 is received, then the braking system is inhibited for 10 seconds. (S. Konrad and B.H.C. Cheng Real-time specification patterns, ICSE 2005) 4

RT Specification in FSM Duration of staying in a state Periodic activity in a state Bounded response for each transition Accumulated delay between multiple transitions Hierarchical FSM a state encloses a FSM enter a state activate a FSM Concurrent FSM FSMs run in parallel (active simultaneously) up idle down open 5

RT Specification in Data Flow The execution of actors is driven by data or clock Communication mechanism and delay End to end delay of each data stream delay at each stage scheduling of actors input stream navigation sensor processing integrated navigation guidance processing flight plan processing 6

Task model Periodic task T i : (examples??) constant (or bounded) period, p i : inter-release time between two consecutive jobs phase i, utilization i = e i / p i, deadline (relative) D i Aperiodic and sporadic: (examples??) uncertain interarrival times but with a minimum separation aperiodic: with a soft or no deadline sporadic: with a hard deadline e i D i i i +p i i +2p i i +3p i 7

Terminology: Temporal Parameters Release time: fixed ( r ), jitter [ r-, r+ ], sporadic or aperiodic Execution time: uncertainty from memory refresh, contention due to DMA, cache misses, interrupts, OS overhead execution path variations WCET: a deterministic parameter for the worstcase execution time a conservative measure an assumption to make scheduling and validation easier how can you measure the WCET of a job? 8

Terminology Hard deadline: late result is little or no value, or may lead to disaster need to validate (can you guarantee it?) Soft deadline: late result may still be useful probability of missing deadlines 95% of telephone switch connects in 10 seconds How serious is serious? Tardiness: min{ 0, deadline - completion time} Usefulness: function of tardiness value completion time 9

Task Functional Parameters Preemptivity: suspend the executing job and switch to the other one should a job (or a portion of job) be preemptable context switch: save the current process status (PC, registers, etc.) and initiate a ready job transmit a UDP package, write a block of data to disk, a busy waiting loop Preemptivity of resources: concurrent use of resources or critical section lock, semaphore, disable interrupts How can a context switch be triggered? Assume you want to preempt an executing job -- why a higher priority job arrives run out the time quantum waiting blocked wake-up executing ready suspended dispatched 10

Synchronous or Asynchronous (1) Synchronous: atomic reactions indexed by a global logical clock, each reaction computes new events for its outputs based on its internal state and on the input values the communication of all events between components occur synchronously during each reaction. Cycles of reading inputs, computing reaction and producing outputs Synchronous = 0-delay = within the same cycle No interference between I/O and computation Simulink? and other synchronous languages Why? deterministic semantics in the presence of concurrency. 11

Synchronous or Asynchronous (2) A_1 A_2 a high priority task B arrives and receives inputs from A (from A_1 or A_2?) A_1 A_2 C B B A_1 C A_2 C B if Pri(A) > Pri(C)>Pri(B), depending upon the execution time of C, B may receive inputs from A_1 or A_2 12

Synchronous or Asynchronous (3) If execution time = 0, then the computation is determined by the order of arrivals, not the arrival instances, nor execution time Can we memorize the arrival order and then fetch data from buffer A_1 A_2 C B B y_1 A_1 C A_2 C B y_1 y_2 13

Reader/Writer -- ISR and Buffering Input: single producer (ISR) and single consumer (thread) If a read is initialed by the thread calls read with a buffer of n bytes initiate IO operation, enable interrupt ISR reads input and store in the buffer. If done, single the completion Blocking or nonblocking in thread context (e.g. vxworks) semaphore, lock in kernel context (Linux) wait queue Guarded access Lock (mutex) and interrupt lock (disable) 14

Deadlock Permanent blocking of a set of threads that are competing for a set of resources. Necessary conditions for a deadlock Mutual exclusion: holds a resource in a non-sharable mode. Hold-and-wait: must hold at least one allocated resource while awaiting one or more resources held by other processes. No preemption: Resources not forcibly removed from a process holding it. Circular wait: a closed chain of processes exists, such that each process holds at least one resource needed by the next process in the chain. 15

Resource Allocation Graph If graph contains no cycles no deadlock. If graph contains a cycle if only one instance per resource type, then deadlock. if several instances per resource type, possibility of deadlock. (A. Silberschatz, G. Gagne, and P. B. Galvin, "Operating System Concepts ) 16

Deadlock Prevention Detect unsafe locking search resource allocation graph for a cycle Eliminate mutual exclusion: not possible in most cases spooling makes I/O devices sharable Eliminate hold-and-wait request all resources at once release all resources before a new request release all resources if current request blocks Eliminate circular wait order all resources: SEQ(Ri) SEQ(Rj) process must request in ascending order 17

Distributed Deadlock Prevention Resources that cause deadlock locks, guarded methods limited number of threads Based on wait-for graphs (Chandy-Misra-Haas algorithm). A process wishing to see if it is involved in a deadlock sends a probe message on all outgoing request arcs. The message is a triple (original blocked process, current sender, current destination). Any process receiving such a message passes it on on any outgoing arcs, with modified sender and receiver). If the message returns to the original sender then a cycle exists. 18

Overrun Management Imprecise computation trades off precision for timeliness during a transient overload. A task consists of two or more logical parts: a mandatory part and at least one optional part. The mandatory part must meet deadline constraint The optional part only affect the quality of result Implementation Synchronous approach: polling after each iteration of optional computation Separate tasks for mandatory and for optional parts The optional task (OT) sends results back to the mandatory task (MT) When running out the allocated execution time, MT puts out results and kill OT Asynchronous transfer of control (Exception) 19

Asynchronous Transfer of Control Communication between threads may be either synchronous or asynchronous. Asynchronous communication Resumption (through signals and signal handling) model and termination model If some change in the system environment needs immediate attention Time out on a computation Terminate a thread Terminate one loop of computation A controversial issue Difficult to write correct code Release resources Performance penalty RTSJ_set2_ 20 20

C and C++ Exceptions C does not define any exception handling facilities C++ exception: try, throw, and catch Cannot throw in signal handler To implement ATC model, it is necessary to save the status of a program's registers etc. on entry to an exception domain and then restore them if an exception occurs. The POSIX facilities of setjmp and longjmp can be used for this purpose Finalization: done by the programmer Language support of ATC: Ada and RTSJ 21

C++ exception throw and catch The function will throw DivideByZero as an exception caught by an exception-handling catch statement that catches exceptions of type int. The necessary construction is a try catch system. So, a program that checks for exceptions and may have exceptions thrown must be enclosed in a try block. (http://www.cprogramming.com/tutorial/exceptions.html) 22 22

setjmp and longjmp setjump saves the program status and returns a 0 longjmp restores the program status and results in the program abandoning its current execution and restarting from the position where setjump was called this time setjump returns the values passed by longjmp See the example in the next slide Program status is saved in a global variable jumper of type jum_buf. One may need different exception handlers in different functions. So how will SigHandler() know which jumper to use? 23

An Exception Handling in C /* The simplest error handling based on setjmp() and longjmp() */ jmp_buf jumper; void sighandler() { if (overrun) longjmp(jumper, -1); /* can't divide by 0 */ return; } void SomeTask (void) { int result; if (setjmp(jumper) == 0) { result = SomeComputation; /* continue working and save result */ } else put_out(result); /*overrun, send out the saved result */ } 24 24