CPU Scheduling

Answer & solution
Official answer: 2
1. SRTF always runs the available process with least unfinished CPU time. P1 runs from 0 to 1, leaving 2 ms. P2 arrives at 1 with burst 1, preempts P1, and finishes at 2.
2. P1 resumes at 2. When P3 arrives at 3, P1 has only 1 ms left versus P3's 3 ms, so P1 continues to completion at 4. P1 has waited 1 ms; P2 has waited 0; P3 has already waited 1.
3. At time 4, P4 arrives. If Z<3, P4 runs first for Z ms, then P3 runs for 3 ms. The total waiting time is W1+W2+W3+W4=1+0+(1+Z)+0=2+Z.
4. Average waiting 1 over four processes requires total waiting 4. Thus 2+Z=4, giving Z=2, which satisfies the branch assumption Z<3.
5. Check the other branch: if Z≥3, running P3 first adds a wait of 3 to P4, giving total 1+0+1+3=5. For Z=3 either tie order gives the same total, so this branch cannot give average 1.
6. With Z=2, completion times are P1=4, P2=2, P4=6, P3=9. Using waiting=completion−arrival−burst gives 1,0,0,3 respectively, totaling 4. Answer 2 ms.