1 C OMP 346 – W INTER 2015 Tutorial # 5. Semaphores for Barrier Sync A barrier is a type of synchronization method. A barrier for a group of threads or.

Slides:



Advertisements
Similar presentations
1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of.
Advertisements

Tutorial 3 Sync or sink! presented by: Antonio Maiorano Paul Di Marco.
Computer Science 320 Clumping in Parallel Java. Sequential vs Parallel Program Initial setup Execute the computation Clean up Initial setup Create a parallel.
Parallel Programming – Barriers, Locks, and Continued Discussion of Parallel Decomposition David Monismith Jan. 27, 2015 Based upon notes from the LLNL.
Operating Systems Mehdi Naghavi Winter 1385.
Overview Assignment 8: hints Assignment 7: solution Deadlocks
Ch 7 B.
Prepared By Sarath S Menon S6 CSE.  Imagine a scenario in which there exists two Distinct processes both operating on a single shared data area.  One.
May 23, 2002Serguei A. Mokhov, 1 Synchronization COMP346/ Operating Systems Tutorial 3 Revision 1.2 October 7, 2003.
1 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
EEE 435 Principles of Operating Systems Interprocess Communication Pt II (Modern Operating Systems 2.3)
CH7 discussion-review Mahmoud Alhabbash. Q1 What is a Race Condition? How could we prevent that? – Race condition is the situation where several processes.
Secure Operating Systems Lesson 5: Shared Objects.
02/27/2004CSCI 315 Operating Systems Design1 Process Synchronization Deadlock Notice: The slides for this lecture have been largely based on those accompanying.
CS427 Multicore Architecture and Parallel Computing
Classic Synchronization Problems
Concurrency and Thread Yoshi. Two Ways to Create Thread Extending class Thread – Actually, we need to override the run method in class Thread Implementing.
Threading Part 3 CS221 – 4/24/09. Teacher Survey Fill out the survey in next week’s lab You will be asked to assess: – The Course – The Teacher – The.
02/19/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
Concurrent Processes Lecture 5. Introduction Modern operating systems can handle more than one process at a time System scheduler manages processes and.
5.6.2 Thread Synchronization with Semaphores Semaphores can be used to notify other threads that events have occurred –Producer-consumer relationship Producer.
CS444/CS544 Operating Systems Synchronization 2/21/2007 Prof. Searleman
Monitors CSCI 444/544 Operating Systems Fall 2008.
02/17/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
CS 470/570 Lecture 7 Dot Product Examples Odd-even transposition sort More OpenMP Directives.
02/19/2007CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Semaphores Questions answered in this lecture: Why are semaphores necessary? How are semaphores used for mutual exclusion? How are semaphores used for.
1 CSCD 330 Network Programming Lecture 13 More Client-Server Programming Sometime in 2014 Reading: References at end of Lecture.
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
Threading and Concurrency Issues ● Creating Threads ● In Java ● Subclassing Thread ● Implementing Runnable ● Synchronization ● Immutable ● Synchronized.
The University of Adelaide, School of Computer Science
1 Processes, Threads, Race Conditions & Deadlocks Operating Systems Review.
4061 Session 23 (4/10). Today Reader/Writer Locks and Semaphores Lock Files.
Synchronization II: CPE Operating Systems
Thread Synchronization Tutorial #8 CPSC 261. A thread is a virtual processor Each thread is provided the illusion that it owns a core – Copy of the registers.
1 Overview on Send And Receive routines in MPI Kamyar Miremadi November 2004.
1 ITCS 6/8010 CUDA Programming, UNC-Charlotte, B. Wilkinson, Jan 25, 2011 Synchronization.ppt Synchronization These notes will introduce: Ways to achieve.
Synchronization Producer/Consumer Problem. Synchronization - PC with Semaphores2 Abstract The producer/consumer problem is a classical synchronization.
CSE 425: Concurrency II Semaphores and Mutexes Can avoid bad inter-leavings by acquiring locks –Guard access to a shared resource to take turns using it.
In Java processes are called threads. Additional threads are associated with objects. An application is associated with an initial thread via a static.
Synchronizing Threads with Semaphores
Lab assignment 1: Synchronization
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-5 Process Synchronization Department of Computer Science and Software.
Processes Creation and Threads. Shared Resource Example The console is just a place where text can be printed by the application currently running. Program.
Synchronization These notes introduce:
Operating Systems COMP 4850/CISG 5550 Interprocess Communication, Part II Dr. James Money.
4.1 Introduction to Threads Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Windows XP Threads Linux Threads.
Operating Systems Unit 4: – Dining Philosophers – Deadlock – Indefinite postponement Operating Systems.
1 ITCS 4/5145 Parallel Programming, B. Wilkinson, Nov 12, CUDASynchronization.ppt Synchronization These notes introduce: Ways to achieve thread synchronization.
Synchronization Exercises. Exercise 1 r Let S be a semaphore that is initialized to 2 r Consider the following: down(S) up(S) down(S) r Does this program.
CSCD 330 Network Programming
CPS110: Reader-writer locks
Chapter 5: Process Synchronization – Part 3
PARALLEL PROGRAM CHALLENGES
Background on the need for Synchronization
Threads Threads.
Deadlocks (part II) Deadlock avoidance – (not reasonable)
Lecture 14: Pthreads Mutex and Condition Variables
CS 143A Quiz 1 Solution.
COP 4600 Operating Systems Fall 2010
Assignment 6 Recitation
Lecture 14: Pthreads Mutex and Condition Variables
CSS430 Deadlocks Textbook Ch8
Synchronization These notes introduce:
“The Little Book on Semaphores” Allen B. Downey
EECE.4810/EECE.5730 Operating Systems
CSE 542: Operating Systems
Synchronization and liveness
Presentation transcript:

