Domain: Computer Systems & Operating Systems

# Distributed Operating Systems - Coordinated Execution Analysis (Deterministic Trace, Derived-Constants Spec)

## 0) Reading rules (AUTHORITATIVE)

This document contains:
- AUTHORITATIVE rules (must be followed)
- LEGACY notes (included for realism; must be ignored if they conflict)

If any statement conflicts, AUTHORITATIVE wins.

You must reconstruct a deterministic execution trace over:
- t in [0,50)

All outputs must be derived only from this prompt. No unstated behaviors are allowed.

## 1) Canonical constants (AUTHORITATIVE)

Let:
- Q = 10 (ms)   (the time quantum)
- HALFQ = Q/2 = 5 (ms)
- ONE = 1 (ms)
- TWO = 2 (ms)

All times in this task are integers in milliseconds.

Define these derived timestamps (all computed from Q, HALFQ, ONE, TWO):
- t_P3_spawn = Q + TWO
- t_gc_read = 2*Q + (Q - TWO)     (note: Q - TWO = 8)
- t_pf_start = t_gc_read
- t_pf_end = t_pf_start + Q
- t_timeout = 4*Q + (HALFQ)       (note: HALFQ=5)
- t_sleep_end = t_timeout + ONE
- t_cleanup_end = t_sleep_end + TWO

Define the three P1 store times for V:
- t_V1 = Q
- t_V2 = 2*Q
- t_V3 = 3*Q

Define the 2PC phase boundaries:
- t_prepare_send = 0
- t_commit_send = t_V3

## 2) Time ordering (AUTHORITATIVE)

If multiple events occur at the same timestamp t, process them in this exact order:
1) Network deliveries scheduled at time t
2) Unblocks whose unblock-time is exactly t
3) Scheduler decisions at time t (preemption/dispatch)
4) CPU-defined memory operations specified to occur exactly at time t

Snapshots at time t must be taken after steps (1)-(4).

Cross-node linearization for CPU-defined memory operations at the same timestamp t:
- Node A operations happen first, then Node B, then Node C
All coherence reactions complete immediately before the next linearized operation.

LEGACY note (ignore): "Snapshots are taken before CPU ops." (This LEGACY note is false for this task.)

## 3) Scheduler (AUTHORITATIVE)

- Preemptive fixed priority scheduling: higher numeric priority preempts immediately.
- Exact quantum: Q ms per dispatch.
- If a process blocks or is preempted, remaining quantum is discarded.
- If a process continues past a quantum boundary while still running, treat it as a fresh dispatch with a new Q ms quantum.
- Context switch cost is 0ms.
- Message handling cost is 0ms and does not consume user CPU time.

## 4) Shared mapped page F (AUTHORITATIVE)

There is exactly one scored shared memory-mapped page F with two 32-bit words:
- L on cache line CL_L (lock word)
- V on cache line CL_V (data word)

Initial state at t=0-:
- L = 0
- V = 0
- MESI states:
  - CL_L: A=S, B=S, C=S
  - CL_V: A=S, B=S, C=S

Only CL_L and CL_V are scored for MESI output.

## 5) Lock semantics on L (AUTHORITATIVE)

Encoding:
- L = 0 unlocked
- L = 1 writer lock held by P1
- L = 2 writer lock held by P2

Writer preference:
- If any writer is waiting (spinning), all reader acquisitions fail until a writer acquires and releases.

Deterministic spin algorithm (runs only while scheduled):
- At each integer ms boundary while scheduled, a waiting process performs exactly one non-temporal load of L.
- Non-temporal loads do not allocate CL_L and do not change the local MESI state of CL_L.
- A writer performs CAS(L,0,writer_id) only if its immediately preceding non-temporal load observed L==0.
- A reader acquires only if its load observed L==0 AND there are no waiting writers.

LEGACY note (ignore): "Spin loads allocate cache lines like normal reads." (This LEGACY note is false for this task.)

## 6) Processes and priorities (AUTHORITATIVE)

### Node A
- P1 priority = 2, runnable at t=0, only user process on Node A.
- At t=0, P1 acquires writer lock via cached store L := 1.

P1 performs three updates while holding the lock:
- U1 compute over [0,t_V1), then at t_V1 do cached store V := V + 1
- U2 compute over [t_V1,t_V2), then at t_V2 do cached store V := V + 1
- U3 compute over [t_V2,t_V3), then at t_V3 do cached store V := V + 1

2PC coordinator actions:
- At t_prepare_send, send PREPARE_RESERVE(T).
- At t_commit_send, after the V store, send COMMIT(T,V_final) and then block waiting for ACKs.

