Скачать презентацию 5 -High-Performance Embedded Systems using Concurrent Process cont Скачать презентацию 5 -High-Performance Embedded Systems using Concurrent Process cont

bc87e2f482147f68491c2cb3c724ba9a.ppt

  • Количество слайдов: 18

5 -High-Performance Embedded Systems using Concurrent Process (cont. ) 1 5 -High-Performance Embedded Systems using Concurrent Process (cont. ) 1

Outline l Models vs. Languages l State Machine Model ¡ FSM/FSMD ¡ HCFSM and Outline l Models vs. Languages l State Machine Model ¡ FSM/FSMD ¡ HCFSM and Statecharts Language ¡ Program-State Machine (PSM) Model l Concurrent Process Model ¡ Communication ¡ Synchronization ¡ Implementation l Dataflow Model l Real-Time Operating Systems 2

Synchronization among processes l Sometimes concurrently running processes must synchronize their execution ¡ When Synchronization among processes l Sometimes concurrently running processes must synchronize their execution ¡ When a process must wait for: l another process to compute some value l reach a known point in their execution l signal some condition l Recall producer-consumer problem ¡ process. A must wait if buffer is full ¡ process. B must wait if buffer is empty ¡ This is called busy-waiting l Process executing loops instead of being blocked l CPU time wasted l More efficient methods ¡ Join operation, and blocking send and receive discussed earlier l Both block the process so it doesn’t waste CPU time ¡ Condition variables and monitors 3

Condition variables l Condition variable is an object that has 2 operations, signal and Condition variables l Condition variable is an object that has 2 operations, signal and wait l When process performs a wait on a condition variable, the process is blocked until another process performs a signal on the same condition variable l How is this done? ¡ Process A acquires lock on a mutex ¡ Process A performs wait, passing this mutex l Causes mutex to be unlocked ¡ Process B can now acquire lock on same mutex ¡ Process B enters critical section l Computes some value and/or make condition true ¡ Process B performs signal when condition true l Causes process A to implicitly reacquire mutex lock l Process A becomes runnable 4

