CS FundamentalsVery hardProduct company interview · GATE level
Two processes (i = 0, 1; j = 1 - i) share bool flag[2] = {false, false} and int turn, and each runs entry code, then its critical section, then flag[i] = false. Every individual read and write is atomic and sequentially consistent. Which entry code guarantees both mutual exclusion and freedom from deadlock? V1: turn = j; flag[i] = true; while (flag[j] && turn == j); V2: flag[i] = true; turn = i; while (flag[j] && turn == j); V3: flag[i] = true; turn = i; while (flag[j] && turn == i); V4: flag[i] = true; while (flag[j]);
- A. V1 and V4
- B. V1 only
- C. V2 and V3
- D. V3 only
Correct answer: D. V3 only
V3 is Peterson's algorithm with the roles of turn mirrored: whoever writes turn last is the one that waits, so both cannot pass the loop, and the earlier writer always gets through. V1 swaps the two writes: P0 sets turn = 1, P1 sets turn = 0 and flag[1], enters (flag[0] still false), then P0 sets flag[0] and sees turn == 0 != 1, entering too. V2 waits on the wrong value: P0 sets flag[0] and turn = 0 and enters while flag[1] is false; P1 then sets flag[1] and turn = 1, sees turn != 0 and walks in as well. V4 is safe but both can set their flags and then spin forever - a deadlock. All four were checked by exhaustively exploring every interleaving.
More Operating Systems questions
- How many lines print the value 3, and what is the sum of all the values printed? (Assume every fork() succeeds.) c int main() { int c = 0…
- A buddy allocator manages 1024 KB and splits blocks in powers of two, always using the lowest-addressed free block of the needed size…
- A disk has cylinders 0-499. The head is at 237 and is moving towards cylinder 0. Pending requests: 312, 45, 401, 198, 260, 12, 477, 150…
- A disk spins at 12,000 RPM, has 250 sectors of 512 bytes per track, an average seek time of 4 ms and a track-to-track seek time of 0.5 ms…
- A machine has 40-bit virtual addresses, 16 KB pages and 4-byte page-table entries. Every page table at every level must fit in a single…
- A 32-bit system uses two-level paging: 10 bits for the outer index, 10 bits for the inner index and a 12-bit offset, with 4-byte entries…