2080 1

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

Operating SystemsChapter 2: Process ManagementProcess SchedulingNumerical: CPU scheduling algorithmsVideo solution

User Mode to Kernel Mode Switch + Process Scheduling

Given Data

ProcessArrival Time (AT)Burst Time (BT)Priority
P0051 (Lowest)
P1134 (Highest)
P2282
P3363

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:

TriggerDescription
System CallProcess requests an OS service (file I/O, memory allocation)
Hardware InterruptExternal device signals CPU (keyboard, disk, timer)
Trap / ExceptionError 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
ProcessATBTCTTAT = CT-ATWT = TAT-BT
P005550
P113874
P22816146
P336221913

$$\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
ProcessATBTCTTAT = CT-ATWT = TAT-BT
P005550
P113874
P33614115
P228222012

$$\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):

TimeRunRemaining afterQueue state after
0-2P03P1 arrived@1, P2@2 present; queue: P1,P2,P0 (P3@3 next)
2-4P11during run P3@3 arrives → queue: P2,P0,P3,P1
4-6P26queue: P0,P3,P1,P2
6-8P01queue: P3,P1,P2,P0
8-10P34queue: P1,P2,P0,P3
10-11P10 → CT=11queue: P2,P0,P3
11-13P24queue: P0,P3,P2
13-14P00 → CT=14queue: P3,P2
14-16P32queue: P2,P3
16-18P22queue: P3,P2
18-20P30 → CT=20queue: P2
20-22P20 → CT=22empty

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
ProcessATBTCTTAT = CT-ATWT = TAT-BT
P00514149
P11311107
P228222012
P336201711

$$\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

AlgorithmAvg Waiting TimeAvg Turnaround Time
FCFS5.75 ms11.25 ms
Priority (non-preemptive)5.25 ms10.75 ms
Round Robin (Q=2)9.75 ms15.25 ms

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.