Title: Course Overview Principles of Operating Systems
1Course OverviewPrinciples of Operating Systems
- Introduction
- Computer System Structures
- Operating System Structures
- Processes
- Process Synchronization
- Deadlocks
- CPU Scheduling
- Memory Management
- Virtual Memory
- File Management
- Security
- Networking
- Distributed Systems
- Case Studies
- Conclusions
2Chapter Overview
- objectives
- prerequisites
- process execution phases
- context switch
- evaluation criteria
- CPU scheduler
- types of scheduling algorithms
- non-preemptive
- preemptive
- scheduling algorithms
- First-Come, First-Served (FCFS)
- Shortest Job First (SJF)
- Shortest Remaining Time First (SRTF)
- priority-based
- round robin
- multilevel queue
- multilevel feedback queue
- evaluation of scheduling algorithms
- important concepts and terms
- summary
3Objectives
- appreciate the relevance of CPU scheduling for
the operation of computer systems - understand the workings of different scheduling
algorithms - know about their strengths and weaknesses
- obtain the knowledge to evaluate and select the
appropriate scheduling algorithm for different
situations and environments
4Prerequisites
- process execution phases
- context switch
- performance evaluation criteria
5Process Execution Phases
move(A,B) add(R1,3) move(R1,A) load(File_1) cop
y(R1,R3) sub(R3,5) move(R3,B) store(File_1) div
(B,2) move(B,A)
CPU-intensive
I/O-intensive
CPU-intensive
I/O-intensive
CPU-intensive
6Context Switch
- removal of the old process from the CPU
- saves the process block in main memory
- selection of the next process
- see scheduling
- installation of the new process in the CPU
- read the process block from main memory
- set the program counter, registers, etc.
- (re-)start the process
7Dispatcher
- transfer of control to the newly selected process
- context switch
- switch to user mode
- continue execution of the process at the position
indicated by the program counter - dispatch latency
- the time it takes to stop one process and start
the execution of another one
8Performance Evaluation Criteria
- fairness
- often CPU time per process
- efficiency
- resource utilization often CPU
- response time (interactive users)
- start of the process/burst until the first
response to the user - turnaround time (batch users)
- start of the process/burst until the end of the
process/burst
9Evaluation Criteria (cont.)
- throughput
- number of finished processes per time unit
- waiting time
- time spent in the ready queue
- context switches
- indication for the amount of overhead
- complexity of the scheduling algorithm
- indication of the time needed to select the next
process
10Scheduling
- deals with the allocation of resources to
processes - CPU scheduling
- disk scheduling
- important aspects
- when does a process get a resource
- how long may the process keep the resource
- how are conflicting requests resolved
11Scheduling of Processes
- long-term scheduling (job scheduling)
- a process is added to the pool of processes
- medium-term scheduling
- a process is swapped in or out of main memory
- short-term scheduling
- which process will get the CPU
- invoked in the following situations
- a process is done with its task or CPU burst
- a process has an I/O request
- the time slice of a process is over
- a new process with a higher priority arrives
- possibly after an interrupt
12Factors in Scheduling
- is the process CPU- or I/O-bound
- interactive or batch
- process priority
- execution time used so far
- execution time required to complete
- preemption frequency
- page fault frequency
13CPU Scheduler
- component of the operating system
- is itself a process
- uses resources of the computer system
- in particular CPU time, memory
- should not consume too much CPU time
- otherwise the overhead is too high
14CPU Scheduler (cont.)
- responsible for the selection of the next process
to be executed - the selection is done according to a scheduling
algorithm - short-term scheduling of processes
- job scheduler and mid-term scheduler also
schedule processes, but with a longer-term
perspective - basic principles are similar for all three
process schedulers
15Scheduling Algorithm
- prescribes the way the next process is selected
- determines the order in which processes are
executed - defines the actions for the scheduler
16Scheduling Algorithms
- types of scheduling algorithms
- preemptive, non-preemptive
- instances of scheduling algorithms
- FCFS, SJF, SRTF, priority-based, round robin
- multilevel, multilevel feedback
17Non-Preemptive Scheduling
- a process stays on the CPU until it voluntarily
releases the CPU - long waiting and response times
- may lead to starvation
- simple, easy to implement
- not suited for multi-user systems
- euphemism cooperative multitasking
18Preemptive Scheduling
- the execution of a process may be interrupted by
the operating system at any time - interrupt
- higher priority process
- arrival of a new process, change of status
- time limit
- prevents a process from using the CPU for too
long - may lead to race conditions
- can be solved by using process synchronization
19First-Come, First-Served (FCFS)
- principle
- processes are served in the order in which they
arrive - even if they have the same arrival time, an order
of arrival is distinguishable, or a process is
chosen randomly
20FCFS Example
- five processes arrive at time 0 in the order P1,
P2, P3, P4, P5 - processes have different (expected) burst lengths
- all processes have the same priority
- Gantt chart illustrates the execution of
processes on the CPU - comparison criteria with other algorithms
- waiting time
- response time
- turnaround time
- number of context switches
21Terminology
- waiting time
- time a process spends in the ready queue
- not in the waiting queue
- may consist of several separate periods
- response time
- time from the arrival of the process until its
first activity - turnaround time
- time from the arrival until the termination of
the process - usually only one single CPU burst is considered
- number of context switches
- simplified assumption for each period of CPU
activity, a process requires one context switch - roughly one half context switch to be loaded into
the CPU, and another half to be removed
22FCFS Example
23FCFS Example
P1
24FCFS Example
P2
P1
25FCFS Example
P2
P3
P1
26FCFS Example
P2
P3
P4
P1
27FCFS Example
P2
P3
P4
P5
P1
28FCFS Example
P2
P3
P4
P5
P1
29FCFS Properties
- very simple, easy to implement
- uses a FIFO queue
- very quick selection of the next process
- constant time, independent of the number of
processes in the ready queue - non-preemptive
- often long average waiting and response times
- not suited for multi-user systems
30Shortest Job First (SJF)
- principle the process with the shortest CPU
burst length is selected - obvious improvement from FCFS
31SJF Example
- same conditions as in the FCFS example
- arrival time 0 for all processes
- same priorities
- different (expected) burst lengths
32SJF Example
P5
33SJF Example
P3
P5
34SJF Example
P3
P4
P5
35SJF Example
P3
P4
P5
P1
36SJF Example
P2
P3
P4
P5
P1
37SJF Example
P2
P3
P4
P5
P1
38SJF Example 2
- slight modification
- different arrival times for the processes
- same priorities
- different (expected) burst lengths
- the processes waiting in the ready queue are
addedto the diagram
39SJF Example 2
ready queue at time 0
P1 6
40SJF Example 2
ready queue at time 3
P1 6
P2 15
P1
41SJF Example 2
P2 15
ready queue at time 5
P1 6
P2 15
P3 3
P3
P1
42SJF Example 2
P2 15
P2 15
P1 6
P2 15
P4 4
P3 3
P3
P4
P1
43SJF Example 2
P2 15
P2 15
P1 6
P2 15
P4 4
P2 15
P3 3
P2
P3
P4
P1
44SJF Example 2
P2 15
P2 15
P1 6
P2 15
P4 4
P2 15
P5 2
P3 3
P2
P3
P4
P5
P1
45SJF Example 2
P2 15
P2 15
P1 6
P2 15
P4 4
P2 15
P5 2
P3 3
P2
P3
P4
P5
P1
46SJF properties
- much better average waiting time than FCFS
- provably optimal with respect to the average
waiting time - non-preemptive
- relies on knowing the length of the CPU bursts
- in general difficult to impossible
- selection is more complex than FCFS
- linear w.r.t. number of processes in the ready
queue - starvation is possible
- if new, short processes keep on arriving, old,
long processes may never be served
47Intermezzo Burst Length
- burst length prediction
- burst length estimation
- burst length calculation
- example
48Burst Length Prediction
- in practice, the length of the next CPU burst of
a process is not known - as a consequence, algorithms such as SJF, SRTF in
their pure form cant be used in practical
systems - the CPU burst length can be estimated based on
previous CPU bursts of the process - estimation by analysis
- requires analysis of the code to be executed
while the scheduling decision is made - in general intractable
49Burst Length Estimation
- the length of the next CPU burst is estimated
from the lengths of previous bursts - frequently, recent bursts are given more
importance than older bursts - additional overhead during the scheduling
decision - time to calculate the estimates
- memory space to keep values of recent CPU burst
lengths
50Burst Length Calculation
- generic formula to estimate the length of the
next CPU burst from the lengths of previous
onesE i a B i-1 (1-a) E i-1 - E i estimate at time i
- B i-1 actual burst length at time i-1
- a factor to balance the importance of recent
and not so recent bursts
51Burst Length Example
- at time Ti we estimate the length of the next
CPU burst based on information we have about
previous bursts according to E i a B i-1
(1-a) E i-1 - we have to select a value for a (here 0.75)
- for the very first burst B0, we have to guess,
after that we use the measured time for the
previous burst - the measured burst time may be significantly
different from the estimate
52Burst Length Example (cont.)
- Time Estimate Previous
- Burst
- T0 0.75 10 0.25 0 7.5 ---
53Burst Length Example (cont.)
- Time Estimate Previous
- Burst
- T0 0.75 10 0.25 0 7.5 6
- T1 0.75 6 0.25 7.5 6.375
54Burst Length Example (cont.)
- Time Estimate Previous
- Burst
- T0 0.75 10 0.25 0 7.5 6
- T1 0.75 6 0.25 7.5 6.375 3
- T2 0.75 3 0.25 6.375 3.85
-
55Burst Length Example (cont.)
- Time Estimate Previous
- Burst
- T0 0.75 10 0.25 0 7.5 6
- T1 0.75 6 0.25 7.5 6.375 3
- T2 0.75 3 0.25 6.375 3.85 7
- T3 0.75 7 0.25 3.85 6.2
-
56Burst Length Example (cont.)
- Time Estimate Previous
- Burst
- T0 0.75 10 0.25 0 7.5 6
- T1 0.75 6 0.25 7.5 6.375 3
- T2 0.75 3 0.25 6.375 3.85 7
- T3 0.75 7 0.25 3.85 6.2 12
- T4 0.75 12 0.25 6.2 9.55
-
57Shortest Remaining Time First (SRTF)
- principle the process with the shortest
remaining time is selected - remaining time is the CPU burst length minus the
time the CPU has already spent serving the
process - if a process with a shorter CPU burst length than
the remaining time of the current process
arrives, the current process is preempted
58SRTF Example
- arrival time of new processes is important
- it is useful to keep track of the processes
currently in the ready queue - policy used here preempted processes go to the
end of the ready queue - may depend on the actual implementation
- a scheduling decision must be made when
- a process is done with its CPU burst
- a new process arrives in the ready queue
59SRTF Example
P1 6
P1
60SRTF Example
P1 3
P1 6
P2 15
P1
P1
61SRTF Example
P1 3
P1 6
P2 15
P2 15
P2
P1
P1
62SRTF Example
P1 3
P2 12
P1 6
P2 15
P2 15
P3 3
P2
P3
P1
P1
63SRTF Example
P1 3
P2 12
P1 6
P2 15
P2 12
P2 15
P3 3
P2
P3
P1
P1
P2
64SRTF Example
P1 3
P2 12
P2 10
P1 6
P2 15
P4 4
P2 12
P2 15
P3 3
P2
P3
P1
P1
P2
P4
65SRTF Example
P4 1
P1 3
P2 12
P2 10
P5 2
P1 6
P2 15
P4 4
P2 12
P2 10
P2 15
P3 3
P2
P3
P1
P1
P2
P4
66SRTF Example
P4 1
P1 3
P2 12
P2 10
P5 2
P5 2
P1 6
P2 15
P4 4
P2 12
P2 10
P2 10
P2 15
P3 3
P2
P3
P1
P1
P2
P4
P5
67SRTF Example
P4 1
P1 3
P2 12
P2 10
P5 2
P5 2
P1 6
P2 15
P4 4
P2 12
P2 10
P2 10
P2 10
P2 15
P3 3
P2
P3
P1
P1
P2
P2
P4
P5
68SRTF Example
P4 1
P1 3
P2 12
P2 10
P5 2
P5 2
P1 6
P2 15
P4 4
P2 12
P2 10
P2 10
P2 10
P2 15
P3 3
P2
P3
P1
P1
P2
P2
P4
P5
69SRTF Properties
- good response time for short processes
- attractive for multi-user systems
- preemptive version of the SJF algorithm
- higher overhead (context switches, elapsed time
table) - starvation possible
- impractical due to burst length prediction problem
70Highest Response Ratio Next
- principle
- priority of a process is a function of its
execution time and the time it has been waiting
for service - priority (time waiting execution time) /
execution time
71HRNN Properties
- non-preemptive in its basic form
- preemptive variations exist
- preference for shorter processes over longer ones
- aging ensures that long processes will
eventually get the CPU - still requires estimation of the execution time
(burst length) - minimum priority 1.0
72Round Robin Scheduling
- principle
- processes are given a fixed time on the CPU(time
quantum, time slice, slot) - the order of the processes is usually FIFO
73Round Robin Example
- same numbers as for the SRTF example
- within the ready queue, a first-come,
first-served order is applied - if a process arrives at the same time as another
one is finished with its time quantum, the new
process goes first - both processes go to the end of the ready queue
- processes are not preempted during their time
quantum in this example - may be different for specific implementations,
e.g. by using priority
74RR Example
time quantum 3 units
P1 6
P1
75RR Example
time quantum 3 units
P1 3
P1 6
P2 15
P2
P1
76RR Example
time quantum 3 units
P2 12
P1 3
P1 6
P1 3
P2 15
P2
P1
P1
77RR Example
time quantum 3 units
P2 12
P3 3
P1 3
P1 6
P1 3
P2 15
P2 12
P2
P1
P1
P2
78RR Example
time quantum 3 units
P2 12
P2 9
P3 3
P1 3
P1 6
P1 3
P2 15
P2 12
P3 3
P2
P3
P1
P1
P2
79RR Example
time quantum 3 units
P2 12
P2 9
P3 3
P4 4
P1 3
P1 6
P1 3
P2 15
P2 12
P3 3
P2 9
P2
P3
P1
P1
P2
P2
80RR Example
time quantum 3 units
P2 6
P2 12
P2 9
P5 2
P3 3
P4 4
P1 3
P1 6
P1 3
P2 15
P2 12
P3 3
P2 9
P4 4
P2
P3
P1
P1
P2
P4
P2
81RR Example
time quantum 3 units
P2 6
P4 1
P2 12
P2 9
P5 2
P2 6
P3 3
P4 4
P1 3
P1 6
P1 3
P2 15
P2 12
P3 3
P2 9
P4 4
P5 2
P2
P3
P1
P1
P2
P4
P5
P2
82RR Example
time quantum 3 units
P2 6
P4 1
P2 12
P2 9
P5 2
P2 6
P3 3
P4 4
P4 1
P1 3
P1 6
P1 3
P2 15
P2 12
P3 3
P2 9
P4 4
P5 2
P2 6
P2
P3
P1
P1
P2
P4
P5
P2
P2
83RR Example
time quantum 3 units
P2 6
P4 1
P2 12
P2 9
P5 2
P2 6
P3 3
P4 4
P4 1
P2 3
P1 3
P1 6
P1 3
P2 15
P2 12
P3 3
P2 9
P4 4
P5 2
P2 6
P4 1
P4
P2
P3
P1
P1
P2
P4
P5
P2
P2
84RR Example
time quantum 3 units
P2 6
P4 1
P2 12
P2 9
P5 2
P2 6
P3 3
P4 4
P4 1
P2 3
P1 3
P2 3
P1 6
P1 3
P2 15
P2 12
P3 3
P2 9
P4 4
P5 2
P2 6
P4 1
P4
P2
P3
P1
P1
P2
P4
P5
P2
P2
P2
85RR Example
time quantum 3 units
P2 6
P4 1
P2 12
P2 9
P5 2
P2 6
P3 3
P4 4
P4 1
P2 3
P1 3
P2 3
P1 6
P1 3
P2 15
P2 12
P3 3
P2 9
P4 4
P5 2
P2 6
P4 1
P4
P2
P3
P1
P1
P2
P4
P5
P2
P2
P2
86Round Robin Characteristics
- preemptive
- often used in time sharing environments
- sensitive to the length of the time quantum
- I/O-bound processes are at a disadvantage
- every time an I/O request occurs the process has
to go back to the end of the ready queue - variation virtual RR
- uses a separate queue with higher priorities for
processes returning from I/O activities
87Time Quantum Size
- options
- large or small quantum
- fixed or variable quantum
- same for everyone or different
- if the quantum is too large RR degenerates into
FCFS - if the quantum is too small there will be a lot
of context switching - guideline quantum should be slightly larger than
the time required for a typical interaction
88Guaranteed Scheduling
- gives users a predictable level of performance
- for N users, each gets 1/N of the CPU time
- scheduling is based on achieving this objective
- variation of round robin
89Multilevel Queue Scheduling
- the ready queue is partitioned into several
separate queues - queues have different priorities
- queues use different scheduling algorithms
- processes are permanently assigned to a queue
- typically interactive processes are in
higher-priority queues, batch processes in
lower-priority ones
90Multilevel Feedback Queue Scheduling
- the ready queue is partitioned into several
separate queues - queues have different priorities
- queues have different scheduling algorithms
- there is an algorithm for scheduling the queues
- frequently time-slicing by allocating a certain
percentage of CPU time to each queue - processes can move between queues
- offers flexibility for processes whose
characteristics change during their life time - can be used as a form of aging to prevent
starvation
91Example Multilevel Feedback Queue
- three queues
- high-priority queue 1 uses a time quantum of 8
units - medium-priority queue 2 uses a time quantum of 16
units - low-priority queue 3 uses FCFS scheduling
- characteristics
- new processes go into queue 1
- processes with short CPU bursts have high
priority - processes with long CPU bursts end up in the
low-priority queue
92Multilevel Queues Example
Queue 1
P1
P2
P3
P4
P5
0
12
28
36
46
P4
P1
P2
P3
P5
17-8 9
25-8 17
8-8 0
18-8 10
32-8 24
0
10
20
30
40
50
60
70
80
90
100
P5
P2
P5
P4
P4
Queue 2
P4
P5
P2
P2
P1
P1
P2
P2
P1
P4
P1
P2
P2
P5
17-3 14
9-4 5
24-16 8
5-5 0
14-2 12
12-11 1
10-10 0
P4
P4
Queue 3
P4
P2
P2
P2
P2
P4
1-1 0
8-8 0
Gantt
Chart
8
12
20
46
44
25
28
65
36
54
81
91
92
P3
P2
P1
P2
P1
P1
P4
P5
P2
P4
P5
P2
P4
P2
Q
1
Q
1
Q
1
Q
1
Q
1
Q2
Q2
Q2
Q2
Q2
Q2
Q2
Q3
Q3
93Multi-Processor Scheduling
- more complex
- symmetrical multiprocessor system
- functionally identical processors
- distributed system
- load sharing and balancing
- common or separate queue
94Hard Real-Time Computing
- hard real-time scheduling
- must complete a critical task within a guaranteed
amount of time. - scheduler must know how long each type of OS
function takes - impossible in a system with virtual memory
- special-purpose software running on dedicated
hardware
95Soft Real-Time Computing
- soft real-time scheduling
- requires the use of a priority scheme
- examples multimedia, highly interactive graphics
- priority must not degrade over time
- small dispatch latency
- insert preemption points in long duration system
calls - make the entire kernel preemptable
96Evaluation of Scheduling Algorithms
- deterministic modeling
- performance is evaluated for a predetermined work
load - simple, fast, but not very realistic
- queuing models
- based on queuing-network analysis
- can handle some dynamic aspects of the system
- simulations
- a model of the computer system is generated, and
the algorithms are run on that models - implementation
- most realistic and accurate, but very expensive
97Important Concepts and Terms
- algorithm evaluation
- arrival time
- burst length
- context switch
- CPU scheduler
- deterministic modeling
- dispatcher
- dispatch latency
- first-come, first-served (FCFS)
- guaranteed scheduling
- multilevel (feedback) queue
- non-preemptive scheduling
- preemptive scheduling
- priority scheduling
- process
- queuing model
- ready queue
- real-time scheduling
- resources management
- response time
- round robin algorithm
- scheduling algorithm
- shortest job first (SJF)
- shortest remaining time first
- simulation
- time quantum
- turnaround time
- throughput
98Chapter Summary
- selection of a process from the ready queue
- dispatcher allocates the CPU to the chosen
process - several scheduling algorithms exist for various
situations - first-come, first-served, shortest job first,
shortest remaining time first, round robin - multilevel (feedback) queues
- scheduling can be non-preemptive or preemptive
- evaluation of scheduling algorithms can be
complex - deterministic, queuing networks, simulation,
implementation