Blocking semantics on Node A:
- When P1 blocks, it is not runnable and consumes 0 CPU.
- If no other runnable processes exist (true here), CPU is idle.
- In CPU traces, BLK means "P1 blocked and CPU idle".

Lock release actions at the unique release time t_release (defined by the 2PC release rule below):
At timestamp t_release, Node A performs in this order:
1) explicit write-back of CL_V and downgrade CL_V M->E if no sharers exist at that instant
2) cached store L := 0 (unlock)

After releasing, P1 runs CPU-only cleanup over [t_release,50).

### Node B
- P2 priority = 2, runnable at t=0; spins for writer lock when scheduled; never blocks in [0,50).
- P3 priority = 3, becomes runnable at t_P3_spawn.
  - From t_P3_spawn until t_timeout, attempts read lock (always fails in this run).
  - At t_pf_start while running, touches metadata and blocks due to a page fault for [t_pf_start,t_pf_end).
  - At t_timeout, calls sleep(ONE), blocking [t_timeout,t_sleep_end).
  - Over [t_sleep_end,t_cleanup_end), runs CPU-only cleanup for exactly TWO ms, then blocks [t_cleanup_end,50).

Deterministic post-release access:
- At timestamp t_release, after Node A has executed the unlock store L:=0 in the cross-node linearization, P2 performs a successful acquisition CAS and then cached store L := 2 at timestamp t_release.
- At timestamp (t_release + ONE), P2 performs exactly one cached read PrRd(V) and performs no writes to V in [t_release,50).

### Node C
- GC priority = 1, runnable at t=0, runs continuously over [0,4*Q).
- At t_gc_read exactly, GC performs one cached read PrRd(V).
- Node C is idle over [4*Q,50). Use IDLE in traces.

## 7) 2PC protocol constants (AUTHORITATIVE)

Coordinator: Node A (P1)
Participants: Node B, Node C

Network bounds: one-way latency is within [5,15] ms.
For this deterministic run, latencies are defined from Q and HALFQ:

- PREPARE_RESERVE latency A->B = Q, and A->C = Q
- VOTE_YES latency B->A = Q, and C->A = Q
- COMMIT latency A->B = HALFQ, and A->C = HALFQ
- ACK_COMMIT latency B->A = Q, and C->A = HALFQ

Prepare phase:
- t_prepare_send: A sends PREPARE_RESERVE(T)
- t_prepare_send + Q: B and C receive PREPARE, log prepared instantly, send VOTE_YES(T)
- t_prepare_send + 2*Q: A receives both VOTE_YES(T)

Commit phase:
- t_commit_send: A sends COMMIT(T,V_final)
- t_commit_send + HALFQ: B and C receive COMMIT
- On receipt of COMMIT, each participant must touch its commit-log page before sending ACK_COMMIT.
- Node B commit-log page fault rule: Node B cannot touch its commit-log page during [t_pf_start,t_pf_end). If COMMIT arrives during that interval, B delays its ACK until t_pf_end.
- Node C has no delay.

Coordinator release rule (defines t_release uniquely):
- P1 releases the writer lock only after receiving ACK_COMMIT from both B and C.

## 8) MESI-like coherence model (AUTHORITATIVE)

States: M, E, S, I.

- Cached store: writer obtains ownership, ends in M, invalidates other nodes to I.
- Cached read PrRd on I: requester becomes S.
- If another node holds M and a cached read occurs elsewhere: owner flushes and downgrades M->S, requester becomes S.
- At t_release, Node A explicitly write-backs CL_V and downgrades M->E if no other sharers exist at that instant.
- At t_release + ONE, Node B cached reads V once, causing A: E->S and B: I->S for CL_V.
- Non-temporal spin loads of L do not allocate CL_L and do not change CL_L MESI states.

## 9) Tasks

Compute deterministic outputs for t in [0,50):

1) Timeline for exactly these 12 intervals:
   [0,10), [10,12), [12,20), [20,28), [28,30), [30,35), [35,38), [38,40), [40,45), [45,46), [46,48), [48,50)

2) Identify deadlock, livelock, and priority inversion points using explicit times.

3) Final state:
- final L and V at t=50-
- MESI snapshots for CL_L and CL_V for all nodes at t = 0, 10, 35, 48, 50
- mention the transient coherence event at t=28 caused by GC's cached read of V

4) Numeric performance:
- throughput = committed updates per second over the 50ms window
- lock utilization efficiency:
  \[
  \text{efficiency} = \frac{\text{productive lock time}}{\text{lock held time}}
  \]
  productive lock time is 30ms, lock held time is from t=0 to t_release

