180 likes | 427 Views
Concurrent Control with “Readers” and “Writers”. Sagnik Bhattacharya Siddharth Dalal. Sequential Programs. Terminating Single thread or process Number of possible execution paths finite Constructions like if-then-else, while are strictly defined
E N D
Concurrent Control with “Readers” and “Writers” Sagnik Bhattacharya Siddharth Dalal
Sequential Programs • Terminating • Single thread or process • Number of possible execution paths finite • Constructions like if-then-else, while are strictly defined • Mathematical tools (though complicated) exist.
Concurrent Programs • Possibly non-terminating • Infinite number of possible execution paths • Synchronization • Scheduling • Prevention of starvation • Lack of formal tools to deal with concurrency
Readers and Writers Problem Reader Writer Critical Section Writer Reader Writer Reader
Readers and Writers Problem Reader Writer Critical Section Writer Reader Writer Reader YES!
Readers and Writers Problem Reader Writer Critical Section Writer Reader Writer Reader NO!
Readers and Writers Problem Reader Writer Critical Section Writer Reader Writer Reader NO!
The P and V operations • Sometimes referred to as wait and signal • P(S) : • while S < 0 do no-op; • S := S - 1; • V(S) : • S := S + 1;
Waiting readers have higher priority. Problem 1 GLOBALS mutex, w : semaphore := 1; // Mutual exclusion semaphores readcount : integer := 0;
Problem 1 READER P(mutex); readcount++; if (readcount=1) then P(w); V(mutex); CRITICAL REGION P(mutex); readcount--; if (readcount=0) then V(w); V(mutex); WRITER P(w); CRITICAL REGION V(w);
For Readers? NO!! For Writers? NO!! Does this guarantee FIFO order?
Waiting writers have higher priority Problem 2 GLOBALS mutex1 : semaphore := 1; // all semaphores here are mutual exclusionmutex2 : semaphore := 1;mutex3 : semaphore := 1;w : semaphore := 1;r : semaphore := 1;readcount, writecount : integer := 0;
Problem 2 READER P(mutex3); P(r); P(mutex1); readcount++; if (readcount is 1) then P(w); V(mutex1); V(r); V(mutex3); CRITICAL REGION P(mutex1); readcount--; if (readcount is 0) then V(w); V(mutex1);
Problem 2 WRITER P(mutex2); writecount++; if (writecount is 1) then P(r); V(mutex2); P(w); CRITICAL REGION V(w); P(mutex2); writecount--; if (writecount is 0) then V(r); V(mutex2);
Does this guarantee FIFO order? • For Readers? • NO!! • For Writers? • NO!!
To achieve FIFO order… • Make P and V operations more powerful • Use array of semaphores
To achieve prioritized order… • Inter-process communication • Complicated • Shared memory • Readers and Writers problem
Philosophical question • Should we build safety critical concurrent systems?