© S. Ramesh / Krithi Ramamritham / Kavi Arya 1 IT-606 Embedded Systems (Software) S. Ramesh Krithi Ramamritham Kavi Arya KReSIT/ IIT Bombay.

Slides:



Advertisements
Similar presentations
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Advertisements

Synchronization and Deadlocks
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
© S. Ramesh / Kavi Arya / Krithi Ramamritham IT-606 Embedded Systems (Software) S. Ramesh Kavi Arya Krithi Ramamritham KReSIT/ IIT Bombay.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
5.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 5: CPU Scheduling.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Concurrent Programming James Adkison 02/28/2008. What is concurrency? “happens-before relation – A happens before B if A and B belong to the same process.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
INSE - Lectures 19 & 20 SE for Real-Time & SE for Concurrency  Really these are two topics – but rather tangled together.
Parallel Processing (CS526) Spring 2012(Week 6).  A parallel algorithm is a group of partitioned tasks that work with each other to solve a large problem.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
The Spin Model Checker Promela Introduction Nguyen Tuan Duc Shogo Sawai.
Event Driven Real-Time Programming CHESS Review University of California, Berkeley, USA May 10, 2004 Arkadeb Ghosal Joint work with Marco A. Sanvido, Christoph.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
1 Synchronization Coordinating the Activity of Mostly Independent Entities.
Chapter 2: Processes Topics –Processes –Threads –Process Scheduling –Inter Process Communication (IPC) Reference: Operating Systems Design and Implementation.
Concurrent Processes Lecture 5. Introduction Modern operating systems can handle more than one process at a time System scheduler manages processes and.
1 Concurrent and Distributed Systems Introduction 8 lectures on concurrency control in centralised systems - interaction of components in main memory -
Concurrency CS 510: Programming Languages David Walker.
Review of “Embedded Software” by E.A. Lee Katherine Barrow Vladimir Jakobac.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
3.5 Interprocess Communication
Chapter 11: Distributed Processing Parallel programming Principles of parallel programming languages Concurrent execution –Programming constructs –Guarded.
CS533 - Concepts of Operating Systems
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes.
Introduction to Parallel Processing 3.1 Basic concepts 3.2 Types and levels of parallelism 3.3 Classification of parallel architecture 3.4 Basic parallel.
Advances in Language Design
Operating System A program that controls the execution of application programs An interface between applications and hardware 1.
Concurrency, Mutual Exclusion and Synchronization.
- 1 - Embedded Systems - SDL Some general properties of languages 1. Synchronous vs. asynchronous languages Description of several processes in many languages.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Introduction to Concurrency.
CIS 540 Principles of Embedded Computation Spring Instructor: Rajeev Alur
1 Concurrency Architecture Types Tasks Synchronization –Semaphores –Monitors –Message Passing Concurrency in Ada Java Threads.
CE Operating Systems Lecture 3 Overview of OS functions and structure.
The Complexity of Distributed Algorithms. Common measures Space complexity How much space is needed per process to run an algorithm? (measured in terms.
© S. Ramesh / Kavi Arya / Krithi Ramamritham 1 IT-606 Embedded Systems (Software) S. Ramesh Kavi Arya Krithi Ramamritham KReSIT/ IIT Bombay.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Conformance Test Experiments for Distributed Real-Time Systems Rachel Cardell-Oliver Complex Systems Group Department of Computer Science & Software Engineering.
Chapter 7 -1 CHAPTER 7 PROCESS SYNCHRONIZATION CGS Operating System Concepts UCF, Spring 2004.
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
© S. Ramesh / Kavi Arya / Krithi Ramamritham 1 IT-606 Embedded Systems (Software ) S. Ramesh Kavi Arya Krithi Ramamritham KReSIT/ IIT Bombay.
Hwajung Lee. Well, you need to capture the notions of atomicity, non-determinism, fairness etc. These concepts are not built into languages like JAVA,
Hwajung Lee. Why do we need these? Don’t we already know a lot about programming? Well, you need to capture the notions of atomicity, non-determinism,
13-1 Chapter 13 Concurrency Topics Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads C# Threads.
Lecture 4 Introduction to Promela. Promela and Spin Promela - process meta language G. Holzmann, Bell Labs (Lucent) C-like language + concurrency dyamic.
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
CIS 540 Principles of Embedded Computation Spring Instructor: Rajeev Alur
CSCI1600: Embedded and Real Time Software Lecture 17: Concurrent Programming Steven Reiss, Fall 2015.
Agenda  Quick Review  Finish Introduction  Java Threads.
Introduction to distributed systems description relation to practice variables and communication primitives instructions states, actions and programs synchrony.
Embedded Real-Time Systems Processing interrupts Lecturer Department University.
Configuring pacemaker while 2kms away. Person is travelling in driverless car.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Model and complexity Many measures Space complexity Time complexity
Process Synchronization
Advanced Topics in Concurrency and Reactive Programming: Asynchronous Programming Majeed Kassis.
Multiple Writers and Races
Designing Parallel Algorithms (Synchronization)
Lecture 2 Part 2 Process Synchronization
Concurrency: Mutual Exclusion and Process Synchronization
Subject : T0152 – Programming Language Concept
Chapter 6: Synchronization Tools
Programming with Shared Memory Specifying parallelism
STATE MACHINE AND CONCURRENT
Presentation transcript:

© S. Ramesh / Krithi Ramamritham / Kavi Arya 1 IT-606 Embedded Systems (Software) S. Ramesh Krithi Ramamritham Kavi Arya KReSIT/ IIT Bombay

© S. Ramesh / Krithi Ramamritham / Kavi Arya 2 Synchronous Models: Motivation S. Ramesh

© S. Ramesh / Krithi Ramamritham / Kavi Arya 3 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

© S. Ramesh / Krithi Ramamritham / Kavi Arya 4 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

© S. Ramesh / Krithi Ramamritham / Kavi Arya 5 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

© S. Ramesh / Krithi Ramamritham / Kavi Arya 6 Example execution 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 Cobegin x = 5 || y = 10 || z = 23 Coend

© S. Ramesh / Krithi Ramamritham / Kavi Arya 7 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?

© S. Ramesh / Krithi Ramamritham / Kavi Arya 8 Some of the Possible results are: 1.Hello World! How are you? What is your name? 2.How are you? What is your name? Hello World! 3.What is your name? Hello World! How are you? Are there any other possibilities? Nondeterminism

© S. Ramesh / Krithi Ramamritham / Kavi Arya 9 Another Example What is the value of x at the end? 1, 2 ? 3? Is 0 possible? cobegin x = 0; x = x + 1; || x = 1; x = x + 1 coend

© S. Ramesh / Krithi Ramamritham / Kavi Arya 10 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

© S. Ramesh / Krithi Ramamritham / Kavi Arya 11 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

© S. Ramesh / Krithi Ramamritham / Kavi Arya 12 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)