1 C OMP 346 – W INTER 2015 Tutorial # 5

Semaphores for Barrier Sync A barrier is a type of synchronization method. A barrier for a group of threads or processes in the source code means any thread/process must stop at this point and cannot proceed until all other threads/processes reach this barrier. 2

Semaphores for Barrier Sync Take a look at the typical problem: all processes must finish their phase I before any of them starts phase II processes must proceed to their phase II in a specific order, for example: 1, 2, 3… This is called barrier synchronization. 4/13/ semaphore s1 = 0, s2 = 0; process P1 process P2 V (s1) V (s2) P (s2) P (s1)

Barrier Sync for Three Processes A possible copy-cat attempt: Why it doesn’t work? 4/13/ semaphore s1 = 0, s2 = 0, s3 = 0; process P1 process P2 process P3 V (s1) V (s2) V (s3) P (s3) P (s1) P (s2)

Barrier Sync for Three Processes A possible copy-cat attempt: Why it doesn’t work? Scenario: 1-6, process 2 even hasn’t started! None of the requirements are met 4/13/ semaphore s1 = 0, s2 = 0, s3 = 0; process P1 process P2 process P V (s1) V (s2) 2 V (s3) 5 P (s3) P (s1) P (s2) 6

Barrier Sync for Three Processes (2) Another attempt: What’s wrong now? 4/13/ semaphore s1 = 0, s2 = 0, s3 = 0; process P1 process P2 process P3 P (s1) V (s1) V (s1) P (s1) P (s2) P (s3) V (s2) V (s3)

Barrier Sync for Three Processes (2) Another attempt: What’s wrong now? Scenario: 1-10, so far so good, but after… The second requirement isn’t met 4/13/ semaphore s1 = 0, s2 = 0, s3 = 0; process P1 process P2 process P P (s1) 5 V (s1) 2 V (s1) 9 P (s1) 6 P (s2) 3 P (s3) 10 V (s2) V (s3)

Barrier Sync for Three Processes (3) Last attempt: A bit “optimized”: 4/13/ semaphore s1 = 0, s2 = 0, s3 = 0; process P1 process P2 process P3 P (s1) V (s1) V (s1) P (s1) P (s2) P (s3) V (s2) V (s3) semaphore s1 = -1, s2 = 0, s3 = 0; process P1 process P2 process P3 V (s1) V (s1) P (s1) P (s2) P (s3) V (s2) V (s3)

Barrier Sync: Need for the General Solution Problem with the proposed solution: # of semaphores == # of processes. Semaphores as any other resource are limited and take space => overhead Imagine you need to sync 10 processes in the same manner? 100? 1000? Complete mess and a high possibility of a deadlock! 4/13/201599

Barrier Sync: Need for the General Solution (2) Attempt for the first requirement: The second requirement is left as an exercise to the curious student :-) 4/13/ semaphore s1 = -n + 2, s2 = 0; process P1 process P2... process Pn... P (s1) V (s1)... V (s1) V (s2) P (s2)... P (s2) V (s2)... V (s2)...

Introducing the Semaphore Class NOTE: Operations Signal and Wait are guaranteed to be atomic! 4/13/ class Semaphore { private int value; public Semaphore(int value) { this.value = value; } public Semaphore() { this(0); }...

Introducing the Semaphore Class (2) 4/13/ public synchronized void Wait() { while(this.value <= 0) { try { wait(); } catch(InterruptedException e) { System.out.println ( "Semaphore::Wait() …” + e.getMessage() ); e.printStackTrace(); } this.value--; }...

Introducing the Semaphore Class (3) 4/13/ public synchronized void Signal() { ++this.value; notify(); } public synchronized void P() { this.Wait(); } public synchronized void V() { this.Signal(); }

References / / barrier barrier oncurrency/sync.html oncurrency/sync.html 14