DOC PREVIEW
UMD CMSC 330 - Practice Problems 7

This preview shows page 1 out of 3 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CMSC330 Fall 2009 Practice Problems 7 1. Multithreading, Data Races, and Deadlock a. For the following program, give two schedules under which the final value of i differs in the two schedules. Give the schedule as a list of line numbers, and in each case, also give the final value of i. l = new ReentrantLock(); m = new ReentrantLock(); i = 0 Thread 1 1. l.lock(); 2. i = 3; 3. l.unlock(); Thread 2 4. m.lock(); 5. i = i + 1; 6. m.unlock(); b. For the following program, give one schedule under which there will be a deadlock, and give one schedule under which there will not be a deadlock. Give the schedule as a list of line numbers. l = new ReentrantLock(); m = new ReentrantLock(); n = new ReentrantLock(); Thread 1 1. l.lock(); 2. m.lock(); 3. m.unlock(); 4. l.unlock(); Thread 2 5. m.lock(); 6. n.lock(); 7. n.unlock(); 8. m.unlock(); Thread 3 9. n.lock(); 10. l.lock(); 11. l.unlock(); 12. n.unlock();c. Using language notation similar to above, write a program that has no data races, but nonetheless may produce different output under different schedules. d. List all possible outputs from the following program. Indicate next to each possible output whether all threads complete at the end, or, if they do not, which threads remain blocked. l = new ReentrantLock(); c = l.newCondition() Thread 1 l.lock(); System.out.print("a "); c.await(); System.out.print("b "); l.unlock(); Thread 2 l.lock(); System.out.print("c "); c.signalAll(); System.out.print("d "); l.unlock(); 2. Multithreading Code a. Using Java Conditions, you must implement a synchronization construct called MyBarrier. A MyBarrier object is created with a certain value n. When a thread calls the method enter( ), it enters the barrier and blocks until a total of n threads have entered the barrier. When the nth threads enters the barrier, all the threads waiting at the barrier wake up and unblock, and the nth thread continues without blocking. When a thread calls the method reset( ), the barrier is reset so that it starts fresh in counting up to n (i.e., n more threads must enter the MyBarrier). You may start by modifying the following code fragment: public class MyBarrier { public void MyBarrier (int n) { … } public enter( ) { … } public reset( ) { … } } b. Implement MyBarrier using Ruby monitors. c. Write a Ruby program that creates a barrier for 2 threads, then creates 2 threads that each print out “hello”, enters the barrier, then prints out “goodbye”.3. Garbage collection Consider the following Java code. Object a, b, c; public foo( ) { a = new Object( ); // object 1 b = new Object( ); // object 2 c = new Object( ); // object 3 a = b; b = c; c = a; } a. What object(s) are garbage when foo( ) returns? Explain why. b. Describe the difference between mark-and-sweep &


View Full Document

UMD CMSC 330 - Practice Problems 7

Documents in this Course
Exam #1

Exam #1

6 pages

Quiz #1

Quiz #1

2 pages

Midterm 2

Midterm 2

12 pages

Exam #2

Exam #2

7 pages

Ocaml

Ocaml

7 pages

Parsing

Parsing

38 pages

Threads

Threads

12 pages

Ruby

Ruby

7 pages

Quiz #3

Quiz #3

2 pages

Threads

Threads

7 pages

Quiz #4

Quiz #4

2 pages

Exam #2

Exam #2

6 pages

Exam #1

Exam #1

6 pages

Threads

Threads

34 pages

Quiz #4

Quiz #4

2 pages

Threads

Threads

26 pages

Exam #2

Exam #2

9 pages

Exam #2

Exam #2

6 pages

Load more
Download Practice Problems 7
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Practice Problems 7 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Practice Problems 7 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?