© S. Ramesh / Krithi Ramamritham / Kavi Arya 13 How to reconcile determinism with concurrency? – Classical approach (discussed earlier) – Synchronous approach ( now) Speed Independence

© S. Ramesh / Krithi Ramamritham / Kavi Arya 14 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

© S. Ramesh / Krithi Ramamritham / Kavi Arya 15 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

© S. Ramesh / Krithi Ramamritham / Kavi Arya 16 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

© S. Ramesh / Krithi Ramamritham / Kavi Arya 17 Atomic steps Here is another variation - One of the simplest Programs indicate atomic actions Specified atomicity can include one or more statements Example: Cobegin indicates atomic action x = x + 1 > || ; Coend What will be the value of x at the end?

© S. Ramesh / Krithi Ramamritham / Kavi Arya 18 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

© S. Ramesh / Krithi Ramamritham / Kavi Arya 19 Starvation Consider the following problem: y = 1; x = 0 cobegin while (y > 0) {x = x + 1} || y = 0 coend Will the program terminate ?

© S. Ramesh / Krithi Ramamritham / Kavi Arya 20 Starvation Example Need not terminate – First process can keep executing Terminates in practice Fairness in selection of processes

© S. Ramesh / Krithi Ramamritham / Kavi Arya 21 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

© S. Ramesh / Krithi Ramamritham / Kavi Arya 22 Message Passing Concurrency Processes do not share variable share channels instead channels carry messages send and receive actions asynchronous communication

© S. Ramesh / Krithi Ramamritham / Kavi Arya 23 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

© S. Ramesh / Krithi Ramamritham / Kavi Arya 24 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

© S. Ramesh / Krithi Ramamritham / Kavi Arya 25 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

© S. Ramesh / Krithi Ramamritham / Kavi Arya 26 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

© S. Ramesh / Krithi Ramamritham / Kavi Arya 27 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

© S. Ramesh / Krithi Ramamritham / Kavi Arya 28 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?

© S. Ramesh / Krithi Ramamritham / Kavi Arya 29 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

© S. Ramesh / Krithi Ramamritham / Kavi Arya 30 Example cobegin x = 4 // y = 6 // z = 19 coend Execution involves one single step in which all the three assignments are simultaneously executed How is this executed in the classical model?

© S. Ramesh / Krithi Ramamritham / Kavi Arya 31 Example (contd.) What about this example? Undefined in the synchronous model Sharing of variables disallowed How do processes communicate then ? cobegin x = 0 // x = 1 coend

© S. Ramesh / Krithi Ramamritham / Kavi Arya 32 Process Communication Through special communication mechanisms Event-oriented communication one process generates or causes an event While other processes waits for consuming the event

© S. Ramesh / Krithi Ramamritham / Kavi Arya 33 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

© S. Ramesh / Krithi Ramamritham / Kavi Arya 34 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)

© S. Ramesh / Krithi Ramamritham / Kavi Arya 35 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

© S. Ramesh / Krithi Ramamritham / Kavi Arya 36 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

© S. Ramesh / Krithi Ramamritham / Kavi Arya 37 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

© S. Ramesh / Krithi Ramamritham / Kavi Arya 38 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

© S. Ramesh / Krithi Ramamritham / Kavi Arya 39 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

© S. Ramesh / Krithi Ramamritham / Kavi Arya 40 Deterministic Execution Synchronous execution coupled with no sharing of variables –leads to deterministic results Testing of absence of signals gives rise to some nondeterminism can be resolved in some way (more on this later)

© S. Ramesh / Krithi Ramamritham / Kavi Arya 41 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

© S. Ramesh / Krithi Ramamritham / Kavi Arya 42 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

© S. Ramesh / Krithi Ramamritham / Kavi Arya 43 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