Homework 12 Due Wednesday, 12/8/10

Similar documents
CS 361 Concurrent programming Drexel University Spring 2000 Lecture 14. The dining philosophers problem

Problems with Concurrency. February 19, 2014

Midterm on next week Tuesday May 4. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 9

Synchronization in Concurrent Programming. Amit Gupta

UNIT-3 : MULTI THREADED PROGRAMMING, EVENT HANDLING. A Multithreaded program contains two or more parts that can run concurrently.

Two Types of Semaphores

Race Conditions & Synchronization

Contents. 6-1 Copyright (c) N. Afshartous

More Synchronization; Concurrency in Java. CS 475, Spring 2018 Concurrent & Distributed Systems

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Need for synchronization: If threads comprise parts of our software systems, then they must communicate.

Object-Oriented Design Lecture 13 CSU 370 Fall 2008 (Pucella) Friday, Oct 31, 2008

What is a thread anyway?

Message Passing. Lecture 7. Shared Variables? Shared Variables? Synchronisation. Overview of Message Passing

Concurrency in Java Prof. Stephen A. Edwards

Concurrent Programming

Advances in Programming Languages

Lecture 6. Introduction to Message Passing

Threads and Parallelism in Java

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages

Info 408 Distributed Applications Programming Exercise sheet nb. 4

CS61B, Spring 2003 Discussion #17 Amir Kamil UC Berkeley 5/12/03

CMSC 330: Organization of Programming Languages. The Dining Philosophers Problem

CS Operating Systems

CS Operating Systems

MCS-378 Intraterm Exam 1 Serial #:

Faculty of Computers & Information Computer Science Department

Computation Abstractions. Processes vs. Threads. So, What Is a Thread? CMSC 433 Programming Language Technologies and Paradigms Spring 2007

Models of concurrency & synchronization algorithms

Question Points Score Total 100

Synchronization Lecture 24 Fall 2018

CMSC 330: Organization of Programming Languages. Threads Classic Concurrency Problems

Contribution:javaMultithreading Multithreading Prof. Dr. Ralf Lämmel Universität Koblenz-Landau Software Languages Team

What's wrong with Semaphores?

Lecture 10: Introduction to Semaphores

Synchronization. Announcements. Concurrent Programs. Race Conditions. Race Conditions 11/9/17. Purpose of this lecture. A8 released today, Due: 11/21

Topic 8: Lazy Evaluation

Multithreaded Programming

CMPSCI 187: Programming With Data Structures. Lecture #20: Concurrency and a Case Study David Mix Barrington 24 October 2012

