PZ12B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 1 PZ12B - Synchronization and semaphores Programming Language.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of.
More on Processes Chapter 3. Process image _the physical representation of a process in the OS _an address space consisting of code, data and stack segments.
Synchronization and Deadlocks
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Intertask Communication and Synchronization In this context, the terms “task” and “process” are used interchangeably.
Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 91 Today’s class Mutual exclusion and synchronization 
PZ13B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ13B - Client server computing Programming Language.
PZ12A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ12A - Guarded commands Programming Language Design.
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.
PZ11B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ11B - Parallel execution Programming Language Design.
PZ13A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ13A - Processor design Programming Language Design.
OS Spring 2004 Concurrency: Principles of Deadlock Operating Systems Spring 2004.
Inter Process Communication:  It is an essential aspect of process management. By allowing processes to communicate with each other: 1.We can synchronize.
CPSC 4650 Operating Systems Chapter 6 Deadlock and Starvation
OS Fall’02 Concurrency: Principles of Deadlock Operating Systems Fall 2002.
1 Organization of Programming Languages-Cheng (Fall 2004) Concurrency u A PROCESS or THREAD:is a potentially-active execution context. Classic von Neumann.
The Structure of the “THE” -Multiprogramming System Edsger W. Dijkstra Jimmy Pierce.
CPS110: Implementing threads/locks on a uni-processor Landon Cox.
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.
Concurrency - 1 Tasking Concurrent Programming Declaration, creation, activation, termination Synchronization and communication Time and delays conditional.
Comparative Programming Languages hussein suleman uct csc304s 2003.
PZ11A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ11A - Exception handling Programming Language Design.
Contact Information Office: 225 Neville Hall Office Hours: Monday and Wednesday 12:00-1:00 and by appointment.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Introduction to Concurrency.
CPS 506 Comparative Programming Languages
1 Announcements The fixing the bug part of Lab 4’s assignment 2 is now considered extra credit. Comments for the code should be on the parts you wrote.
1 Concurrency Architecture Types Tasks Synchronization –Semaphores –Monitors –Message Passing Concurrency in Ada Java Threads.
SEMAPHORE By: Wilson Lee. Concurrency Task Synchronization Example of semaphore Language Support.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Parallel execution Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Using a simple Rendez-Vous mechanism in Java
ICS 313: Programming Language Theory Chapter 13: Concurrency.
Chapter 7 -1 CHAPTER 7 PROCESS SYNCHRONIZATION CGS Operating System Concepts UCF, Spring 2004.
Synchronization and semaphores Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
13-1 Chapter 13 Concurrency Topics Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads C# Threads.
Chapter 7 - Interprocess Communication Patterns
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.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
1 Parallel execution Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
CSCI1600: Embedded and Real Time Software Lecture 17: Concurrent Programming Steven Reiss, Fall 2015.
1 5-High-Performance Embedded Systems using Concurrent Process (cont.)
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community.
PZ11A Programming Language design and Implementation -4th Edition
Chapter 3: Process Concept
CS703 – Advanced Operating Systems
5-High-Performance Embedded Systems using Concurrent Process (cont.)
Lecture 12 Concepts of Programming Languages
Concurrency: Mutual Exclusion and Synchronization
Shared Memory Programming
Parallel execution Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
PZ01C - Machine architecture
Background and Motivation
Concurrency: Mutual Exclusion and Process Synchronization
Subject : T0152 – Programming Language Concept
CSCI1600: Embedded and Real Time Software
CS333 Intro to Operating Systems
PZ09B - Parameter transmission
CSCI1600: Embedded and Real Time Software
Parallel execution Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Synchronization and semaphores
Chapter 3: Process Management
Presentation transcript:

PZ12B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ12B - Synchronization and semaphores Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section

PZ12B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Issues in synchronization Easy to do - Context switching (and statement, fork() function, task creation) Hardware virtual memory makes it easy for operating system to create independently executing tasks in their own address space Hard to do - Synchronization. How to pass information reliably among a set of independently executing parallel tasks. Consider and statement discussed previously: What is output that is printed? I = 1; I = I+1 and I = I-1 and write(I); write(I) Both first and second write can be 0, 1 or 2. Why?