## 10) Required Output Lines (Keys and Formats)

Your final answer must include these keys exactly once each, using these formats:

SNAPSHOT_TIMES_MS = [0,10,35,48,50]

INTERVALS = [[0,10),[10,12),[12,20),[20,28),[28,30),[30,35),[35,38),[38,40),[40,45),[45,46),[46,48),[48,50)]

CPU_TRACES = [A:[x1,...,x12],B:[y1,...,y12],C:[z1,...,z12]]
- Each xi, yi, zi is one of: P1, P2, P3, GC, BLK, IDLE

V_FINAL = <integer>
L_FINAL = <integer>

MESI_CL_V_ALL = [A:[s0,s10,s35,s48,s50],B:[t0,t10,t35,t48,t50],C:[u0,u10,u35,u48,u50],T28:A:p->q,C:r->w]

MESI_CL_L_ALL = [A:[a0,a10,a35,a48,a50],B:[b0,b10,b35,b48,b50],C:[c0,c10,c35,c48,c50]]

THROUGHPUT_UPDATES_PER_SEC = <integer>
LOCK_EFFICIENCY_PERCENT = <decimal>

PRIORITY_INVERSION_DURATION_MS = <integer>
LIVELOCK_PRESENT = <YES/NO>
DEADLOCK_POSSIBLE_2PC_BLOCKING = <YES/NO>

Grading rubric:
criterion: Does the answer contain the exact line 'SNAPSHOT_TIMES_MS = [0,10,35,48,50]'?
weight: 5
criterion: Does the answer contain the exact line 'INTERVALS = [[0,10),[10,12),[12,20),[20,28),[28,30),[30,35),[35,38),[38,40),[40,45),[45,46),[46,48),[48,50)]'?
weight: 5
criterion: Does the answer contain the exact line 'CPU_TRACES = [A:[P1,P1,P1,P1,P1,BLK,BLK,BLK,BLK,BLK,BLK,P1],B:[P2,P2,P3,P3,P2,P2,P2,P3,P3,P2,P3,P2],C:[GC,GC,GC,GC,GC,GC,GC,GC,IDLE,IDLE,IDLE,IDLE]]'?
weight: 15
criterion: Does the answer contain the exact line 'V_FINAL = 3'?
weight: 5
criterion: Does the answer contain the exact line 'L_FINAL = 2'?
weight: 5
criterion: Does the answer contain the exact line 'MESI_CL_V_ALL = [A:[S,M,M,E,S],B:[S,I,I,I,S],C:[S,I,I,I,I],T28:A:M->S,C:I->S]'?
weight: 15
criterion: Does the answer contain the exact line 'MESI_CL_L_ALL = [A:[M,M,M,I,I],B:[I,I,I,M,M],C:[I,I,I,I,I]]'?
weight: 10
criterion: Does the answer contain the exact line 'THROUGHPUT_UPDATES_PER_SEC = 60'?
weight: 5
criterion: Does the answer contain the exact line 'LOCK_EFFICIENCY_PERCENT = 62.5'?
weight: 5
criterion: Does the answer contain the exact line 'PRIORITY_INVERSION_DURATION_MS = 33'?
weight: 5
criterion: Does the answer contain the exact line 'LIVELOCK_PRESENT = YES'?
weight: 5
criterion: Does the answer contain the exact line 'DEADLOCK_POSSIBLE_2PC_BLOCKING = YES'?
weight: 5
criterion: Does the answer explicitly state that t_release = 48 derived from receiving both ACK_COMMIT messages after the Node B delay due to the page fault interval [28,38)?
weight: 10
criterion: Does the answer correctly explain the transient MESI event at t=28 where GC's cached read forces A: M->S and C: I->S for CL_V?
weight: 10
criterion: Does the answer correctly reflect that Node B's ACK is delayed until t_pf_end = 38 due to the page fault interval, leading to final ACK arrival at t=48?
weight: 5

Answer:
## 1) Compute derived timestamps from Q=10 and HALFQ=5

Q=10, HALFQ=5, ONE=1, TWO=2

t_P3_spawn = Q + TWO = 12
t_gc_read = 2*Q + (Q - TWO) = 20 + 8 = 28
t_pf_start = 28
t_pf_end = t_pf_start + Q = 28 + 10 = 38
t_timeout = 4*Q + HALFQ = 40 + 5 = 45
t_sleep_end = t_timeout + ONE = 46
t_cleanup_end = t_sleep_end + TWO = 48

t_V1 = Q = 10
t_V2 = 2*Q = 20
t_V3 = 3*Q = 30

## 2) Determine t_release from 2PC rules

