Operating Systems ยท Unit 2 ยท 10 hrs
Process Management
Exam-focused notes for Process Management (Operating Systems, CSC264): what the TU syllabus asks and how it has actually been tested, with 19 solved past questions from this unit.
Process Management in short
This chapter studies the process as the operating system's central unit of execution: how a process differs from the program it runs, how the operating system tracks and switches between processes and threads, how independent processes coordinate access to shared data without racing each other, and how the scheduler decides which runnable process gets the CPU next.
What this unit covers
- Process vs Program
- Multiprogramming
- Process Model
- Process States
- Process Control Block
- Threads
- Thread vs Process
- User and Kernel Space Threads
- Inter Process Communication
- Race Condition
- Critical Section
- Implementing Mutual Exclusion: Mutual Exclusion with Busy Waiting (Disabling Interrupts, Lock Variables, Strict Alteration, Peterson's Solution, Test and Set Lock)
- Sleep and Wakeup
- Semaphore
- Monitors
- Message Passing
- Classical IPC problems: Producer Consumer, Sleeping Barber, Dining Philosopher Problem
- Process Scheduling: Goals, Batch System Scheduling (First-Come First-Served, Shortest Job First, Shortest Remaining Time Next), Interactive System Scheduling (Round-Robin Scheduling, Priority Scheduling, Multiple Queues), Overview of Real Time System Scheduling
Classical IPC problems
2081 ยท 10 marks
Explain the Sleeping Barber problem. Illustrate on how it can be solved.[10]
Semaphore
2081 ยท 5 marks
Explain how semaphore solves the problem of critical section. [5]
Process Scheduling
2081 ยท 5 marks
Calculate the average waiting time and turnaround time using priority algorithm (Priority 1 being the highest) for the given scenario:
$$\begin{array}{|c|c|c|c|} \hline \text{PID} & \text{Burst Time} & \text{Arrival Time} & \text{Priority} \ \hline A & 3 & 0 & 3 \ B & 2 & 2 & 3 \ C & 4 & 3 & 2 \ D & 2 & 3 & 1 \ \hline \end{array}$$
[5]
Show solved answerHide answer
Priority Scheduling (Non-Preemptive) - Average WT and TAT
Given
| PID | Burst Time | Arrival Time | Priority |
|---|---|---|---|
| A | 3 | 0 | 3 |
| B | 2 | 2 | 3 |
| C | 4 | 3 | 2 |
| D | 2 | 3 | 1 |
Priority 1 = highest. Assume non-preemptive priority scheduling.
Solution
Execution Order
- t = 0: Only A available โ A runs 0 to 3.
- t = 3: Ready = B (P3), C (P2), D (P1). Highest priority = D โ D runs 3 to 5.
- t = 5: Ready = B (P3), C (P2). Higher priority = C โ C runs 5 to 9.
- t = 9: Only B remains โ B runs 9 to 11.
Gantt Chart
| A | D | C | B |
0 3 5 9 11
Completion, Turnaround, Waiting Times
TAT = CT โ AT, WT = TAT โ BT
| PID | AT | BT | CT | TAT = CTโAT | WT = TATโBT |
|---|---|---|---|---|---|
| A | 0 | 3 | 3 | 3 | 0 |
| B | 2 | 2 | 11 | 9 | 7 |
| C | 3 | 4 | 9 | 6 | 2 |
| D | 3 | 2 | 5 | 2 | 0 |
Averages
$$\text{Avg TAT} = \frac{3 + 9 + 6 + 2}{4} = \frac{20}{4} = 5 \text{ ms}$$
$$\text{Avg WT} = \frac{0 + 7 + 2 + 0}{4} = \frac{9}{4} = 2.25 \text{ ms}$$
Final Result
| Metric | Value |
|---|---|
| Average Turnaround Time | 5 ms |
| Average Waiting Time | 2.25 ms |
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
Show solved answerHide answer
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 |
2079 ยท 5 marks
For the following dataset, compute average waiting time for SRTN and SJF.
| Process | Arrival Time | Burst Time |
|---|---|---|
| P0 | 0 | 7 |
| P1 | 2 | 4 |
| P2 | 4 | 1 |
| P3 | 5 | 4 |
[5]
Show solved answerHide answer
SRTN and SJF Scheduling - Average Waiting Time
Given
| Process | Arrival Time | Burst Time |
|---|---|---|
| P0 | 0 | 7 |
| P1 | 2 | 4 |
| P2 | 4 | 1 |
| P3 | 5 | 4 |
All required data present.
Solution
Part 1: SRTN (Shortest Remaining Time Next) - Preemptive
At each new arrival, compare remaining times and run the smallest.
Execution Trace
- t=0: Only P0. Run P0.
- t=2: P1 arrives (4). P0 remaining = 5. Since 4 < 5, run P1. (P0 rem = 5)
- t=4: P2 arrives (1). P1 remaining = 2. Since 1 < 2, run P2. (P0=5, P1=2)
- t=5: P2 finishes. P3 arrives (4). Ready: P1=2, P3=4, P0=5. Run P1.
- t=7: P1 finishes. Ready: P3=4, P0=5. Run P3.
- t=11: P3 finishes. Run P0 (5 remaining).
- t=16: P0 finishes.
Note: at $t=4$ the remaining time for P1 is $4 - 2 = \mathbf{2}$ units, since P1 ran from $t=2$ to $t=4$.
Gantt Chart
| P0 | P1 | P2 | P1 | P3 | P0 |
0 2 4 5 7 11 16
Waiting Time (WT = Completion - Arrival - Burst)
| Process | Completion | Arrival | Burst | Waiting Time |
|---|---|---|---|---|
| P0 | 16 | 0 | 7 | 16 - 0 - 7 = 9 |
| P1 | 7 | 2 | 4 | 7 - 2 - 4 = 1 |
| P2 | 5 | 4 | 1 | 5 - 4 - 1 = 0 |
| P3 | 11 | 5 | 4 | 11 - 5 - 4 = 2 |
$$\text{Average WT (SRTN)} = \frac{9 + 1 + 0 + 2}{4} = \frac{12}{4} = \boxed{3.0 \text{ ms}}$$
Part 2: SJF (Shortest Job First) - Non-Preemptive
- t=0: Only P0 arrived. Run P0 to completion โ finishes at 7.
- t=7: P1(4), P2(1), P3(4) all arrived. Shortest = P2. Run P2 โ finishes at 8.
- t=8: P1(4), P3(4). Tie broken by arrival โ P1. Finishes at 12.
- t=12: P3 runs โ finishes at 16.
Gantt Chart
| P0 | P2 | P1 | P3 |
0 7 8 12 16
Waiting Time
| Process | Completion | Arrival | Burst | Waiting Time |
|---|---|---|---|---|
| P0 | 7 | 0 | 7 | 0 |
| P1 | 12 | 2 | 4 | 6 |
| P2 | 8 | 4 | 1 | 3 |
| P3 | 16 | 5 | 4 | 7 |
$$\text{Average WT (SJF)} = \frac{0 + 6 + 3 + 7}{4} = \frac{16}{4} = \boxed{4.0 \text{ ms}}$$
Summary
| Algorithm | Average Waiting Time |
|---|---|
| SRTN (Preemptive) | 3.0 ms |
| SJF (Non-Preemptive) | 4.0 ms |
2078 ยท 5 marks
Discuss the concept of SJF and SRTN scheduling algorithms with suitable example. [5]
2076 ยท 10 marks
Interactive System Goals and Scheduling Algorithms
2080.1 ยท 10 marks
Why do we need to schedule process? Find the average waiting time and average turnaround time for the following set of processes using FCFS, SJF, RR (Quantum = 3) and shortest remaining time next.
| Process | CPU burst time | Arrival time |
|---|---|---|
| P1 | 20 | 0 |
| P2 | 25 | 15 |
| P3 | 10 | 30 |
| P4 | 15 | 45 |
[10]
Show solved answerHide answer
Process Scheduling Solution
Given Data
| Process | Burst Time | Arrival Time |
|---|---|---|
| P1 | 20 | 0 |
| P2 | 25 | 15 |
| P3 | 10 | 30 |
| P4 | 15 | 45 |
Quantum for RR = 3
Formulas:
- $TAT = CT - AT$
- $WT = TAT - BT$
Why Do We Need to Schedule Processes?
- CPU utilization: keep the CPU busy in a multiprogramming environment.
- Throughput: maximize processes completed per unit time.
- Fairness: give each process a reasonable share of CPU.
- Minimize waiting, turnaround, and response times.
- Prevent starvation and manage resources efficiently.
1. FCFS
Execution order by arrival: P1, P2, P3, P4.
Gantt: |P1|P2|P3|P4| at 0-20-45-55-70
| Process | AT | BT | CT | TAT | WT |
|---|---|---|---|---|---|
| P1 | 0 | 20 | 20 | 20 | 0 |
| P2 | 15 | 25 | 45 | 30 | 5 |
| P3 | 30 | 10 | 55 | 25 | 15 |
| P4 | 45 | 15 | 70 | 25 | 10 |
$$AWT = \frac{0+5+15+10}{4} = 7.5\text{ ms}$$ $$ATAT = \frac{20+30+25+25}{4} = 25\text{ ms}$$
2. SJF (Non-Preemptive)
- t=0: only P1 arrived โ run P1 (0-20).
- t=20: only P2 arrived โ run P2 (20-45).
- t=45: P3 (BT=10) and P4 (BT=15) available โ pick P3 (45-55).
- t=55: run P4 (55-70).
Same schedule as FCFS.
$$AWT = 7.5\text{ ms},\quad ATAT = 25\text{ ms}$$
3. Round Robin (Quantum = 3)
Ready queue rule: on preemption, newly arrived processes are inserted before the preempted process.
Trace:
- 0-3: P1 (17). Queue: [P1]
- 3-6: P1 (14). Queue: [P1]
- 6-9: P1 (11). Queue: [P1]
- 9-12: P1 (8). Queue: [P1]
- 12-15: P1 (5). At t=15 P2 arrives. Queue after: [P2, P1]
- 15-18: P2 (22). Queue: [P1, P2]
- 18-21: P1 (2). Queue: [P2, P1]
- 21-24: P2 (19). Queue: [P1, P2]
- 24-27: P1 (2โ0, runs 2 ms, done at 26).
To be precise. At 24-27 P1 has only 2 ms remaining, so:
- 24-26: P1 (2โ0, P1 completes at 26). Queue: [P2]. At t=30 P3 arrives later.
- 26-29: P2 (16). Queue: [P2]
- 29-32: P2 (13). P3 arrives at t=30. Queue after: [P3, P2]
- 32-35: P3 (7). Queue: [P2, P3]
- 35-38: P2 (10). Queue: [P3, P2]
- 38-41: P3 (4). Queue: [P2, P3]
- 41-44: P2 (7). Queue: [P3, P2]
- 44-47: P3 (1). P4 arrives at t=45. Queue after: [P2, P4, P3]
- 47-50: P2 (4). Queue: [P4, P3, P2]
- 50-53: P4 (12). Queue: [P3, P2, P4]
- 53-54: P3 (1โ0, P3 completes at 54). Queue: [P2, P4]
- 54-57: P2 (1). Queue: [P4, P2]
- 57-60: P4 (9). Queue: [P2, P4]
- 60-61: P2 (1โ0, P2 completes at 61). Queue: [P4]
- 61-64: P4 (6). Queue: [P4]
- 64-67: P4 (3). Queue: [P4]
- 67-70: P4 (3โ0, P4 completes at 70).
Completion times: P1=26, P2=61, P3=54, P4=70
| Process | AT | BT | CT | TAT | WT |
|---|---|---|---|---|---|
| P1 | 0 | 20 | 26 | 26 | 6 |
| P2 | 15 | 25 | 61 | 46 | 21 |
| P3 | 30 | 10 | 54 | 24 | 14 |
| P4 | 45 | 15 | 70 | 25 | 10 |
$$AWT = \frac{6+21+14+10}{4} = \frac{51}{4} = 12.75\text{ ms}$$ $$ATAT = \frac{26+46+24+25}{4} = \frac{121}{4} = 30.25\text{ ms}$$
(Note: RR ordering conventions vary; results depend on insertion order.)
4. Shortest Remaining Time Next (SRTN, Preemptive SJF)
Preempt whenever a newly arrived process has less remaining time.
- 0-15: P1 runs (only process). At t=15 P1 remaining = 5, P2 arrives (25). P1(5) < P2(25) โ continue P1.
- 15-20: P1 (5โ0, P1 completes at 20).
- 20-30: P2 runs. At t=30 P2 remaining = 15, P3 arrives (10). P3(10) < P2(15) โ preempt.
- 30-40: P3 (10โ0, P3 completes at 40).
- 40-45: P2 runs (15โ10). At t=45 P4 arrives (15). P2(10) < P4(15) โ continue P2.
- 45-55: P2 (10โ0, P2 completes at 55).
- 55-70: P4 (15โ0, P4 completes at 70).
Completion times: P1=20, P2=55, P3=40, P4=70
| Process | AT | BT | CT | TAT | WT |
|---|---|---|---|---|---|
| P1 | 0 | 20 | 20 | 20 | 0 |
| P2 | 15 | 25 | 55 | 40 | 15 |
| P3 | 30 | 10 | 40 | 10 | 0 |
| P4 | 45 | 15 | 70 | 25 | 10 |
$$AWT = \frac{0+15+0+10}{4} = \frac{25}{4} = 6.25\text{ ms}$$ $$ATAT = \frac{20+40+10+25}{4} = \frac{95}{4} = 23.75\text{ ms}$$
Summary
| Algorithm | Avg WT (ms) | Avg TAT (ms) |
|---|---|---|
| FCFS | 7.5 | 25 |
| SJF | 7.5 | 25 |
| RR (q=3) | 12.75 | 30.25 |
| SRTN | 6.25 | 23.75 |
Critical Section
2080 ยท 10 marks
How do you recognize critical section? Why do we need to synchronise it? Consider the request for the page references 7,0,1,2,0,3,0,4,2,3,0,3,2. Find the number of page fault for FIFO and LRU with 4 page frames.[10]
Implementing Mutual Exclusion
2080 ยท 5 marks
How can we achieve mutual exclusion? Describe. [5]
2079 ยท 5 marks
List any two demerits of disabling interrupt to achieve mutual exclusion. Describe about fixed and variable partitioning. [5]
Show solved answerHide answer
Answer
Two Demerits of Disabling Interrupts for Mutual Exclusion
According to the notes, the following are two demerits of disabling interrupts to achieve mutual exclusion:
-
Unsafe to give power to user processes: It is unwise to give user processes the power to turn off interrupts. If a user process disables interrupts and never re-enables them, the entire system could come to a halt. This makes the approach unsuitable for general user-level processes.
-
Not appropriate for user processes: Disabling interrupts is only a useful technique within the operating system itself. It is not appropriate for user processes because a malfunctioning or malicious process could permanently disable interrupts, causing the system to stop functioning correctly.
Fixed and Variable Partitioning
In a multiprogramming environment, several programs reside in primary memory at the same time and the CPU switches control between them. One way to support multiprogramming is to divide main memory into several partitions, each allocated to a single process. Depending on how and when partitions are created, there are two types:
(i) Fixed Partitioning (Static Partitioning)
- Memory is divided into several fixed-size partitions before execution begins.
- Each partition can accommodate only one program at a time.
- The number of programs residing in memory is bounded by the number of partitions.
- When a program terminates, its partition is freed for another program waiting in the queue.
- When a job arrives, it is placed in the input queue for the smallest partition large enough to hold it.
Demerit: Since partitions are fixed in size, any unused space within a partition cannot be used by other jobs and is simply wasted while that job runs. This is known as internal fragmentation.
(ii) Variable Partitioning (Dynamic Partitioning)
- Partitions are not fixed in advance; they are created dynamically at the time a process is loaded into memory.
- Each process is allocated exactly as much memory as it requires, so there is no internal fragmentation.
- As processes finish and leave memory, holes (free blocks) are created at various places in memory.
- Over time, memory becomes fragmented into many small holes scattered throughout, which is known as external fragmentation.
- Techniques such as compaction may be used to combine these holes into one large free block.
Demerit: External fragmentation occurs over time, making it difficult to allocate contiguous memory to new processes without compaction.
Comparison Table
| Feature | Fixed Partitioning | Variable Partitioning |
|---|---|---|
| Partition size | Fixed at system startup | Determined at load time |
| Internal fragmentation | Yes | No |
| External fragmentation | No | Yes |
| Flexibility | Low | High |
| Implementation | Simple | More complex |
2078 ยท 5 marks
What is lock variable? Discuss its working and problems associated with it in detail. [5]
2080.1 ยท 5 marks
How lock variable is used in achieving mutual exclusion? Describe. [5]
Thread vs Process
2080 ยท 5 marks
What makes thread different with process? Draw the transition diagram between states of a process. [5]
Race Condition
2079 ยท 10 marks
When does race condition occur in inter process communication? What does busy waiting mean and how it can be handled using sleep and wakeup strategy?[10]
Sleep and Wakeup
2078 ยท 10 marks
What kind of problem arises with sleep and wakeup mechanism of achieving mutual exclusion? Explain with suitable code snippet.[10]
User and Kernel Space Threads
2076 ยท 5 marks
When threads are better than processes? Explain the concept of user level threads in detail. [5]
Monitors
2076 ยท 5 marks
What is problem associated with semaphores? Explain the concept of monitors in brief. [5]
Process States
2080.1 ยท 5 marks
What is system call? Describe the transition between different states of process. [5]
Make Unit 2 stick
Practice CSC264 with flashcards & quizzes