Why synchronization prevents data corruption in Operating Systems - Performance Analysis
When multiple processes access shared data, synchronization controls their access to avoid conflicts.
We want to understand how synchronization affects the number of operations as more processes try to access data.
Analyze the time complexity of this synchronization example.
lock.acquire()
shared_data = shared_data + 1
lock.release()
This code shows a process locking access before updating shared data, then unlocking after.
Look at what repeats when many processes run this code.
- Primary operation: acquiring and releasing the lock around data update
- How many times: once per process trying to update shared data
As more processes try to update, each must wait for the lock, so operations grow with the number of processes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 lock acquire/release + 10 updates |
| 100 | 100 lock acquire/release + 100 updates |
| 1000 | 1000 lock acquire/release + 1000 updates |
Pattern observation: operations increase directly with the number of processes trying to update.
Time Complexity: O(n)
This means the total time grows linearly as more processes try to update the shared data safely.
[X] Wrong: "Synchronization makes updates instant and free of waiting."
[OK] Correct: Synchronization adds waiting time because processes must take turns, so it costs time proportional to the number of processes.
Understanding how synchronization affects operation growth helps you explain safe data access and performance trade-offs clearly.
What if we replaced the lock with a lock-free method? How would the time complexity change?
Practice
Solution
Step 1: Understand the problem of concurrent access
When multiple processes access shared data without control, they can overwrite each other's changes, causing data corruption.Step 2: Role of synchronization
Synchronization uses locks or similar methods to make sure only one process changes the data at a time, preventing corruption.Final Answer:
It prevents multiple processes from changing data at the same time, avoiding corruption. -> Option CQuick Check:
Synchronization stops simultaneous changes = B [OK]
- Thinking synchronization speeds up all processes
- Believing processes can safely ignore each other's changes
- Assuming synchronization deletes data automatically
Solution
Step 1: Identify correct lock usage
To protect shared data, a lock must be acquired before entering the critical section and released after finishing.Step 2: Match with options
lock.acquire() // critical section lock.release() shows acquiring the lock first, then releasing it after the critical section, which is correct.Final Answer:
lock.acquire() before and lock.release() after critical section -> Option BQuick Check:
Acquire before, release after = A [OK]
- Releasing lock before acquiring it
- Using wait/notify instead of acquire/release for locks
- Using start/stop which are not lock methods
Thread 1: temp = counter counter = temp + 1 Thread 2: temp = counter counter = temp + 1If both threads read the counter as 0 before either writes back, what is the final value of
counter if it started at 0?Solution
Step 1: Understand race condition without synchronization
Both threads read the counter as 0 before either writes back, so both write 1, losing one increment.Step 2: Calculate final counter value
Instead of 2, the counter ends at 1 because one increment overwrites the other.Final Answer:
1 -> Option AQuick Check:
Race condition causes lost update = 1 [OK]
- Assuming both increments always add up correctly
- Thinking counter stays 0 without increments
- Expecting counter to be 3 with only two increments
lock.acquire() shared_var = shared_var + 1 // missing lock.release()What is the main problem here?
Solution
Step 1: Identify missing lock release
The code acquires the lock but never releases it, so other processes cannot enter the critical section.Step 2: Understand effect on synchronization
This causes a deadlock where other processes wait forever, effectively blocking progress and causing issues.Final Answer:
The lock is never released, causing other processes to wait forever. -> Option DQuick Check:
Missing release causes deadlock = C [OK]
- Thinking lock acquired twice causes this problem
- Believing variable increment is incorrect here
- Assuming lock should be acquired after modification
Solution
Step 1: Understand the need for exclusive access
Multiple threads updating the same balance can cause lost updates if they run at the same time.Step 2: Choose synchronization method
Using a single lock ensures only one thread updates the balance at a time, preventing corruption.Step 3: Evaluate other options
Allowing no locks risks corruption; multiple locks on parts of balance is complex and error-prone; updating once at end misses intermediate changes.Final Answer:
Use a lock to allow only one thread to update the balance at a time. -> Option AQuick Check:
Single lock for exclusive update = A [OK]
- Ignoring synchronization for speed
- Using multiple locks unnecessarily
- Delaying updates without synchronization
Ready to prove it?
Pass the test and this pattern moves unknown