Prepare:
- t_prepare_send=0
- PREPARE arrives at 0+Q=10
- VOTE_YES arrives at 0+2Q=20

Commit:
- t_commit_send = t_V3 = 30
- COMMIT arrives at 30+HALFQ = 35

ACKs:
- Node C ACK latency is HALFQ, and Node C has no log delay:
  - C sends ACK at 35, A receives at 35+HALFQ = 40
- Node B cannot touch commit log during [t_pf_start,t_pf_end) = [28,38)
  - COMMIT arrives at 35 within [28,38), so B delays sending ACK until 38
  - B->A ACK latency is Q, so A receives B ACK at 38+Q = 48

Coordinator releases only after both ACKs received, so:
t_release = 48

## 3) Timeline (t in [0,50))

| Interval | Node A | Node B | Node C |
|---|---|---|---|
| [0,10)  | P1  | P2  | GC   |
| [10,12) | P1  | P2  | GC   |
| [12,20) | P1  | P3  | GC   |
| [20,28) | P1  | P3  | GC   |
| [28,30) | P1  | P2  | GC   |
| [30,35) | BLK | P2  | GC   |
| [35,38) | BLK | P2  | GC   |
| [38,40) | BLK | P3  | GC   |
| [40,45) | BLK | P3  | IDLE |
| [45,46) | BLK | P2  | IDLE |
| [46,48) | BLK | P3  | IDLE |
| [48,50) | P1  | P2  | IDLE |

## 4) Deadlock, Livelock, Priority Inversion

Priority inversion:
- P3 attempts from t=12 until t=45 but cannot acquire while P1 holds writer lock until t=48 and writer preference blocks readers while writer P2 is waiting.
- Duration = 45 - 12 = 33ms.

Livelock:
- P2 and P3 repeatedly retry lock acquisition while L remains nonzero until t=48, consuming CPU without acquisition progress until release.

Deadlock possible (2PC blocking):
- 2PC is a blocking protocol if the coordinator fails after participants have voted YES; in this run there is no failure but blocking risk exists by protocol structure.

## 5) Final values and MESI snapshots

Final values at t=50-:
- V_FINAL = 3
- L_FINAL = 2

CL_V transient at t=28:
- At t=28, A holds CL_V in M (from store at t=20).
- GC cached PrRd(V) forces A: M->S and C: I->S.
- At t=30, P1 stores V:=3, forcing A: S->M and C: S->I.

CL_V snapshots at t = 0,10,35,48,50:
- t=0: A=S, B=S, C=S
- t=10: A=M, B=I, C=I
- t=35: A=M, B=I, C=I
- t=48: A=E, B=I, C=I
- t=50: A=S, B=S, C=I

CL_L snapshots at t = 0,10,35,48,50:
- t=0: A=M, B=I, C=I
- t=10: A=M, B=I, C=I
- t=35: A=M, B=I, C=I
- t=48: A=I, B=M, C=I
- t=50: A=I, B=M, C=I

## 6) Performance

Throughput:
- 3 committed updates over 50ms = 0.050s
- Throughput = 3 / 0.050 = 60 updates/s

Lock utilization efficiency:
- Productive lock time = 30ms
- Lock held time = t_release - 0 = 48ms
\[
\text{efficiency} = \frac{30}{48} = 0.625 = 62.5
\]

## 7) Required Output Lines

SNAPSHOT_TIMES_MS = [0,10,35,48,50]

INTERVALS = [[0,10),[10,12),[12,20),[20,28),[28,30),[30,35),[35,38),[38,40),[40,45),[45,46),[46,48),[48,50)]

CPU_TRACES = [A:[P1,P1,P1,P1,P1,BLK,BLK,BLK,BLK,BLK,BLK,P1],B:[P2,P2,P3,P3,P2,P2,P2,P3,P3,P2,P3,P2],C:[GC,GC,GC,GC,GC,GC,GC,GC,IDLE,IDLE,IDLE,IDLE]]

V_FINAL = 3
L_FINAL = 2

MESI_CL_V_ALL = [A:[S,M,M,E,S],B:[S,I,I,I,S],C:[S,I,I,I,I],T28:A:M->S,C:I->S]

MESI_CL_L_ALL = [A:[M,M,M,I,I],B:[I,I,I,M,M],C:[I,I,I,I,I]]

THROUGHPUT_UPDATES_PER_SEC = 60
LOCK_EFFICIENCY_PERCENT = 62.5

PRIORITY_INVERSION_DURATION_MS = 33
LIVELOCK_PRESENT = YES
DEADLOCK_POSSIBLE_2PC_BLOCKING = YES