---
title: "Priority and SJF scheduling with P4 and P5"
subject: "Embedded System"
chapter: 6
key: "semester_6/embedded_system:sched-priority-and-sjf-5-process"
source: https://www.bctengineer.com/q/semester_6/embedded_system/sched-priority-and-sjf-5-process
site: BCT Engineering
---

# Priority and SJF scheduling with P4 and P5

Embedded System · Chapter 6: RTOS

- Asked in: 2078 Baisakh, 3 marks
- Video: [L-2.4: Shortest Job First(SJF) Scheduling  Algorithm with  Example | Operating System](https://www.youtube.com/watch?v=VCIVXPoiLpU) — Gate Smashers
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_6/embedded_system/sched-priority-and-sjf-5-process
- Whole chapter: https://www.bctengineer.com/md/semester_6/embedded_system/chapter-6

## Question

Three processes with process ID P1, P2, P3 with estimated completion time 4, 10, 5 ms and priorities 1, 4, 3 respectively enter the ready queue together. New process P4 with estimated completion time 3 ms and priority 2 enters the ready queue after 5 ms of start of operation and P5 with estimated completion time 7 ms and priority 0 enters the ready queue after 2 ms of start of P4. Calculate WT, TAT for each process and calculate AWT and ATAT using preemptive priority-based and shortest job first scheduling algorithms.

## Short answer

Preemptive priority: AWT = 8 ms, ATAT = 13.8 ms. Preemptive SJF (SRT): AWT = 6.2 ms, ATAT = 12 ms.

## Answer

#### Preemptive priority

Assumed: P1, P2, P3 arrive at 0; P4 arrives at 5 ms; P5 arrives 2 ms after P4, i.e. at 7 ms. Lower priority number means higher priority. Burst times 4, 10, 5, 3, 7 ms; priorities 1, 4, 3, 2, 0.

**Gantt chart**

```text
| P1 | P3 | P4 | P5 | P4 | P3 | P2 |
0    4    5    7    14   15   19   29
```

**Trace**

- 0-4: P1 (priority 1, best of the three) runs and finishes.
- 4-5: P3 (priority 3) runs; it beats P2 (priority 4).
- 5: P4 (priority 2) arrives and preempts P3 (4 ms left).
- 7: P5 (priority 0) arrives and preempts P4 (1 ms left).
- 7-14: P5 runs to completion.
- 14-15: P4 finishes; 15-19: P3 finishes; 19-29: P2 runs.

**Calculation**

TAT = completion time - arrival time; WT = TAT - burst time.

| Process | AT | BT | CT | TAT = CT - AT | WT = TAT - BT |
|---|---|---|---|---|---|
| P1 | 0 | 4 | 4 | 4 | 0 |
| P2 | 0 | 10 | 29 | 29 | 19 |
| P3 | 0 | 5 | 19 | 19 | 14 |
| P4 | 5 | 3 | 15 | 10 | 7 |
| P5 | 7 | 7 | 14 | 7 | 0 |

AWT = (0 + 19 + 14 + 7 + 0) / 5 = 40/5 = 8 ms

ATAT = (4 + 29 + 19 + 10 + 7) / 5 = 69/5 = 13.8 ms

**Answer: AWT = 8 ms, ATAT = 13.8 ms.**

#### Shortest job first (preemptive)

Shortest job first, applied in its preemptive form (shortest remaining time first). The CPU always runs the process with the smallest remaining burst; priorities are ignored.

**Gantt chart**

```text
| P1 | P3 | P4 | P3 | P5 | P2 |
0    4    5    8    12   19   29
```

**Trace**

- 0-4: P1 (4 ms) is the shortest and finishes.
- 4-5: P3 (5 ms) runs, as P2 needs 10 ms.
- 5: P4 (3 ms) arrives; P3 has 4 ms left, so P4 preempts it.
- 5-8: P4 finishes. At 7, P5 (7 ms) arrives but is longer than the remaining 1 ms of P4, so no preemption.
- 8-12: P3 (4 left) finishes; 12-19: P5 (7 ms); 19-29: P2.

**Calculation**

TAT = completion time - arrival time; WT = TAT - burst time.

| Process | AT | BT | CT | TAT = CT - AT | WT = TAT - BT |
|---|---|---|---|---|---|
| P1 | 0 | 4 | 4 | 4 | 0 |
| P2 | 0 | 10 | 29 | 29 | 19 |
| P3 | 0 | 5 | 12 | 12 | 7 |
| P4 | 5 | 3 | 8 | 3 | 0 |
| P5 | 7 | 7 | 19 | 12 | 5 |

AWT = (0 + 19 + 7 + 0 + 5) / 5 = 31/5 = 6.2 ms

ATAT = (4 + 29 + 12 + 3 + 12) / 5 = 60/5 = 12 ms

**Answer: AWT = 6.2 ms, ATAT = 12 ms.**

If non-preemptive SJF is meant instead, the order is P1, P3, P4, P5, P2 (finish 4, 9, 12, 19, 29), giving AWT = (0+4+4+5+19)/5 = 6.4 ms and ATAT = (4+9+7+12+29)/5 = 12.2 ms.

#### Comparison

SJF (AWT 6.2 ms) gives a lower average waiting time than priority scheduling (AWT 8 ms) here, because it always favours the shortest remaining work.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. When does P5 arrive?
   A. 2 ms
   B. 5 ms
   C. 7 ms (2 ms after P4 at 5)
   D. 9 ms
   Answer: C. P5 enters 2 ms after P4's arrival at 5. (Key point: P5 arrival)

2. What is the AWT in preemptive priority scheduling?
   A. 6.2 ms
   B. 8 ms
   C. 13.8 ms
   D. 7 ms
   Answer: B. WT = 0, 19, 14, 7, 0 totals 40; 40/5 = 8. (Key point: Priority AWT)

3. Why does preemptive SJF/SRT give lower AWT here?
   A. It always runs the process with the smallest remaining time, ignoring priority
   B. It uses time slices
   C. It runs P2 first
   D. It has no context switches
   Answer: A. AWT 6.2 ms versus 8 ms. (Key point: SJF vs priority)

4. At t = 7 under SRT, why is P4 not pre-empted by P5?
   A. P5 has lower priority number
   B. P5 has not arrived
   C. SRT never pre-empts
   D. P4 has only 1 ms left, less than P5's 7 ms
   Answer: D. SRT compares the new burst with remaining time. (Key point: No pre-emption of P4)

## Close variants set in other exams

- [Preemptive priority scheduling (4, 10, 5 ms)](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Asched-priority-4-10-5-p4) — Embedded System, chapter 6, asked 2×

## Related in Embedded System

- [Preemptive priority scheduling with P4 (6 ms)](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Asched-priority-10-5-7-p4) — Embedded System, chapter 6, asked 2×
- [Priority scheduling (7, 8, 5 ms) with P4](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Asched-priority-7-8-5-p4) — Embedded System, chapter 6, asked 2×
- [Preemptive SJF/SRT scheduling (10, 5, 7 ms)](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Asched-srt-10-5-7) — Embedded System, chapter 6
- [Non-preemptive priority scheduling (10, 5, 7 ms)](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Asched-priority-nonpreemptive-10-5-7) — Embedded System, chapter 6, asked 2×
- [Preemptive priority scheduling (4, 6, 5 ms)](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Asched-priority-4-6-5) — Embedded System, chapter 6
- [Non-preemptive SJF with P4 arriving](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Asched-sjf-nonpreemptive-6-8-2) — Embedded System, chapter 6, asked 2×

## Related in other subjects

- [FCFS, SJF, Priority, RR Gantt; max AWT](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Ascheduling-gantt-fcfs-sjf-priority-rr-q2) — Operating System, chapter 2
- [RR, preemptive priority, preemptive SJF](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Aprocess-rr-priority-preemptive-sjf) — Operating System, chapter 2
- [Real-time scheduling; two-processor numerical](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Arealtime-two-processor-fcfs-sjf-priority-rr) — Operating System, chapter 2
