CSC264 · 2080 · 10 marks
(question text pending review) # Answer ## When does the request switch from user mode to kernel mode? A request switches from user mode to kernel mode through a **system call** (also calle
(question text pending review) # Answer
When does the request switch from user mode to kernel mode?
A request switches from user mode to kernel mode through a system call (also calle
User Mode to Kernel Mode Switch + Process Scheduling
Given Data
| Process | Arrival Time (AT) | Burst Time (BT) | Priority |
|---|---|---|---|
| P0 | 0 | 5 | 1 (Lowest) |
| P1 | 1 | 3 | 4 (Highest) |
| P2 | 2 | 8 | 2 |
| P3 | 3 | 6 | 3 |
Priority convention (from question): higher number = higher priority (P1 = 4 = Highest).
Part 1: User Mode to Kernel Mode Switch
A CPU runs in two modes controlled by a mode bit:
- User Mode (bit = 1): application programs run with restricted privileges.
- Kernel Mode (bit = 0): the OS runs with full hardware access.
The switch from user mode to kernel mode occurs whenever a user process needs a privileged operation it cannot perform itself. This is triggered by:
| Trigger | Description |
|---|---|
| System Call | Process requests an OS service (file I/O, memory allocation) |
| Hardware Interrupt | External device signals CPU (keyboard, disk, timer) |
| Trap / Exception | Error condition (division by zero, page fault, invalid access) |
Mechanism: user process issues a system-call/trap instruction → CPU saves user state → mode bit set to 0 (kernel) → OS runs the system-call handler → on completion, mode bit set to 1 (user) → control returns to the process.
Example:
int fd = open("file.txt", O_RDONLY); // system call
The open() call traps into the kernel. The CPU switches to kernel mode, the kernel checks permissions and locates the file, returns a file descriptor, then switches back to user mode. The process cannot touch the disk directly, so it must request the kernel to act on its behalf.
Part 2: Process Scheduling
Algorithm 1: FCFS (Non-preemptive, by arrival order)
Order: P0 → P1 → P2 → P3
Gantt Chart
| P0 | P1 | P2 | P3 |
0 5 8 16 22
| Process | AT | BT | CT | TAT = CT-AT | WT = TAT-BT |
|---|---|---|---|---|---|
| P0 | 0 | 5 | 5 | 5 | 0 |
| P1 | 1 | 3 | 8 | 7 | 4 |
| P2 | 2 | 8 | 16 | 14 | 6 |
| P3 | 3 | 6 | 22 | 19 | 13 |
$$\text{Avg TAT} = \frac{5+7+14+19}{4} = \frac{45}{4} = 11.25 \text{ ms}$$ $$\text{Avg WT} = \frac{0+4+6+13}{4} = \frac{23}{4} = 5.75 \text{ ms}$$
Algorithm 2: Priority Scheduling (Non-preemptive, higher number = higher priority)
- t=0: only P0 arrived → run P0 (until t=5).
- t=5: P1(4), P2(2), P3(3) available → highest is P1 (until t=8).
- t=8: P2(2), P3(3) → highest is P3 (until t=14).
- t=14: only P2 → run (until t=22).
Gantt Chart
| P0 | P1 | P3 | P2 |
0 5 8 14 22
| Process | AT | BT | CT | TAT = CT-AT | WT = TAT-BT |
|---|---|---|---|---|---|
| P0 | 0 | 5 | 5 | 5 | 0 |
| P1 | 1 | 3 | 8 | 7 | 4 |
| P3 | 3 | 6 | 14 | 11 | 5 |
| P2 | 2 | 8 | 22 | 20 | 12 |
$$\text{Avg TAT} = \frac{5+7+11+20}{4} = \frac{43}{4} = 10.75 \text{ ms}$$ $$\text{Avg WT} = \frac{0+4+5+12}{4} = \frac{21}{4} = 5.25 \text{ ms}$$
Algorithm 3: Round Robin (Quantum = 2, preemptive)
BTs: P0=5, P1=3, P2=8, P3=6. Ready-queue ordering by arrival; a preempted process rejoins the tail after newly arrived processes.
Trace the ready queue (arrivals: P0@0, P1@1, P2@2, P3@3):
| Time | Run | Remaining after | Queue state after |
|---|---|---|---|
| 0-2 | P0 | 3 | P1 arrived@1, P2@2 present; queue: P1,P2,P0 (P3@3 next) |
| 2-4 | P1 | 1 | during run P3@3 arrives → queue: P2,P0,P3,P1 |
| 4-6 | P2 | 6 | queue: P0,P3,P1,P2 |
| 6-8 | P0 | 1 | queue: P3,P1,P2,P0 |
| 8-10 | P3 | 4 | queue: P1,P2,P0,P3 |
| 10-11 | P1 | 0 → CT=11 | queue: P2,P0,P3 |
| 11-13 | P2 | 4 | queue: P0,P3,P2 |
| 13-14 | P0 | 0 → CT=14 | queue: P3,P2 |
| 14-16 | P3 | 2 | queue: P2,P3 |
| 16-18 | P2 | 2 | queue: P3,P2 |
| 18-20 | P3 | 0 → CT=20 | queue: P2 |
| 20-22 | P2 | 0 → CT=22 | empty |
Gantt Chart
|P0|P1|P2|P0|P3|P1|P2|P0|P3|P2|P3|P2|
0 2 4 6 8 10 11 13 14 16 18 20 22
| Process | AT | BT | CT | TAT = CT-AT | WT = TAT-BT |
|---|---|---|---|---|---|
| P0 | 0 | 5 | 14 | 14 | 9 |
| P1 | 1 | 3 | 11 | 10 | 7 |
| P2 | 2 | 8 | 22 | 20 | 12 |
| P3 | 3 | 6 | 20 | 17 | 11 |
$$\text{Avg TAT} = \frac{14+10+20+17}{4} = \frac{61}{4} = 15.25 \text{ ms}$$ $$\text{Avg WT} = \frac{9+7+12+11}{4} = \frac{39}{4} = 9.75 \text{ ms}$$
Summary
| Algorithm | Avg Waiting Time | Avg Turnaround Time |
|---|---|---|
| FCFS | 5.75 ms | 11.25 ms |
| Priority (non-preemptive) | 5.25 ms | 10.75 ms |
| Round Robin (Q=2) | 9.75 ms | 15.25 ms |
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.