MCP-S11-04 - LECTURE 4 – COORDINATION AND RESOURCES NYU...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

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

Unformatted text preview: LECTURE 4 – COORDINATION AND RESOURCES NYU Multicore Programming Class, Spring 2011 [email protected] 1 OS HW Task-oriented application (* Lecture 2) Concurrent Objects (*) Tasks (*) and synchronization primitives (Lecture 3 application concurrency support Context and today) Today we’ll go over cases that allow more (or different) concurrency than simple mutual exclusion. 2 NYU Multicore Programming, Spring 2011 • Reader-writer locks • Barriers • Case study: deadlock detection R W L o c k s B a r r ie r s C a s e S t Today’s agenda 3 t u d y NYU Multicore Programming, Spring 2011 R W L o c k s • Suppose that a resource is shared by several threads • Some of them may not want to alter the resource in any way, just read it, while others may want to modify the resource • Could we coordinate these writers and readers? Idea: a reader-writer lock • Each reader acquires a special lock that allows them to share the resource with other readers The reader-writer problem • A writer acquires another kind of lock that gives it exclusive access to the resource • The locks work in tandem to guarantee the resource’s consistency 4 NYU Multicore Programming, Spring 2011 reader_lock() lock(mutex) readcount += 1 if readcount == 1 then lock(w) unlock(mutex) reader_unlock() lock(mutex) adcount 1 writer_lock() lock(w) writer_unlock() unlock(w) int readcount = 0 semaphore mutex, w R W L o c k s A simple reader-writer lock Who is getting the priority here, readers or writers? readcount -= 1 if readcount = 0 then unlock(w) unlock(mutex) 5 NYU Multicore Programming, Spring 2011 reader_lock() lock(mutex) readcount += 1 if readcount == 1 then lock(w) unlock(mutex) reader_unlock() lock(mutex) adcount 1 writer_lock() lock(w) writer_unlock() unlock(w) int readcount = 1 semaphore mutex, w R W L o c k s readcount -= 1 if readcount = 0 then unlock(w) unlock(mutex) Reader arrives before writer What if yet a new readers arrives? 6 NYU Multicore Programming, Spring 2011 reader_lock() lock(mutex) readcount += 1 if readcount == 1 then lock(w) unlock(mutex) reader_unlock() lock(mutex) adcount 1 writer_lock() lock(w) writer_unlock() unlock(w) int readcount = 1 semaphore mutex, w R W L o c k s readcount -= 1 if readcount = 0 then unlock(w) unlock(mutex) Reader arrives before writer What if yet a new readers arrives? 7 NYU Multicore Programming, Spring 2011 reader_lock() lock(mutex) readcount += 1 if readcount == 1 then lock(w) unlock(mutex) reader_unlock() lock(mutex) adcount 1 writer_lock() lock(w) writer_unlock() unlock(w) int readcount = 2 semaphore mutex, w R W L o c k s readcount -= 1 if readcount = 0 then unlock(w) unlock(mutex) Readers may starve the writer 8 NYU Multicore Programming, Spring 2011 reader_lock() lock(mutex) readcount += 1 if readcount == 1 then lock(w) unlock(mutex) reader_unlock() lock(mutex) adcount 1 writer_lock() lock(w) writer_unlock() unlock(w) int readcount = 1 semaphore mutex, w R W L o c k s readcount -= 1 if readcount = 0 then unlock(w) unlock(mutex) Readers may starve the writer 9 NYU Multicore Programming, Spring 2011 reader_lock()...
View Full Document

This note was uploaded on 09/09/2011 for the course COMPUTER S MCP-S11 taught by Professor Robertgrimm during the Spring '11 term at NJIT.

Page1 / 36

MCP-S11-04 - LECTURE 4 – COORDINATION AND RESOURCES NYU...

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

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