prac7-soln-fall09

prac7-soln-fall09 - CMSC330 Fall 2009 Practice Problems 7...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
CMSC330 Fall 2009 Practice Problems 7 Solutions 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(); Since l and m are different locks, they do not provide any mutual exclusion in this example, and the threads may be interleaved arbitrarily. Some example solutions: 1, 2, 3, 4, 5, 6 - i = 4 1, 4, 5, 2, 3, 6 - i = 3
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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(); Some example solutions: No deadlock - 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 Deadlock - 1, 5, 9 Because thread 1 holds l, thread 2 holds m, and thread 3 holds n, so none of the threads can make progress
Background image of page 2
c. Using
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/13/2012 for the course CMSC 330 taught by Professor Staff during the Fall '08 term at Maryland.

Page1 / 6

prac7-soln-fall09 - CMSC330 Fall 2009 Practice Problems 7...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online