






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Questions 1–8 reference seven different scenarios of interaction among concurrent actors that ac- cess a shared database object. The actors in this example perform transactions that invoke a series of methods on the database object, and we distinguish two different types of actors—readers and writers. To illustrate, suppose the database stores bank-account information and provides methods for depositing funds, withdrawing funds, and checking the balance of accounts given their account numbers. A typical reader might be interested in computing the total balance of a list of accounts and thus might execute the sequence of operations:
unsigned sum = 0;
for (unsigned i = 0; i < 10; ++i) { sum += db.get_balance(accounts[i]); }
On the other hand, a writer will perform a transaction that modifies the contents of the database. For example, a writer client might perform a transaction that transfers $50.00 between accounts acct 1 and acct 2 by executing the sequence of operations:
db.withdraw(acct_1, 50); db.deposit(acct_2, 50);
It should be safe for multiple reader transactions to execute concurrently because reader clients do not modify the contents of the database object. However, a writer transaction should never execute concurrently with any reader or any other writer transaction. Class Database supports this readers-writer style of synchronization by providing four methods—start read, stop read, start write, and stop write—which reader and writer threads use to signal the start and finish of one of these transactions. Figures 1–4 depict the C++ code for class Database as well as functions that implement the reader and writer actors. Notice that the “business logic” has been largely elided—for example, the account-management operations for class Database are absent. Class Database:
Please take a moment to familiarize yourself with this code.
Scenario 1: A scenario involving the main thread. Assume that the main thread is in the running state and has just executed the calls to spawn n on lines 83 and 84.
(a) A context switch occurs such that the main thread transitions to the ready state and one of the reader threads starts running at line 34.
(b) A context switch occurs such that the main thread transitions to the ready state and one of the writer threads starts running at line 73.
(c) The main thread remains in the running state and executes the call to wait on line 85, which causes it to transition to the ready state.
(d) A context switch occurs such that the main thread transitions to the blocked state and both reader threads transition to the running state.
(e) The main thread remains in the running state and all the newly spawned threads transi- tion to the blocked state.
Scenario 2: An interaction involving one reader R and one writer W. Assume W is in the running state within an invocation of start write and R is in the ready state. A context switch occurs just after W increments n writers by one. W transitions to the ready state, and R transitions to the running state. R invokes start read and quickly transitions to the blocked state.
(a) R enters the blocked state via the call to wait on line 50 because n writers is non-zero at the time.
(b) Deadlock occurs. Because W holds the lock, R can’t possibly acquire the lock.
(c) R enters the blocked state via the call to wait on line 36 because n writers is non-zero at the time.
(d) R enters the blocked state via the call to acquire on line 35.
(e) R enters the blocked state due to the occurrence of a context switch.
Scenario 4: An interaction involving one reader thread R and one writer thread W. Assume that after R is in the running state and has just returned from an invocation of start read. A context switch occurs such that R transitions to the ready state, and W transitions to the running state. W invokes start write.
(a) W remains running until the invocation completes.
(b) W transitions to the blocked state within the call to acquire on the lock.
(c) W obtains the lock but shortly thereafter releases the lock and transitions to the blocked state.
(d) R transitions to the blocked state.
(e) Deadlock occurs.
Scenario 5: An interaction involving two reader threads R 1 and R 2. Assume the R 1 is in the running state and has just completed the invocation of start read, and R 2 is in the ready state. A context switch occurs such that R 1 transitions to the ready state and R 2 transitions to the running state. R 2 issues a call to start read.
(a) R 2 acquires the lock and increases n Readers to two.
(b) R 2 acquires the lock but transitions to the blocked state via the call to wait on line 36 because n readers is non-zero at the time.
(c) R 2 acquires the lock but transitions to the blocked state via the call to wait on line 36 because n writers is non-zero at the time.
(d) R 2 transitions to the blocked state via the call to acquire on line 35.
(e) Deadlock occurs.
Scenario 6: An interaction involving two reader threads R 1 and R 2. Assume R 1 is in the run- ning state within an invocation of start read, and R 2 is in the ready state. A context switch occurs just after R 1 increments n readers by one. R 1 transitions to the ready state, and R 2 transitions to the running state. R 2 then invokes start read.
(a) R 2 acquires the lock but transitions to the blocked state via the call to wait on line 36 because n readers is non-zero.
(b) R 2 acquires the lock but transitions to the blocked state via the call to wait on line 36 because n writers is non-zero.
(c) R 2 acquires the lock and increases n readers to two.
(d) R 2 transitions to the blocked state via the call to acquire on line 35.
(e) Deadlock occurs.
Scenario 7: An interaction involving two writer threads W 1 and W 2. Assume W 1 is in the running state and has just completed its invocation of start write, and W 2 is in the ready state. A context switch occurs. W 1 transitions to the ready state, and W 2 transitions to the running state. W 2 issues a call to start write.
(a) W 2 acquires the lock and transitions to the blocked state via the call to wait on line 50, retaining the lock.
(b) W 2 acquires the lock and transitions to the blocked state via the call to wait on line 50, releasing the lock.
(c) W 2 transitions to the blocked state via the call to acquire on line 49.
(d) W 2 acquires the lock and increases n writers to two.
(e) Deadlock occurs.
1 #include <ace / Thread_Mutex .h > 2 #include <ace / Condition_Thread_Mutex .h >
3 class Database 4 { 5 public : 6 Database (); 7 8 void start_read () const ; 9 void stop_read () const ; 10 11 void start_write () const ; 12 void stop_write () const ; 13 14 // ... functions for reading/writing database entries ... 15 16 private : 17 // The number of readers currently reading 18 mutable unsigned n_readers ; 19 20 // The number of writers currently writing 21 mutable unsigned n_writers ; 22 23 mutable ACE_Thread_Mutex lock ; 24 mutable ACE_Condition_Thread_Mutex ok_to_read ; 25 mutable ACE_Condition_Thread_Mutex ok_to_write ; 26 27 // ... data structure for storing entries ... 28 };
Figure 1: Class definition for a database that allows concurrent readers. The database is imple- mented using the ACE library. For the sake of simplicity, we elided the “business logic” (e.g., functions for reading and writing database entries).
29 Database :: Database () 30 : n_readers (0) , n_writers (0) , 31 ok_to_read ( lock ), ok_to_write ( lock ) 32 {}
33 void Database :: start_read () const 34 { 35 lock. acquire (); 36 while ( n_writers > 0) ok_to_read. wait (); 37 ++ n_readers ; 38 lock. release (); 39 }
40 void Database :: stop_read () const 41 { 42 lock. acquire (); 43 -- n_readers ; 44 if ( n_readers == 0) ok_to_write. signal (); 45 lock. release (); 46 }
47 void Database :: start_write () const 48 { 49 lock. acquire (); 50 while ( n_writers > 0 || n_readers > 0) ok_to_write. wait (); 51 ++ n_writers ; 52 lock. release (); 53 }
54 void Database :: stop_write () const 55 { 56 lock. acquire (); 57 -- n_writers ; 58 ok_to_write. signal (); 59 ok_to_read. broadcast (); 60 lock. release (); 61 }
Figure 2: Member-function definitions for class Database.
1 class BankAccount 2 { 3 public : 4 BankAccount (); 5 void deposit ( double amount ); 6 void withdraw ( double amount ); 7 8 private : 9 double balance_ ; 10 ACE_Thread_Mutex lock_ ; // the monitor lock 11 ACE_Condition_Thread_Mutex okToWithdraw_ ; 12 };
13 BankAccount :: BankAccount () 14 : balance_ (0) , lock_ () , okToWithdraw_ ( lock_ ) 15 {}
16 void BankAccount :: deposit ( double amount ) 17 { 18 lock_. acquire (); 19 20 balance_ += amount ; 21 22 okToWithdraw_. broadcast (); 23 lock_. release (); 24 }
25 void BankAccount :: withdraw ( double amount ) 26 { 27 lock_. acquire (); 28 29 while ( amount > balance_ ) { 30 okToWithdraw_. wait (); 31 } 32 33 balance_ -= amount ; 34 35 lock_. release (); 36 }
Figure 5: A bank-account class whose design is based on the monitor pattern.