CS FundamentalsVery hardGATE CS
Peterson's algorithm is written with `turn = i;` (giving the turn to ITSELF) instead of `turn = j;`: `flag[i] = true; turn = i; while (flag[j] && turn == j); CS; flag[i] = false;`. What goes wrong?
- A. Nothing; it is equivalent
- B. It deadlocks whenever both try
- C. Mutual exclusion can be violated: whoever writes turn last still gets in, and the other can already be inside
- D. It only violates bounded waiting
Correct answer: C. Mutual exclusion can be violated: whoever writes turn last still gets in, and the other can already be inside
P0 sets flag, turn = 0 and enters (P1 not interested). P1 sets flag, turn = 1; its test is flag[0] && turn == 0, which is false since turn = 1, so P1 also enters. Correct Peterson yields the turn to the other process.
More Operating Systems questions
- stdout is a pipe (fully buffered) and every fork() succeeds. What is the complete output, up to the order of the lines? c int main() {…
- How many times is "x" printed? (Every fork() succeeds; output unbuffered.) c int main() { for (int i = 0; i < 2; i++) { if (fork() == 0)…
- What values are printed (in any order)? (Every fork() succeeds.) c int main() { int x = 1; if (fork() == 0) { x = 2; if (fork() == 0) x =…
- How many processes are created in total, including the original? (Every fork() succeeds.) c int main() { for (int i = 0; i < 3; i++) if…
- Processes (arrival, burst): P1 (0, 7), P2 (2, 3), P3 (3, 1), P4 (6, 2). SRTF is used and every switch to a DIFFERENT process costs 1 ms…
- Processes on one CPU, Round Robin quantum 2: A arrives at 0 and needs CPU 3, then I/O 2, then CPU 1; B arrives at 1 and needs CPU 4. A…