Distributed S2
Distributed S2
20201474143
Problem 1 :
This schedule results in a lost update as the final values of A, B, C are affected by interleaving the
write operations of T1, T2, T3.
you can use locks. For example, you can use a write lock to ensure exclusive access to the data items
during write operations. In this scenario, each transaction would acquire a write lock before
performing a write operation and release it afterward.
T1: Write Lock(A), Read(A), Write Lock(B), Read(B), Write(A), Write(B), Release Lock(A, B)
T2: Write Lock(B), Read(B), Write Lock(C), Read(C), Write(B), Write(C), Release Lock(B, C)
T3: Write Lock(A, B, C), Read(A), Read(B), Read(C), Write(A), Write(B), Write(C), Release Lock(A, B, C)
By using write locks, we make sure that a transaction has exclusive access to the data items it is
writing to, preventing lost updates.
Problem 2:
Without control mechanisms, the final values of A and B could be inconsistent due to interleaving
of read and write operations in T1 and T2. For example:
The final values might vary depending on the interleaving, leading to potential inconsistencies.
2. Using Locks to Ensure Consistency:**
To ensure consistency, you can use locks. For example, you can use write locks to ensure exclusive
access to the accounts during write operations, preventing interleaving and maintaining consistency.
- T1: **Write Lock(A, B)**, Read(A), Read(B), Write(A), Write(B), **Release Lock(A, B)**
- T2: **Write Lock(B, A)**, Read(B), Read(A), Write(B), Write(A), **Release Lock(B, A)**
By using write locks, we ensure that a transaction has exclusive access to the accounts it is reading
from and writing to, ensuring inconsistencies.
Problem 3:
Without control mechanisms, the final values of A, B, and C could be inconsistent due to
interleaving of read and write operations in T1, T2, and T3. The final values might vary depending on
the interleaving, leading to potential inconsistencies.
To prevent inconsistency, you can use locks. For example, you can use write locks to ensure
exclusive access to the accounts during write operations, preventing interleaving and maintaining
consistency.
- T1: Write Lock(A, B), Read(A), Read(B), Write(A), Write(B), Release Lock(A, B)
- T2: Write Lock(B, C), Read(B), Read(C), Write(B), Write(C), Release Lock(B, C)
- T3: Write Lock(C, A), Read(C), Read(A), Write(C), Write(A), Release Lock(C, A)
By using write locks, we ensure that a transaction has exclusive access to the accounts it is reading
from and writing to, preventing inconsistencies
Problem 4 :
1. Issues Without Proper Transaction Management:
- Lost Update: Both users might believe they can purchase the last item, leading to a race condition
where one user’s update is lost.
- Inconsistent Data: The final inventory count may be incorrect due to interleaved and uncontrolled
updates.
- *Transactions: Implement a transaction for each user attempting to purchase the item. This
ensures atomicity, meaning the entire transaction (reserve and update inventory) either succeeds or
fails.
- *Locks: Use locks to ensure exclusive access to the inventory during the transaction. For example,
employ a write lock on the inventory when a user initiates the purchase transaction, preventing
other transactions from modifying the inventory concurrently.With proper transaction management
and locks, you can maintain consistency in the inventory, ensuring that only one user successfully
purchases the last item, and the inventory count is accurately updated.