public class Shared0 { private static int x = 0, y = 0;

Last Class: Synchronization. Review. Semaphores. Today: Semaphores. MLFQ CPU scheduler. What is test & set?

Overview. CMSC 330: Organization of Programming Languages. Concurrency. Multiprocessors. Processes vs. Threads. Computation Abstractions

An Introduction to Programming with Java Threads Andrew Whitaker University of Washington 9/13/2006. Thread Creation

Race Conditions & Synchronization

EI 338: Computer Systems Engineering (Operating Systems & Computer Architecture)

Multiple Inheritance. Computer object can be viewed as

CS 556 Distributed Systems

CMSC 433 Spring 2013 Exam 1

Outline of lecture. i219 Software Design Methodology 10. Multithreaded programming. Kazuhiro Ogata (JAIST)

Real-Time and Concurrent Programming Lecture 4 (F4): Monitors: synchronized, wait and notify

Synchronization Lecture 23 Fall 2017

Programming Language Concepts: Lecture 11

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Part IV Other Systems: I Java Threads

Synchronization in Java

Concurrent Programming using Threads

Threads and Concurrency in Java

Chair of Software Engineering. Java and C# in depth. Carlo A. Furia, Marco Piccioni, Bertrand Meyer. Java: concurrency

MultiJav: A Distributed Shared Memory System Based on Multiple Java Virtual Machines. MultiJav: Introduction

Introduction to Java Threads

Synchronization I. Jo, Heeseung

Introduction to Locks. Intrinsic Locks

Deadlock and Monitors. CS439: Principles of Computer Systems September 24, 2018

Learning from Bad Examples. CSCI 5828: Foundations of Software Engineering Lecture 25 11/18/2014

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Steve Gribble. Synchronization. Threads cooperate in multithreaded programs

Last Class: Synchronization

Week 7. Concurrent Programming: Thread Synchronization. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

CMSC 132: Object-Oriented Programming II

Informatica 3. Marcello Restelli. Laurea in Ingegneria Informatica Politecnico di Milano 9/15/07 10/29/07

Handouts. 1 Handout for today! Recap. Homework #2 feedback. Last Time. What did you think? HW3a: ThreadBank. Today. Small assignment.

sample exam Concurrent Programming tda383/dit390 Sample exam March 2016 Time:?? Place: Johanneberg

Synchronization I. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Memory system behavior: volatile variables. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 6. Volatile variables and concurrency

CSE 120. Fall Lecture 6: Semaphores. Keith Marzullo

1 Process Coordination

Dr. D. M. Akbar Hussain DE5 Department of Electronic Systems

Question Points Score Total 100

Problems with Concurrency

CMSC 132: Object-Oriented Programming II. Threads in Java

Concurrency with Threads and Actors

CSE 153 Design of Operating Systems

Lecture 6 (cont.): Semaphores and Monitors

Monitors; Software Transactional Memory

Dealing with Issues for Interprocess Communication

Module - 4 Multi-Threaded Programming

CmpSci 187: Programming with Data Structures Spring 2015

Synchronization COMPSCI 386

Synchronization Possibilities and Features in Java

CS180 Review. Recitation Week 15

CS 159: Parallel Processing

CISC 4700 L01 Network & Client-Server Programming Spring Cowell Chapter 15: Writing Threaded Applications

CS193k, Stanford Handout #8. Threads 3

CS 455: INTRODUCTION TO DISTRIBUTED SYSTEMS [THREADS] Frequently asked questions from the previous class survey

F. Tip and M. Weintraub CONCURRENCY

CMSC 330: Organization of Programming Languages

Java Threads. Written by John Bell for CS 342, Spring 2018

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Hank Levy 412 Sieg Hall

CSE 160 Lecture 7. C++11 threads C++11 memory model

Project. Threads. Plan for today. Before we begin. Thread. Thread. Minimum submission. Synchronization TSP. Thread synchronization. Any questions?

MCS-378 Intraterm Exam 1 Serial #:

Transcription:

Homework 12 Due Wednesday, 12/8/10 Please turn in your homework solutions online at http://www.dci.pomona.edu/tools-bin/cs131upload.php before the beginning of class on the due date. 1. (10 points) Atomicity & Race Conditions The DoubleCounter class defined below has methods incrementboth and getdifference. Assume that DoubleCounter will be used in multi-threaded applications. class DoubleCounter { protected int x = 0, y = 0; public int getdifference() { return x - y; public void incrementboth() { x++; y++; There is a potential data race between incrementboth and getdifference if getdifference is called between the increment of x and the increment of y. (a) What are the possible return values of getdifference if there are 2 threads? (b) What are the possible return values of getdifference if there are n threads? (c) Data races can be prevented by inserting synchronization primitives. One option is to declare public synchronized int getdifference() {... public int incrementboth() {... This will prevent two threads from executing method getdifference at the same time. Is this enough to ensure that getdifference always returns 0? Explain briefly. (d) Is the following declaration public int getdifference() {... public synchronized int incrementboth() {... sufficient to ensure that getdifference always returns 0? Explain briefly. (e) What are the possible values of getdifference if the following declarations are used? 1

public synchronized int getdifference() {... public synchronized int incrementboth() {... 2. (10 points) Concurrent Access to Objects Please do problem 14.6 from Mitchell, page 471. 3. (10 points) Java synchronized objects Please do problem 14.7, parts a,b,c, and e, from Mitchell, page 472. For part e, consider the following variant of the program (rather than the modifications they suggest in part d, which you get to skip): BoundedBuffer from the text: public class BoundedBuffer { protected int MaxBuffSize; private int[] buffer; private int numslots = 0; private int takeout = 0, putin = 0; private int count=0; private Object lockput = new Object(); private Object lockget = new Object(); public BoundedBuffer(int numslots) { if(numslots < 0) { throw new IllegalArgumentException("numSlots <= 0"); this.numslots = numslots; buffer = new int[numslots]; public void put(int value) throws InterruptedException { synchronized (lockput) { while (count == numslots) lockput.wait(); buffer[putin] = value; putin = (putin + 1) % numslots; count++; synchronized(lockget){ lockget.notifyall(); public int get() throws InterruptedException { int value; 2

synchronized (lockget) { while (count == 0) lockget.wait(); value = buffer[takeout]; takeout = (takeout + 1) % numslots; count--; synchronized(lockput){ lockput.notifyall(); return value; Note how the use of two different locks allows us to avoid two threads executing the get method or two threads executing the put method, but allows one thread to execute get while another executes put. Also notice that Java only allows a method to send a notifyall() message to a lock if it holds the lock. Thus put must grab the lockget lock before it can notify those processes waiting to do a get that there are now elements that can be grabbed. 4. (20 points) Using Bounded Buffer The Haskell function, primesto n, given below, can be used to calculate the list of all primes less than or equal to n by using a variant of the classic Sieve of Eratosthenes. {- Sieve of Eratosthenes: Remove all multiples of first element from list, then repeat sieving with the tail of the list. If start with list [2..n] then will find all primes from 2 up to and including n. - sieve :: (Integral a) => [a] -> [a] sieve [] = [] sieve (fst:rest) = let nurest = filter (\n -> (n mod fst) /= 0) rest in fst:(sieve nurest) -- return list of primes from 2 to n primesto :: (Integral t) => t -> [t] primesto n = sieve [2..n]; Notice that each time through the sieve we first filter all of the multiples of the first element from the tail of the list, and then perform the sieve on the reduced tail. In an eager language, one must wait until the entire tail has been filtered before you can start the sieve on the resulting list. In Haskell, it will perform the calculation on demand and only calculate as much as is needed. For example if we displayed only the first element of the result of primesto 100, then only that element would be calculated. If we have spare processors, one could use parallelism to have one 3

process start sieving the result before the entire tail had been completely filtered by the original process. Here is a good way to think of this concurrent algorithm that will use the Java BoundedBuffer class defined in the previous problem. The main program should begin by creating a BoundedBuffer object (say with 5 slots) and then should successively insert the numbers from 2 to n (for some fixed n) into the BoundedBuffer using the insert method, and finally put in -1 to signal that it is the last element. After the creation of the BoundedBuffer object, but before starting to put the numbers into the BoundedBuffer, the program should create a Sieve object (using the Sieve class described below) and pass it the BoundedBuffer object (as a parameter to Sieve s constructor). The Sieve object should then begin running in a separate thread while the main program inserts the numbers in the buffer. After the Sieve object has been constructed and the BoundedBuffer object stored in an instance variable, in, its run method should get the first item from in using the delete method. If that number is negative then the run method should terminate. Otherwise it should print out the number (System.out.println is fine) and then create a new BoundedBuffer object, out. A new Sieve should be created with BoundedBuffer out and started running in a new thread. Meanwhile the current Sieve object should start filtering the elements from the in buffer. That is, the run method should successively grab numbers from the in buffer, checking to see if each is divisible by the first number that was obtained from in. If a number is divisible, then it is discarded, otherwise it is put on buffer out. This reading and filtering continues until a negative number is read. When the negative number is read then it is put into the out buffer and then the run method terminates. If all of this works successfully then the program will eventually have created a total of p + 1 objects of class Sieve (all running in separate threads), where p is the number of primes between 2 and n. The instances of Sieve will be working in a pipeline, using the buffers to pass numbers from one Sieve object to the next. Please write this program in Java using the BoundedBuffer class from the previous problem. Each of the buffers used should be able to hold at most 5 items. 5. (15 points) Sieve with Actors Rewrite the Sieve of Eratosthenes program from above in Scala using Actors (and no BoundedBuffer). In the Java program you wrote, you created a new Thread for every prime number you discovered. This time, you will create a new Sieve actor for each prime Int. Hints: Your Sieve actor should keep track of the prime it was created with, and it should have a slot that can hold another follower Sieve actor that will handle the next prime found. (The mailbox of this other Actor will play the role of the BoundedBuffer from the previous problem in that it will hold the numbers being passed along from one actor to the next.) Each Sieve actor should be able to handle messages of the form Number(n) and Stop. The operation of each Sieve actor, once started, is similar to the previous problem. If it receives a message of the form Number(n) then it checks if it is divisible by its prime. If so, it is discarded. If not, then if the follower Actor has not yet been created, create it with the number. If not, then send the number to the follower Sieve actor. When the Stop message is sent, pass it on to the follower (if any), and exit. This program works best with a receive and a while loop (like in the Auction example) rather than react and loop (as in the PingPong and BoundedBuffer actor examples from class). 4

Your program should print (in order) all of the primes less than 100. You can print each prime as it is discovered (e.g., when you create the corresponding Sieve actor), but it would be even better to return a list of all of the primes, and then print those out. To do this, you can send the message Stop synchronously with!? and get back the list of all primes. The!? operator returns an object of type Any (equivalent to Java s Object), so you ll have to use match to decode it as a list of Int. 5