Lec2 +Operating+System+Part+1
Lec2 +Operating+System+Part+1
Operating Systems
Operating
system
OS Road Map
Concepts
+
Process
synchronization
+
Processes& +
Threads
+
CPU
Scheduling
+
Memory
©Ahmed Hagag Operating Systems Management
2
Reference
Computer Operating System Concepts
• Author: Silberschatz
• Publisher: Wiley
• Edition: Sixth
• ISBN: 0471250600
Operating System
What Operating Systems Do?
• OS is a resource allocator
➢ Manages all resources.
➢ Decides between conflicting requests for efficient and fair resource
use.
• OS is a control program
➢ Controls execution of programs to prevent errors and improper
use of the computer.
storage
standby
• It shares with other threads belonging to the same process its code section,
data section, and other operating-system resources, such as open files and
signals.
©Ahmed Hagag Operating Systems 34
Introduction CONT’S
• Let's say, for example, a program is not capable of drawing pictures while
reading keystrokes.
• The program must give its full attention to the keyboard input lacking the
• The ideal solution to this problem is the seamless execution of two or more
sections of a programOperating
©Ahmed Hagag
at theSystems
same time. Threads allows
35
us to do this.
Introduction
Schedulers
• Long-term scheduler chooses some of them to go to memory
(ready queue).
• Then, short-term scheduler (or CPU scheduler) chooses from
ready queue a job to run on CPU.
• Medium-term scheduler may move (swap) some partially-
executed jobs from memory to disk (to enhance performance).
CPU Scheduler
• Whenever the CPU becomes idle, the operating system must select
one of the processes in the ready queue to be executed. The selection
process is carried out by the short-term scheduler, or CPU
scheduler.
Note: A process may have many CPU bursts, but in the following
examples we show only one for simplicity.
P1
0
P1
0
P1
0
P1
0
Waiting Time
𝑷𝟏 𝑷𝟐 𝑷𝟑
P1
0
Waiting Time
𝑷𝟏 𝑷𝟐 𝑷𝟑
0 24 27
P1
0
Turnaround Time
𝑷𝟏 𝑷𝟐 𝑷𝟑
P1
0
Turnaround Time
𝑷𝟏 𝑷𝟐 𝑷𝟑
24 27 30
P1
0
Response Time
𝑷𝟏 𝑷𝟐 𝑷𝟑
P1
0
Response Time
𝑷𝟏 𝑷𝟐 𝑷𝟑
0 24 27
P2 P3 P
0
P2 P3 P
0
P2 P3 P
0
P2 P3 P
0
Waiting Time
𝑷𝟏 𝑷𝟐 𝑷𝟑
P2 P3 P
0
Waiting Time
𝑷𝟏 𝑷𝟐 𝑷𝟑
6 0 3
P2 P3 P
0
Turnaround Time
𝑷𝟏 𝑷𝟐 𝑷𝟑
P2 P3 P
0
Turnaround Time
𝑷𝟏 𝑷𝟐 𝑷𝟑
30 3 6
P2 P3 P
0
Response Time
𝑷𝟏 𝑷𝟐 𝑷𝟑
P2 P3 P
0
Response Time
𝑷𝟏 𝑷𝟐 𝑷𝟑
6 0 3
P4 P1 P3 P2
0 3 9 16 24
P4 P1 P3 P2
0 3 9 16 24
P4 P1 P3 P2
0 3 9 16 24
P4 P1 P3 P2
0 3 9 16 24
P4 P1 P3 P2
0 3 9 16 24
Waiting Time
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
P4 P1 P3 P2
0 3 9 16 24
Waiting Time
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
3 16 9 0
P4 P1 P3 P2
0 3 9 16 24
Turnaround Time
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
P4 P1 P3 P2
0 3 9 16 24
Turnaround Time
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
9 24 16 3
P4 P1 P3 P2
0 3 9 16 24
Response Time
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
P4 P1 P3 P2
0 3 9 16 24
Response Time
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
3 16 9 0
P1 P2 P4 P1 P3
0 1 5 10 17 26
P1 P2 P4 P1
P3
0 1 5 10 17 26
P1 P2 P4 P1 P3
0 1 5 10 17
2
6
Can be done by using the length of previous CPU bursts, using exponential
averaging
1. t n = actual length of n th CPU burst
P1 P2 P4 P1 P3
0 1 5 10 17 26
P1 P2 P4 P1 P3
0 1 5 10 17 26
P1 P2 P4 P1 P3
0 1 2 3 5 10 17
26
P1 P2 P4 P1
P3
0 1 2 3 5 10 17 26
P1 P2 P4 P1
P3
0 1 2 3 5 10 17 26
P1 P2 P4 P1
P3
0 1 5 10 17 26
P1 P2 P4 P1 P3
0 1 5 10 17
2
6
P1 P2 P4 P1 P
3
0 1 5 10 17 26
P1 P2 P4 P1 P3
0 1 5 10 17 26
P1 P2 P4 P1 P3
0 1 5 10 17 26
Waiting Time
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
P1 P2 P4 P1 P3
0 1 5 10 17 26
Waiting Time
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
10 − 1 1−1 17 − 2 5−3
= 𝟗 = 𝟎 = 𝟏𝟓 = 𝟐 Average waiting time = [9+0+15+2]/4 = 26/4 = 6.5 msec
©Ahmed Hagag Operating Systems 115
Scheduling Algorithms
2.2 Shortest-remaining-time-first (Preemptive SJF )
Process Arrival Time Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
Preemptive SJF Gantt Chart
P1 P2 P4 P1 P3
0 1 5 10 17 26
Turnaround Time
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
P1 P2 P4 P1 P3
0 1 5 10 17 26
Turnaround Time
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
17 − 0 5−1 26 − 2 10 − 3
= 𝟏𝟕 = 𝟒 = 𝟐𝟒 = 𝟕 Average turnaround time = [17+4+24+7]/4 = 52/4 = 13 msec
P1 P2 P4 P1 P3
0 1 5 10 17 26
Response Time
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
P1 P2 P4 P1 P3
0 1 5 10 17 26
Response Time
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
0−0 1−1 17 − 2 5−3
= 𝟎 = 𝟎 = 𝟏𝟓 = 𝟐 Average response time = [0+0+15+2]/4 = 17/4 = 4.25 msec
The CPU is allocated to the process with the highest priority (smallest integer
highest priority)
Preemptive
Nonpreemptive
SJF is priority scheduling where priority is the inverse of predicted next CPU
burst time
Waiting Time
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒 𝑷𝟓
Waiting Time
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒 𝑷𝟓
6 0 16 18 1 Average Waiting time = [6+0+16+18+1]/5 = 8.2 msec
Turnaround Time
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒 𝑷𝟓
Turnaround Time
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒 𝑷𝟓
16 1 18 19 6 Average Turnaround time = [16+1+18+19+6]/5 = 12 msec
Response Time
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒 𝑷𝟓
Response Time
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒 𝑷𝟓
6 0 16 18 1 Average Response time = [6+0+16+18+1]/5 = 8.2 msec
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
# of context switches = ??
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
# of context switches = 7
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
Waiting Time
𝑷𝟏 𝑷𝟐 𝑷𝟑
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
Waiting Time
𝑷𝟏 𝑷𝟐 𝑷𝟑
0 + (10 − 4) 4 7
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
Turnaround Time
𝑷𝟏 𝑷𝟐 𝑷𝟑
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
Turnaround Time
𝑷𝟏 𝑷𝟐 𝑷𝟑
30 7 10
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
Response Time
𝑷𝟏 𝑷𝟐 𝑷𝟑
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
Response Time
𝑷𝟏 𝑷𝟐 𝑷𝟑
0 4 7
𝑄0
𝑄1
𝑄2
7ms
7ms
𝑄0 𝑃1
𝑄1
𝑄2
7ms
7ms 8ms
𝑄0 𝑃1 𝑃2
𝑄1
𝑄2
15ms
7ms 8ms
𝑄0 𝑃1 𝑃2
𝑄1
𝑄2
15ms
7ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3
𝑄1 𝑃2
𝑄2
23ms
7ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3
𝑄1 𝑃2
𝑄2
23ms
7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
𝑄1 𝑃2
𝑄2
31ms
7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
16ms
𝑄1 𝑃2
𝑄2
31ms
7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
16ms 12ms
𝑄1 𝑃2 𝑃3
36ms
𝑄2 𝑃2
43ms
7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
16ms 12ms
𝑄1 𝑃2 𝑃3
36ms
𝑄2 𝑃2
43ms
7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
16ms 12ms 16ms
𝑄1 𝑃2 𝑃3 𝑃4
36ms
𝑄2 𝑃2
59ms
7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
16ms 12ms 16ms
𝑄1 𝑃2 𝑃3 𝑃4
36ms
𝑄2 𝑃2
67ms
7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
16ms 12ms 16ms
𝑄1 𝑃2 𝑃3 𝑃4
36ms
𝑄2 𝑃2
67ms
7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
16ms 12ms 16ms
𝑄1 𝑃2 𝑃3 𝑃4
36ms 16ms
𝑄2 𝑃2 𝑃4
83ms
7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
16ms 12ms 16ms
𝑄1 𝑃2 𝑃3 𝑃4
36ms 16ms
𝑄2 𝑃2 𝑃4
𝑄0
𝑄1
𝑄2
7ms
7ms
𝑄0 𝑃1
𝑄1
𝑄2
7ms
7ms
𝑄0 𝑃1
𝑄1
𝑄2
𝑄1
𝑄2
𝑄1
𝑄2
𝑄1
𝑄2
𝑄2
𝑄2
𝑄2
𝑄2
P1 0 7 = 𝟕 = 66 = 41 = 80
P2 1 60 52 36
P3 2 20 12
P4 3 40 32 16
15ms 31ms 59ms
7ms 23ms 47ms 75ms 111ms 127ms
7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
16ms 12ms 16ms
𝑄1 𝑃2 𝑃3 𝑃4
36ms 16ms
𝑄2 𝑃2 𝑃4
P3 2 20 12
P4 3 40 32 16
15ms 31ms 59ms
7ms 23ms 47ms 75ms 111ms 127ms
7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
16ms 12ms 16ms
𝑄1 𝑃2 𝑃3 𝑃4
36ms 16ms
𝑄2 𝑃2 𝑃4