Agenda  Quick Review  Finish Introduction  Java Threads.

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

Concurrent Programming Abstraction & Java Threads
CSC321 Concurrent Programming: §3 The Mutual Exclusion Problem 1 Section 3 The Mutual Exclusion Problem.
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
D u k e S y s t e m s Time, clocks, and consistency and the JMM Jeff Chase Duke University.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Silberschatz, Galvin and Gagne ©2007 Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Chapter 6 (a): Synchronization.
Chapter 6: Process Synchronization
Background Concurrent access to shared data can lead to inconsistencies Maintaining data consistency among cooperating processes is critical What is wrong.
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.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
Operating System Concepts and Techniques Lecture 12 Interprocess communication-1 M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and.
Mutual Exclusion.
Critical Section chapter3.
© 2009 Matthew J. Sottile, Timothy G. Mattson, and Craig E Rasmussen 1 Concurrency in Programming Languages Matthew J. Sottile Timothy G. Mattson Craig.
Today’s Agenda  Midterm: Nov 3 or 10  Finish Message Passing  Race Analysis Advanced Topics in Software Engineering 1.
Concurrency.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Concurrency CS 510: Programming Languages David Walker.
University of Pennsylvania 9/19/00CSE 3801 Concurrent Processes CSE 380 Lecture Note 4 Insup Lee.
Chapter 11: Distributed Processing Parallel programming Principles of parallel programming languages Concurrent execution –Programming constructs –Guarded.
Chapter 11 Operating Systems
CS533 - Concepts of Operating Systems
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes.
Synchronization CSCI 444/544 Operating Systems Fall 2008.
Fundamentals of Python: From First Programs Through Data Structures
Advances in Language Design
1 Testing Concurrent Programs Why Test?  Eliminate bugs?  Software Engineering vs Computer Science perspectives What properties are we testing for? 
© 2009 Matthew J. Sottile, Timothy G. Mattson, and Craig E Rasmussen 1 Concurrency in Programming Languages Matthew J. Sottile Timothy G. Mattson Craig.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Concurrency, Mutual Exclusion and Synchronization.
Lecture 2 Foundations and Definitions Processes/Threads.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Introduction to Concurrency.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion.
COMP 111 Threads and concurrency Sept 28, Tufts University Computer Science2 Who is this guy? I am not Prof. Couch Obvious? Sam Guyer New assistant.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
The Complexity of Distributed Algorithms. Common measures Space complexity How much space is needed per process to run an algorithm? (measured in terms.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
CY2003 Computer Systems Lecture 04 Interprocess Communication.
Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
Concurrency Properties. Correctness In sequential programs, rerunning a program with the same input will always give the same result, so it makes sense.
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,
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-5 Process Synchronization Department of Computer Science and Software.
13-1 Chapter 13 Concurrency Topics Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads C# Threads.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
C H A P T E R E L E V E N Concurrent Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Operating Systems CMPSC 473 Signals, Introduction to mutual exclusion September 28, Lecture 9 Instructor: Bhuvan Urgaonkar.
Operating System Chapter 5. Concurrency: Mutual Exclusion and Synchronization Lynn Choi School of Electrical Engineering.
Program Correctness. The designer of a distributed system has the responsibility of certifying the correctness of the system before users start using.
Software Systems Verification and Validation Laboratory Assignment 4 Model checking Assignment date: Lab 4 Delivery date: Lab 4, 5.
Today’s Agenda  Quiz 4  Temporal Logic Formal Methods in Software Engineering1.
1 Critical Section Problem CIS 450 Winter 2003 Professor Jinhua Guo.
Introduction to distributed systems description relation to practice variables and communication primitives instructions states, actions and programs synchrony.
Synchronization Questions answered in this lecture: Why is synchronization necessary? What are race conditions, critical sections, and atomic operations?
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
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
Concurrency.
Shared Memory Programming
Background and Motivation
Dr. Mustafa Cem Kasapbaşı
Concurrency: Mutual Exclusion and Process Synchronization
CSE 153 Design of Operating Systems Winter 19
Foundations and Definitions
Presentation transcript:

Agenda  Quick Review  Finish Introduction  Java Threads

Quick Review  What are the unique aspects of concurrent programs?

Advanced Topics in Software Engineering 3 Introduction  Overview  Motivating Examples  Interleaving Model  Semantics of Correctness  Testing, Debugging, and Verification

Advanced Topics in Software Engineering 4 Concurrent Programs  Characterized by a set of cooperative processes or threads that can execute in parallel.  Three types of concurrent programs  multi-threaded programs: multiple threads running in a single process space  distributed programs: multiple processes running in a distributed environment  distributed multi-threaded programs: multiple processes running a distributed environment, and each process consists of multiple threads

Advanced Topics in Software Engineering 5 Why Concurrency?  Better utilization of resources, which often leads to increased computation efficiency  Provides a natural model for many problem domains that are inherently concurrent  Allows computation to be distributed in a network environment So what is the catch?

Advanced Topics in Software Engineering 6 How different?  Three fundamental issues in concurrent programming  nondeterminism  synchronization  communication  Note that synchronization can be considered as a special case of communication.

Advanced Topics in Software Engineering 7 Nondeterminism  An inherent property of concurrent programs - two executions of the same program with the same input may produce different, and sometimes unpredictable, results.  Caused by one or more of the following factors:  the unpredictable rates of progress of threads  the unpredictable network latency  the explicit use of nondeterministic programming constructs

Advanced Topics in Software Engineering 8 Synchronization  Enforces a certain order on tasks that are performed by different threads/processes  Mutual exclusion ensures that critical sections in different threads do not execute at the same time.  Conditional synchronization delays a thread until a given condition is true.  Mutual exclusion is a special case of conditional synchronization.

Advanced Topics in Software Engineering 9 Communication  Exchange data between threads/processes that are running in parallel  shared variables - a thread writes into a variable that is read by another thread.  message passing - a thread sends a message that is received by another thread.

Advanced Topics in Software Engineering 10 Process and Thread In general, a process is a program in execution. A thread is a unit of control within a process. When a program is executed, the operating system creates a process. A process starts with a running thread, called the “main” thread, which executes the “main function” of the program. Other threads can be created by the main thread, and these threads can create other threads.

Advanced Topics in Software Engineering 11 Process vs. Thread A process has its own stream of instructions and its own logical address space. A thread has its own stream of instructions, but shares the same logic address space with other threads in the same process. This difference has significant implications on how processes and threads communicate.

Advanced Topics in Software Engineering 12 Introduction  Overview  Motivating Examples  Interleaving Model  Semantics of Correctness  Testing, Debugging, and Verification

Advanced Topics in Software Engineering 13 Example 1 Assume that integer x is initially 0. Thread 1 Thread 2 Thread 3 (1) x = 1; (2) x = 2; (3) y = x; What is the final value of y?  (1) (2) (3)  (2) (1) (3)  (3) (2) (1)

Advanced Topics in Software Engineering 14 Example 2 Assume that y and z are initially 0. Thread 1 Thread 2 x = y + z y = 1; z = 2; What is the final value of x?

Advanced Topics in Software Engineering (1), (2), (3), (4), (5) 2. (4), (1), (2), (3), (5) 3. (1), (4), (5), (2), (3) 4. (4), (5), (1), (2), (3) Example 2 Thread 1 (1) load r1, y (2) add r1, z (3) store r1, x Thread 2 (4) assign y, 1 (5) assign z, 2

Advanced Topics in Software Engineering 16 Example 3 Assume that the initial value of x is 0. Thread 1 Thread 2 x = x + 1; x = 2; What is the final value of x?

Advanced Topics in Software Engineering 17 Example 3 Thread 1 (1) load r1, x (2) add r1, 1 (3) store r1, x Thread 2 (4) assign x, 2 1. (1), (2), (3), (4) 2. (4), (1), (2), (3) 3. (1), (2), (4), (3)

Advanced Topics in Software Engineering 18 Example 4 Consider a linked list of Nodes, pointed to by variable first, and accessed by methods deposit and withdraw. class Node { public int value; public Node* next; } Node* first; void deposit (int value) { Node p = new Node (); // (1) p.value = value; // (2) p.next = first; // (3) first = p; // (4) } int withdraw () { int value = first -> value; // (5) first = first -> next; // (6) return value; // (7) } Can you find a scenario which produces unexpected results?

Advanced Topics in Software Engineering 19 Example 4 Suppose that the linked list pointed to by first is not empty and that methods deposit and withdraw are executed concurrently by two threads. In the following possible scenario, the deposited item has been lost. int value = first -> value; (5) in withdraw Node p = new Node (); (1) in deposit p.value = value; (2) in deposit p.next = first; (3) in deposit first = p; (4) in deposit first = first -> next; (6) in withdraw return value; (7) in withdraw

Advanced Topics in Software Engineering 20 Introduction  Overview  Motivating Examples  Interleaving Model  Semantics of Correctness  Testing, Debugging, and Verification

Advanced Topics in Software Engineering 21 They actually take turns... A multithreaded process consists of a set of threads that execute simultaneously. However, a single CPU can only execute one instruction (from one thread) at a time. In general, each thread receives a time slice of the CPU. Threads that execute simultaneously actually take turns to run.

Advanced Topics in Software Engineering 22 Scheduler A hardware timer is used to generate interrupts at predetermined interval, which interrupts the execution of the current thread. The operating system uses a scheduler program to determine which thread will take over the CPU for the next time slice. A context switch is then performed between the current thread and the next thread.

Advanced Topics in Software Engineering 23 Atomic Actions In principle, atomic actions are actions whose intermediate states are not visible to other threads. A context switch can occur in the middle of an atomic action, but another thread either sees the state that is before or after the action, but not in between.

Advanced Topics in Software Engineering 24 Interleaving View From the CPU’s perspective, simultaneous execution of multiple threads is indeed an interleaving of atomic instructions of individual threads. This interleaving view can be generalized to the case of multiple processors or computers. An alternative to the interleaving model is the so- called partial order or happened-before model, which we will discuss later in the class.

Advanced Topics in Software Engineering 25 Instructions vs. Statements In general, when we write concurrent programs, we consider machine instructions are atomic actions. Statements in high level languages are usually translated into a stream of machine instructions, and thus can not be considered as atomic actions.

Advanced Topics in Software Engineering 26 Introduction  Overview  Motivating Examples  Interleaving Model  Semantics of Correctness  Testing, Debugging, and Verification

Advanced Topics in Software Engineering 27 Not straightforward... Unlike sequential programs, the correctness of concurrent programs is not straightforward. Furthermore, nondeterministic execution makes it difficult to ensure the correctness of concurrent programs.

Advanced Topics in Software Engineering 28 Every interleaving counts! Given an input, multiple executions of a concurrent program may produce different results. A concurrent program is correct if and only if it is correct under ALL interleavings.

Advanced Topics in Software Engineering 29 Safety & Liveness There are two types of properties:  Safety: Nothing bad will happen.  Liveness: Something good will eventually happen.

Advanced Topics in Software Engineering 30 Usually domain specific... Correctness is usually a domain specific concept. It depends on the functional requirements of a program. However, deadlock, livelock, and starvation are properties that are important to all concurrent programs.

Advanced Topics in Software Engineering 31 Deadlock A thread T is said to be deadlocked if T is blocked, and will remain blocked forever, regardless of what other threads will do. As an example, assume that P contains threads T1 and T2, and the following sequence is executed:  T1 waits to receive a message from T2  T2 waits to receive a message from T1 Both T1 and T2 will remain blocked forever.

Advanced Topics in Software Engineering 32 Livelock A thread is said to be livelocked if the following conditions are satisfied, regardless of what other threads will do:  The thread will not terminate or deadlock.  The thread will never make progress.

Advanced Topics in Software Engineering 33 Starvation Starvation refers to one possible execution where one process dominates, not allowing other processes to progress.

Advanced Topics in Software Engineering 34 Introduction  Overview  Motivating Examples  Interleaving Model  Correctness Semantics  Testing, Debugging, and Verification

Advanced Topics in Software Engineering 35 Failure & Faults A failure is an observed departure of the external result of software operation from software requirements or user expectations A fault is a defective, missing, or extra instruction or a set of related instructions that is the cause of one or more actual or potential failures.

Advanced Topics in Software Engineering 36 Testing & Debugging Testing is to find program failures; debugging is to locate and correct program faults. The conventional approach to testing and debugging a program is as follows: 1. Select a set of test inputs. 2. Execute the program once with each input and check the test results. 3. If a test input finds a failure, execute the program again with the same input to locate and correct the fault that caused the failure 4. After the fault has been located and corrected, execute the program again with each of the test inputs to verify that the fault has been corrected and that no new faults have been introduced.

Advanced Topics in Software Engineering 37 Problems and Issues Unfortunately, the conventional approach breaks down for concurrent programs. Why? Let P be a concurrent program.  When testing P with input X, a single execution is insufficient to determine the correctness of P with X.  When debugging an erroneous execution of P with input X, there is no guarantee that this execution will be repeated by executing P with X.  After P has been modified to correct a fault, one or more successful executions of P with X do not imply that the fault has been corrected.

Advanced Topics in Software Engineering 38 Problems and Issues To solve these problems, we need to address the following issues:  How to replay concurrent executions?  How to capture enough information, but as little as possible, for replay?  How to determine sequence feasibility and validity?  How to deal with the probe effect?

Advanced Topics in Software Engineering 39 CCS (Calculus of Communicating Systems) Testing can prove the presence of bugs, but not their absence. CCS provides a formal notation that can be used to analyze and reason about the correctness of a system. At the core of CCS are a number of equational laws and several notions of equivalence that allows one to reason about the behavior of a concurrent system.

Advanced Topics in Software Engineering 40 Review  A concurrent program consists of a number of threads that execute in parallel.  There are three fundamental issues in concurrent programming: nondeterminism, synchronization and communication.  A concurrent execution can be characterized as an interleaving of atomic actions.

Advanced Topics in Software Engineering 41 Review  A concurrent program should be free from deadlock, livelock, and/or starvation.  Testing and debugging of concurrent programs are difficult due to nondeterminism.  Testing and debugging cannot provide total confidence about program correctness.  CCS provides a mathematical treatment of concurrent programs.