-
Notifications
You must be signed in to change notification settings - Fork 0
MY OS Notes
Madrajib Lab edited this page Dec 10, 2025
·
23 revisions
- File in mem -> Pipes
- Shared mem for communications
Inter -> comm betn two entity
Intra -> comm withing same entity (func calls)
- Inconsistency (incorrectness)
- Loss of Data
- Deadlock
-
Competition: Two process compete for accessing a shared resource
- Leads to deadlock and incosistency
- Cooperation : Execution of one task effects the other .eg producer consumer problem
- Critical section : accessing shared resource
-
Race Condition : concurrent access of processes to a critical section
- Final result of output depends on the order in which they finish their update.
- Preemption : A task can be preemted by other.
Has two major parts
- Entry Section
- Exit Section : its job is to notify other about CS availability
ENTRY -> CS -> EXIT
- Mutual Exclusion
-
Definition: If one process
$P_i$ is executing in its critical section, then no other process is allowed to be executing in its critical section. - Significance: This is the most crucial requirement, ensuring that shared resources (data structures, files, etc.) are accessed by only one process at a time, preventing race conditions and maintaining data integrity.
-
Definition: If one process
- Progress
- Definition: If no process is executing in its critical section and some processes wish to enter their critical sections, then only those processes that are not executing in their remainder section can participate in the decision of which will enter the critical section next.
- This selection cannot be postponed indefinitely.
- Significance: This prevents deadlock. It ensures that if the critical section is free, a waiting process can eventually enter it, and the system does not unnecessarily halt or wait.
- Bounded Wating
-
Definition: There exists a limit (a bound) on the number of times that other processes are allowed to enter their critical sections after a process
$P_i$ has made a request to enter its critical section and before that request is granted. - Significance: This prevents starvation. It guarantees that a process won't wait forever to enter the critical section while other processes repeatedly enter and exit it.
-
Definition: There exists a limit (a bound) on the number of times that other processes are allowed to enter their critical sections after a process
A synchronisation mechanism that satisfies all three is considered a correct solution to the critical section problem.
-
Mutual Exclusion
$\rightarrow$ Safety (ensures nothing bad happens - data integrity is preserved). -
Progress and Bounded Waiting
$\rightarrow$ Liveness (ensures something good eventually happens - processes can make progress).
- Simple Locks
- TSL (Test and Set Lock)
- XCHG / SWAP
- Strict Alternation
- Peterson's Solution
-
Simple Locks
- Busy Wait
- No mutual exclusion
lock = 0; // vacant lock = 1; // taken while (!lock); // Entry section lock = 1; lock = 0; // Exit section
test:
(1) push rbp
(2) mov rbp, rsp
(3) nop
.L2:
(4) mov eax, DWORD PTR lock[rip]
(5) test eax, eax
(6) je .L2
(7) mov DWORD PTR lock[rip], 1
// CS (critical section)- T1 : 1,2,3,4| T2: 1,2,3,4,5,6,7,CS | T1: 5,6,7,CS
-
When to use ?
- Only in theoretical examples, as they fail Mutual Exclusion due to race conditions (unless atomic).
-
TSL (Test Set Lock, atomic operations)
- Gurantees Mutual exclusion
- Gurantees Progress
- BW not gurateed.
(1) TSL LOCK, R0
(2) CMP R0, #0
(3) JNZ 1
(4) CS- T1: 1| T2: 1, 2, 3, CS, 1, 2, 3 | T2: 2, 3, 4
- Hardware support has to be present
- Not architectural neutral
-
when to use ?
- Multiprocessor Systems: When the critical section is very short. The overhead of a context switch would be greater than the time spent busy waiting.
-
Strict Alternation
- No Progres
- Gurantess MEX
- Gurantess B.W
- Every process waiting to endter CS has to check turn value, if not then wait.
// Task 0
while (trun != 0);
<CS>
turn 1;
//Task 2
while (turn != 1)
<CS>
turn 0;