Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 16: Readers-Writers Problem and Message Passing

Similar presentations


Presentation on theme: "Lecture 16: Readers-Writers Problem and Message Passing"— Presentation transcript:

1 Lecture 16: Readers-Writers Problem and Message Passing

2 Review: Mutual Exclusion Solutions
Software solution Disabling interrupts Strict alternation Peterson’s solution Hardware solution TSL/XCHG Semaphore Conditional variables POSIX standards

3 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

4 DEADLOCK

5 In this lecture Readers/Writer problem Message passing

6 Classical problems Dining Philosophers Problem Readers-Writers Problem

7 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

8 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

9 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

10 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

11 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

12 Interprocess/thread communication
Shared variables Mutual exclusion Message passing

13 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)

14 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


Download ppt "Lecture 16: Readers-Writers Problem and Message Passing"

Similar presentations


Ads by Google