PZ12B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Parallel soup (1) I = I+1 and (2) I = I-1 and (3) write(I); (4) write(I) If execution order is (1)(2)(3)(4), output is If execution order is (1)(3)(2)(4), output is If execution order is (2)(3)(1)(4), output is How to get second write of 0: (1) I=I+1 is not a single operation. It is usually 3 instructions: Load from I, Add 1, store into I. What if context switch between instructions 1 and 2? (2) I=I-1 will set I to 0 then context switch back to (1) causes original value of I to be incremented to 2, which is printed as If I=I-1 is executed first, we could get, etc.

PZ12B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Critical regions A critical region is a sequence of program statements within a task where the task is operating on some data object shared with other tasks. Must use some mechanism to access this data: Semaphores Messages Monitors Rendezvous

PZ12B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Mutual exclusion 1. Mutual exclusion - Only one process can be executing a given section of code at one time. block(X) - block semaphore X. wakeup(X) - unblock semaphore X block(x) - if x is free, grab x, else wait until it is free wakeup(x) - free x and start any process waiting on x Example again: I = 1; block(A); I = I+1; wakeup(A) and block(A); I = I-1; wakeup(A) and block (A); write(I); wakeup(A) write(I) Output: Second write is always I=1, but first write can be 0, 1 or 2, depending upon which and statement executes first.

PZ12B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Semaphores 2. P-V semaphores (Dijkstra). P(X) { If X> 0 continue, else wait until X > 0. X = X-1 (in unit time)} V(X) { X = X+1 (in unit time)} P acts as a gate to limit access to tasks. In addition, you can use the semaphore as a counter to control how much access you need. Buffer manager: Initialization:{ counter = number of buffers available}; GetBuffer: { P(counter}, return buffer and process data}; FreeBuffer: { Put buffer on free list; V{counter}}; In GetBuffer, if no buffers are available, the program will wait until some other process issues a FreeBuffer to unfreeze the blocked process.

PZ12B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Multiple address spaces P-V assume all semaphores are addressable by all waiting tasks. When processes execute in different address spaces (e.g., on different machines), semaphores don't work. Messages can be used: send(A) - Send a message to pipe A. receive(A) - Receive a message on pipe A. If no pending message, wait until one shows up. Sample Input-Process-Output loop: P1: do loop P2: do loopP3: do loop Get data; Receive(P2, data) Receive(P3, data); Send(P2,data); Process data; Print data end Send(P3, data) end end Synchronization can be handled by sending messages in both directions: Pipe AB sends from A to B. Pipe BA sends from B to A.

PZ12B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Problem: Deadlock Process A: Block(X); Block(Y);... Do something Wakeup(X); Wakeup(Y); Process B: Block(Y); Block(X);... Do something Wakeup(X); Wakeup(Y) If process A does Block(X) at same time process B does Block(Y), then Both will succeed and then Both will wait for the other resource forever An unlikely occurrence, but it CAN happen.

PZ12B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Synchronization in Ada Rendezvous in Ada: Sender issues call DataReady Receiver issues accept DataReady. Either waits for other to reach this point. accept DataReady do -- Entry point for task for sender to do call DataReady -- process synchronization taask end; How to prevent receiver of task to be blocked- Use guarded if (select statement)...

PZ12B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Ada rendezvous select when Device1Status = ON => accept Ready1 do... end; or when Device2Status = ON => accept Ready2 do... end; or when Device3Status = connected => accept Ready3 do... end; else No device is ready; do something end select; rendezvous - Task waits for either of Device 1, 2, or 3, and if none are ON, does something else

PZ12B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Monitors A monitor is a shared data object together with the set of operations that may manipulate it (i.e., an abstract data type) A task may manipulate the shared data object only by using the defined operations (i.e., data is encapsulated) To enforce mutual exclusion, require that at most one of the operations defined for the data object may be executing at any given time. We could implement monitors in Java or C++ as classes