Lecture 16: Readers-Writers Problem and Message Passing
Review: Mutual Exclusion Solutions Software solution Disabling interrupts Strict alternation Peterson’s solution Hardware solution TSL/XCHG Semaphore Conditional variables POSIX standards
Review: Deadlock Thread 1 Thread 2 proc1( ) { pthread_mutex_lock(&m1); /* use object 1 */ pthread_mutex_lock(&m2); /* use objects 1 and 2 */ pthread_mutex_unlock(&m2); pthread_mutex_unlock(&m1); } proc2( ) { pthread_mutex_lock(&m2); /* use object 2 */ pthread_mutex_lock(&m1); /* use objects 1 and 2 */ pthread_mutex_unlock(&m1); pthread_mutex_unlock(&m2); } In this example our threads are using two mutexes to control access to two different objects. Thread 1, executing proc1, first takes mutex 1, then, while still holding mutex 1, obtains mutex 2. Thread 2, executing proc2, first takes mutex 2, then, while still holding mutex 2, obtains mutex 1. However, things do not always work out as planned. If thread 1 obtains mutex 1 and, at about the same time, thread 2 obtains mutex 2, then if thread 1 attempts to take mutex 2 and thread 2 attempts to take mutex 1, we have a deadlock. Thread 1 Thread 2 Mutex m1 Mutex m2 Thread 1 Thread 2
DEADLOCK
In this lecture Readers/Writer problem Message passing
Classical problems Dining Philosophers Problem Readers-Writers Problem
Readers-Writers Problem Multiple threads reading/writing a database Many threads can read simultaneously Only one can be writing at any time When a writer is executing, nobody else can read or write
Readers-Writers Problem Database has multiple threads operating Many threads can read simultaneously Only one can be writing at any time When a writer is executing, nobody else can read or write Example: Multithreaded web server cache http://www.theserverside.com/resources/articles/Readers-Writers/article.html
Solution Idea Readers: Writer: First reader locks the database If a reader inside, other readers enter without locking again Checking for readers occurs within a mutex Writer: Always lock database before entering
Readers-Writers Two semaphores: database protector While (1) { generate_data(); down(database); write(); up(database); } READER: While (1) { down(protector); rc++; if (rc == 1) //first reader down(database); up(protector); read(); rc--; If (rc == 0) then // last one up(database); …. } Two semaphores: database protector Can we replace database with a mutex? Initial: protector=1, database =1 rc =0
Writer Starvation Readers might continuously enter while a writer waits Writer Priority Solution? What does it mean to give writer priority? What is a fair solution? Give a specification
Interprocess/thread communication Shared variables Mutual exclusion Message passing
Message Passing No shared variables Two primitives: send (destination, message) receive (destination, message) Usually blocks till a message arrives Non-blocking version also usually available Message Passing Interface (MPI)
Issues Across different machines, message passing is the real thing Many issues to consider: Marshaling data into messages Provide reliable transmission across unreliable links? Event-driven mode of programming Computer Networking: deals with sending messages across machines