430 likes | 538 Views
IT-606 Embedded Systems (Software). S. Ramesh Krithi Ramamritham Kavi Arya KReSIT/ IIT Bombay. Synchronous Models: Motivation S. Ramesh. Concurrency Models . ES are concurrent systems Environment and System evolve simultaneously
E N D
IT-606Embedded Systems(Software) S. Ramesh Krithi Ramamritham Kavi Arya KReSIT/ IIT Bombay
Concurrency Models • ES are concurrent systems • Environment and System evolve simultaneously • ES often decomposed into subsystems that evolve concurrently • Concurrent systems are more complex • Model of concurrency varied and often confused • Clear understanding essential
Concurrent Programs • A concurrent program consists of two or more processes • Each process is a sequential program • `threads' or `processes’ • Example: Cobegin x = 5 || y = 10 || z = 23 Coend
Concurrent Execution Concurrent execution involves, • Sequentially executing each process • Interleaved execution model • any process executed at any time • any number of instructions from a process • arbitrary interleaving • No assumption on relative speeds • This is a conceptual model • In reality, simultaneous execution possible • useful for analysis and understanding
Example execution Cobegin x = 5 || y = 10 || z = 23 Coend • x, y and z updated to 5, 10 and 23 respectively • Updation takes place in any order • Not necessarily the textual order • Final result independent of execution order • Not true, in general
Nondeterminism • Same input, different execution leads to different results, in general • Nondeterminism cobegin printf ("Hello world!\\n") || printf ("How are you?\\n") || printf ("What is your name?\\n") coend • What will be the result of execution?
Nondeterminism Some of the Possible results are: • Hello World! How are you? What is your name? • How are you? What is your name? Hello World! • What is your name? Hello World! How are you? Are there any other possibilities?
Another Example cobegin x = 0; x = x + 1; || x = 1; x = x + 1 coend What is the value of x at the end? • 1, 2 ? • 3? • Is 0 possible?
Interleaved Execution How different values possible? • Interleaving of steps of processes • What is a step? • A single statement in the language? • An instruction in the machine language? • It can be anything • Even reading / writing a bit / byte of data • Depends upon the machine
Speed Independence Result of concurrent execution • Depends upon relative speeds of processes (Race Condition) • But this is undesirable, in general • Write programs that compute the same irrespective of relative speed of execution
Non Deterministic Programs • There are cases, when we do not mind indeterminacy • Server responses to client processes • Order of printing of user files • Abstract models (recall lift controller example)
Speed Independence • How to reconcile determinism with concurrency? • Classical approach (discussed earlier) • Synchronous approach ( now)
Classical Approach • Independent Processes • Too restrictive • Communicating Processes • Shared variables and synchronization mechanisms • Test and Set primitives, Semaphores, Monitors • Message Passing • No sharing of variables • Send and receive primitives • Some kind of synchronization mechanism
Atomicity Problem with shared variables concurrency is: • Programmer does not know what the steps are? • Steps would depend upon various factors: machines, schedulers, OS, load etc. • For deterministic behaviour, programmer should specify these steps and execution mechanism ensures that
Atomicity (contd.) • Synchronization mechanisms enable specify these steps • These steps are called atomic steps • No sub-steps - no interleaving • Test-set, semaphores and monitors are high level specification of atomic steps
Atomic steps • Here is another variation - One of the simplest • Programs indicate atomic actions • Specified atomicity can include one or more statements Example: Cobegin < x = 0; % <…> indicates atomic action x = x + 1 > || < x = 0 >; < x = x + 1> Coend What will be the value of x at the end?
Problems with concurrency • Programs will still be non-deterministic! • Due to interleaving of atomic actions from different processes • Careful use of shared variables essential • Programs can be deadlocking. Eg.: P1::P(x); P(y); S1; V(y); V(x) || P2::P(y); P(x); S2; V(x); V(y) • P1 waits for P2, P2 waits for P1 • No progress, circular wait • Deadlock, a new kind of error
Starvation Consider the following problem: y = 1; x = 0 cobegin while (y > 0) {x = x + 1} || y = 0 coend • Will the program terminate ?
Starvation Example • Need not terminate • First process can keep executing • Terminates in practice • Fairness in selection of processes
Conspiracy cobegin P1:: while (x > 0) { await (y =1) do y = 0; S1; y = 1 } || P2:: while (x > 0) { await (y =1) do y = 0; S2; y = 1 } || P3:: while (x > 0) { await (y =1) do y = 0; S3; y = 1 } coend
Message Passing Concurrency Processes • do not share variable • share channels instead • channels carry messages • send and receive actions • asynchronous communication
Message Passing Concurrency • handshake communication • receiver waits till sender sends a message • sender waits when the channel is full • guarded wait actions • Languages like CSP, Promela, Handel-C
Problems • This model is also not free of problems • Deadlock, livelock, conspiracies possible Example: Cobegin P1:: recv m1 from P2; send m2 to P3 || P2:: recv m3 from P3; send m1 to P1 || P3:: recv m2 from P1; send m3 to P2 coend
Synchronization Mechanisms • to ensure determinacy • But still no guarantee on execution • Unpredictability, Deadlocking, Divergence • Naive implementation not suitable for real-time embedded systems • Many proposals to improve the situation
Synchronization Mechanisms • Priorities, specific scheduling mechanisms • But still a lot of problems • Complex task management strategies • Robustness under change of design • It is a very challenging job • Definitely can be avoided for very many embedded systems • Consumer embedded systems, games and toys
Synchronous Model • Alternative to the classical model • reconciles concurrency with determinism • Free of many of the above problems • with Enhanced predictability • More confidence • works very effectively for simple systems • Various Languages employ this • HW description languages (VHDL, Verilog) • Esterel, Lustre, Signal, Statecharts • Handle C
Synchronous Concurrency • A novel model of concurrency • Given a concurrent program cobegin P1 // P2 // P3 Coend • P1, P2 and P3 simultaneously executed! • Execution of each Pi is a series of atomic steps • What is an atomic step?
Synchronous Concurrency • Every step of each process is synchronized with that of other processes • Contrast with classical notion: • steps of different processes are interleaved • One single step of only one process at a time
Example cobegin x = 4 // y = 6 // z = 19 coend • Execution involves one single step in which all the threeassignments are simultaneously executed • How is this executed in the classical model?
Example (contd.) • What about this example? cobegin x = 0 // x = 1 coend • Undefined in the synchronous model • Sharing of variables disallowed • How do processes communicate then?
Process Communication • Through special communication mechanisms • Event-oriented communication • one process generates or causes an event • While other processes waits for consuming the event
Process Communication • Synchrony Hypothesis • Event generation and consumption simultaneous • Example: cobegin emit S(15) // await S(x) coend • Execution involves • emission of S with value 15 by process 1 • absorption of the signal by process 2 with x assigned this value
Process Communication • Communicating partners - various possibilities: • one to one communication • one to many communication: • broadcasting • can there be multiple generators? • Some models allow this (VHDL, Esterel)
Process Communication • Special care for events with different values • Example: cobegin emit S(10) // emit S(15) // await S(x) coend • value of x is 25! - Resolution Function
Absence of events • Synchronous execution is powerful • Since signal emission is simultaneous, absence of signals can be tested! • Absence of a message can not be tested in asynchronous systems • Example: cobegin x = 16 // present not(S) then y = 20 coend
Absence of events • Of course this gives rise to paradoxes: present not(S1) then emit S2 // present not(S2) then emit S1 • More on this later
A notion of time • Synchronous execution defines a sequence of discrete instances • In the first instance, first steps of all processes executed • In the second, second steps of all are executed and so on • Instances can be taken as clock ticks • Execution can be viewed to proceed in a sequence of instances
A notion of time (contd.) • The instances can be triggered from outside • by clock ticks (HW implementation) • periodically or interrupt-driven by program (SW) • This makes the language a real-time language • Processes can count time now! await 4 ticks
Deterministic Execution • Synchronous execution coupled with no sharing of variables • leads to deterministic results • Testing of absence of signals gives rise to some nondeterminism • canbe resolved in some way (more on this later)
Implementation • How to implement synchronous execution? • Hardware implementation: • By multiple functional units with the same clock • Software Implementation: • By compiling away concurrency (Esterel) • concurrency required only for ease of description • can be replaced by sequential code at run time (Esterel) • Advantage: • No run time overhead of tasks and tasks scheduling • More predictable results • More on this later
Summary Classical Concurrency Model • an asynchronous model • No upper-bound on waiting time • No guarantee on execution of atomic actions • Very little control on timing • System behaviour unpredictable, especially under revision • gives serious problems for real-time applications
Summary • Synchronous Model • free of many undesirable features • Given a very brief introduction • Multi-step execution • Communication via broadcasting • Notion of time • Concrete illustration using Esterel later