سوال ۱۰۲ کنکور ارشد مهندسی کامپیوتر ۱۴۰۰
کدام مورد سیستم عامل را مجبور میکند دستورات ، ، ، که بهترتیب در پردازههای همزمان ، ، ، قرار دارند به همان ترتیب ، ، ، اجرا کند؟ (مقدار اولیه سمافورها )
پاسخ صحیح: گزینهٔ A — : ؛ Signal(a) — : Wait(a)؛ ؛ Signal(b) — : Wait(b)؛ ؛ Signal(c) — : Wait(c)؛
To impose a strict execution order on statements that live in different concurrent processes, we build a precedence chain with counting semaphores initialized to . A semaphore initialized to acts as a one-shot baton: a Wait on it blocks until some other process does a matching Signal.
The desired order is
Each arrow is enforced by one semaphore: the successor Waits on it before running its statement, and the predecessor Signals it after running its statement.
Option 1 (correct):
P1: S1; Signal(a)
P2: Wait(a); S2; Signal(b)
P3: Wait(b); S3; Signal(c)
P4: Wait(c); S4
Trace with :
- hits
Wait(a)and blocks (because ). Only after runs and thenSignal(a)does become and proceed. Hence executes before . - Symmetrically
Wait(b)blocks until has finished and signalled , so . Wait(c)blocks until has finished and signalled , so .
The three constraints compose into the total order , exactly as required.
Why the others fail:
- Option 2 places each statement before its
Wait(S2; Wait(a)…). The statement is no longer guarded, so can run before their predecessors — no ordering is enforced. - Option 3
Signals before runs and lumps all theWaits into ; nothing serializes relative to each other. - Option 4 puts the
Signalof a semaphore in the successor and theWaitin the predecessor, reversing the baton direction; with the semaphores at this deadlocks ( blocks onWait(a)while theSignal(a)that would release it sits behind it in ), and even ignoring that it never produces the forward order.
So only Option 1 guarantees .
Exam trick: For a precedence chain, initialize the sync semaphores to and read each one as an arrow: the producer of the arrow does
Signalafter its statement, the consumer doesWaitbefore its statement. A statement that sits before itsWait, or aSignalplaced before the guarded statement, never enforces ordering.