Condition variable example: consumer-producer l 2 condition variables ¡ Textbook: buffer_empty renamed to not_full Condition variable example: consumer-producer l 2 condition variables ¡ Textbook: buffer_empty renamed to not_full l Signals at least 1 free location available in buffer ¡ Textbook: buffer_full renamed to not_empty l Signals at least 1 valid data item in buffer l process. A: ¡ ¡ produces data item acquires lock (cs_mutex) for critical section checks value of count if count = N, buffer is full l performs wait operation on one_spot_empty l this releases the lock on cs_mutex allowing process. B to enter critical section, consume data item and free location in buffer l process. B then performs signal ¡ if count < N, buffer is not full l process. A inserts data into buffer l increments count l signals process. B making it runnable if it has performed a wait operation on not_empty Consumer-producer using condition variables 01: data_type buffer[N]; 02: int count = 0; 03: mutex cs_mutex; 04: condition not_full, not_empty; 06: void process. A() { 07: int i; 08: while( 1 ) { 09: produce(&data); 10: cs_mutex. lock(); 11: if( count == N ) not_full. wait(cs_mutex); 13: buffer[i] = data; 14: i = (i + 1) % N; 15: count = count + 1; 16: cs_mutex. unlock(); 17: not_empty. signal(); 18: } 19: } 20: void process. B() { 21: int i; 22: while( 1 ) { 23: cs_mutex. lock(); 24: if( count == 0 ) not_empty. wait(cs_mutex); 26: data = buffer[i]; 27: i = (i + 1) % N; 28: count = count - 1; 29: cs_mutex. unlock(); 30: not_full. signal(); 31: consume(&data); //Note: only one signal in this process. Fix textbook typo. 32: } 33: } 34: void main() { 35: create_process(process. A); create_process(process. B); 37: } 5

Monitors l l Collection of data and methods or subroutines that operate on data Monitors l l Collection of data and methods or subroutines that operate on data similar to an object-oriented paradigm Monitor guarantees only 1 process can execute inside monitor at a time Monitor DATA l (a) Process X executes while Process Y has to wait l (c) Process Y signals condition Process X waiting on ¡ Process Y blocked ¡ Process X allowed to continue executing DATA (b) Process X performs wait on a condition ¡ Process Y allowed to enter and execute l Waiting l CODE Process X CODE Process Y Process X Process Y (a) Monitor (d) Process X finishes executing in monitor or waits on a condition again ¡ Process Y made runnable again (b) Monitor DATA Waiting DATA CODE Process X CODE Process Y (c) Process X (d) Process Y 6

Monitor example: consumer-producer l l l Single monitor encapsulates both processes along with buffer Monitor example: consumer-producer l l l Single monitor encapsulates both processes along with buffer and count One process will be allowed to begin executing first If process. B allowed to execute first ¡ ¡ ¡ ¡ Will execute until it finds count = 0 Will perform wait on buffer_full condition variable process. A now allowed to enter monitor and execute process. A produces data item finds count < N so writes to buffer and increments count process. A performs signal on buffer_full condition variable process. A blocked process. B reenters monitor and continues execution, consumes data, etc. 01: 02: 03: 04: 06: 07: 08: 09: 10: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 35: Monitor { data_type buffer[N]; int count = 0; condition buffer_full, condition buffer_empty; void process. A() { int i; while( 1 ) { produce(&data); if( count == N ) buffer_empty. wait(); buffer[i] = data; i = (i + 1) % N; count = count + 1; buffer_full. signal(); } } void process. B() { int i; while( 1 ) { if( count == 0 ) buffer_full. wait(); data = buffer[i]; i = (i + 1) % N; count = count - 1; buffer_empty. signal(); consume(&data); buffer_full. signal(); } } } /* end monitor */ void main() { create_process(process. A); create_process(process. B); } 7

Implementation l Mapping of system’s functionality onto hardware processors: ¡ captured using computational model(s) Implementation l Mapping of system’s functionality onto hardware processors: ¡ captured using computational model(s) ¡ written in some language(s) l l l Implementation choice independent from language(s) choice Implementation choice based on power, size, performance, timing and cost requirements Final implementation tested for feasibility State machine Pascal Sequent. program C/C++ Dataflow Java Concurrent processes VHDL ¡ Also serves as blueprint/prototype for mass manufacturing of final product Implementation A Implementation B Implementation C The choice of computational model(s) is based on whether it allows the designer to describe the system. The choice of language(s) is based on whether it captures the computational model(s) used by the designer. The choice of implementation is based on whether it meets power, size, performance and cost requirements. 8

Concurrent process model: implementation Can use single and/or general-purpose processors (a) Multiple processors, each Concurrent process model: implementation Can use single and/or general-purpose processors (a) Multiple processors, each executing one process ¡ l Processor A Process 1 Process 2 (a) Most processes don’t use 100% of processor time Can share processor time and still achieve necessary execution rates (c) Combination of (a) and (b) ¡ Process 3 Process 4 (b) One general-purpose processor running all processes ¡ ¡ l True multitasking (parallel processing) General-purpose processors l Use programming language like C and compile to instructions of processor l Expensive and in most cases not necessary Custom single-purpose processors l More common Communication Bus ¡ ¡ Processor B Processor C Processor D Process 1 Process 2 (b) Process 3 General Purpose Processor Process 4 Multiple processes run on one general-purpose processor while one or more processes run on own single_purpose processor Processor A Communication Bus l l Process 1 Process 2 (c) Process 3 Process 4 General Purpose Processor 9

Implementation: multiple processes sharing single processor l Can manually rewrite processes as a single Implementation: multiple processes sharing single processor l Can manually rewrite processes as a single sequential program ¡ ¡ ¡ l Can use multitasking operating system ¡ ¡ l Ok for simple examples, but extremely difficult for complex examples Automated techniques have evolved but not common E. g. , simple Hello World concurrent program from before would look like: I = 1; T = 0; while (1) { Delay(I); T = T + 1; if T modulo X is 0 then call Print. Hello. World, i. e. T is a multiple of X if T modulo Y is 0 then call Print. How. Are. You, i. e. T is a multiple of Y } Much more common Operating system schedules processes, allocates storage, and interfaces to peripherals, etc. Real-time operating system (RTOS) can guarantee execution rate constraints are met Describe concurrent processes with languages having built-in processes (Java, Ada, etc. ) or a sequential programming language with library support for concurrent processes (C, C++, etc. using POSIX threads for example) Can convert processes to sequential program with process scheduling right in code ¡ ¡ Less overhead (no operating system) More complex/harder to maintain 10

Processes vs. threads l Different meanings when operating system terminology l Regular processes ¡ Processes vs. threads l Different meanings when operating system terminology l Regular processes ¡ Heavyweight process ¡ Own virtual address space (stack, data, code) ¡ System resources (e. g. , open files) l Threads ¡ ¡ Lightweight process Subprocess within process Only program counter, stack, and registers Shares address space, system resources with other threads l Allows quicker communication between threads ¡ Small compared to heavyweight processes l Can be created quickly l Low cost switching between threads 11

Implementation: suspending, resuming, and joining l Multiple processes mapped to single-purpose processors ¡ ¡ Implementation: suspending, resuming, and joining l Multiple processes mapped to single-purpose processors ¡ ¡ Hardware pins needed (e. g. suspend, done pins) Built into processor’s implementation Could be extra input signal that is asserted when process suspended Additional logic needed for determining process completion l Extra output signals indicating process done l Multiple processes mapped to single general-purpose processor ¡ Scheduling needed ¡ Built into programming language or special multitasking library like POSIX ¡ Language or library may rely on operating system to handle 12

Implementation: Scheduling l Must meet timing requirements when multiple concurrent processes implemented on single Implementation: Scheduling l Must meet timing requirements when multiple concurrent processes implemented on single general-purpose processor ¡ Not true multitasking l Scheduler ¡ Special process that decides when and for how long each process is executed ¡ Implemented as preemptive or nonpreemptive scheduler ¡ Preemptive l Determines how long a process executes before preempting to allow another process to execute • Time quantum: predetermined amount of execution time preemptive scheduler allows each process (may be 10 to 100 s of milliseconds long) l Determines which process will be next to run ¡ Nonpreemptive l Only determines which process is next after current process finishes execution 13

Scheduling: priority l Process with highest priority always selected first by scheduler ¡ Typically Scheduling: priority l Process with highest priority always selected first by scheduler ¡ Typically determined statically during creation and dynamically during execution l FIFO ¡ Runnable processes added to end of FIFO as created or become runnable ¡ Front process removed from FIFO when time quantum of current process is up or process is blocked l Priority queue ¡ Runnable processes again added as created or become runnable ¡ Process with highest priority chosen when new process needed ¡ If multiple processes with same highest priority value then selects from them using first-come first-served ¡ Called priority scheduling when nonpreemptive ¡ Called round-robin when preemptive 14

Priority assignment l Period of process ¡ ¡ l Execution deadline ¡ l Amount Priority assignment l Period of process ¡ ¡ l Execution deadline ¡ l Amount of time process must be completed by after it has started l E. g. , execution time = 5 ms, deadline = 20 ms, period = 100 ms l Process must complete execution within 20 ms after it has begun regardless of its period l Process begins at start of period, runs for 4 ms then is preempted l Process suspended for 14 ms, then runs for the remaining 1 ms l Completed within 4 + 1 = 19 ms which meets deadline of 20 ms l Without deadline process could be suspended for much longer Rate monotonic scheduling ¡ ¡ l Smallest repeating time interval within which the process must be called for execution l E. g. , period = 100 ms l Process must execute once every 100 ms Usually determined by the description of the system l E. g. , refresh rate of display is 27 times/sec l Period = 37 ms Processes with shorter periods have higher priority Typically used when execution deadline = period Deadline monotonic scheduling ¡ ¡ Processes with shorter deadlines have higher priority Typically used when execution deadline < period Rate monotonic Process Period Priority A B C D E F 25 ms 50 ms 12 ms 100 ms 40 ms 75 ms 5 3 6 1 4 2 Deadline monotonic Process Deadline Priority G H I J K L 17 ms 50 ms 32 ms 10 ms 140 ms 32 ms 5 2 3 6 1 4 15

Real-time operating systems l Systems composed of 2 or more cooperating, concurrent processes with Real-time operating systems l Systems composed of 2 or more cooperating, concurrent processes with stringent execution time constraints ¡ E. g. , set-top boxes have separate processes that read or decode video and/or sound concurrently and must decode 20 frames/sec for output to appear continuous ¡ Other examples with stringent time constraints are: l digital cell phones l navigation and process control systems l assembly line monitoring systems l multimedia and networking systems l etc. ¡ Communication and synchronization between processes for these systems is critical ¡ Therefore, concurrent process model best suited for describing these systems 16

Real-time operating systems (RTOS) l l Provide mechanisms, primitives, and guidelines for building real-time Real-time operating systems (RTOS) l l Provide mechanisms, primitives, and guidelines for building real-time embedded systems Windows CE ¡ ¡ ¡ l Built specifically for embedded systems and appliance market Scalable real-time 32 -bit platform Supports Windows API Perfect for systems designed to interface with Internet Preemptive priority scheduling with 256 priority levels per process Kernel is 400 Kbytes QNX ¡ ¡ Real-time microkernel surrounded by optional processes (resource managers) that provide POSIX and UNIX compatibility l Microkernels typically support only the most basic services l Optional resource managers allow scalability from small ROM-based systems to huge multiprocessor systems connected by various networking and communication technologies Preemptive process scheduling using FIFO, round-robin, adaptive, or priority-driven scheduling 32 priority levels per process Microkernel < 10 Kbytes and complies with POSIX real-time standard 17

Summary l Computation models are distinct from languages l Sequential program model is popular Summary l Computation models are distinct from languages l Sequential program model is popular ¡ Most common languages like C support it directly l State machine models good for control ¡ Extensions like HCFSM provide additional power ¡ PSM combines state machines and sequential programs l Concurrent process model for multi-task systems ¡ Communication and synchronization methods exist ¡ Scheduling is critical 18