This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
04/24/23 Operating Systems 2013 - Michael Elhadad, Meni Adler& Amnon Meisels 1
Course Syllabus1. Introduction - History; Views; Concepts; Structure2. Process Management - Processes; State + Resources; Threads;
Unix implementation of Processes3. Scheduling – Paradigms; Unix; Modeling4. Synchronization - Synchronization primitives and their
Fairness: Comparable processes should get comparable service
Efficiency: keep CPU and I/O devices busy Response time: minimize, for interactive users Turnaround time: average time for a job to complete Waiting time: minimize the average over processes Throughput: number of completed jobs per time unit
04/24/23 Operating Systems 2013 - Michael Elhadad, Meni Adler& Amnon Meisels 4
More on quality criteriaMore on quality criteria
Throughput – number of processes completed per unit time Turnaround time – interval of time from process submission
to completion Waiting time – sum of time intervals the process spends in
the ready queue Response time – the time between submitting a command
and the generation of the first output CPU utilization – the percentage of time in which the CPU is
not idle
04/24/23 Operating Systems 2013 - Michael Elhadad, Meni Adler& Amnon Meisels 5
Criteria by system typeCriteria by system type
04/24/23 Operating Systems 2013 - Michael Elhadad, Meni Adler& Amnon Meisels 6
Conflicting GoalsConflicting Goals
Response Time vs. Turnaround time:A conflict between interactive and batch users
Fairness vs. Throughput:How should we schedule very long jobs?
04/24/23 Operating Systems 2013 - Michael Elhadad, Meni Adler& Amnon Meisels 7
Scheduling – Types of behaviorScheduling – Types of behavior
Bursts of CPU usage alternate with periods of I/O waito CPU-bound processeso I/O bound processes
04/24/23 Operating Systems 2013 - Michael Elhadad, Meni Adler& Amnon Meisels 8
CPU Utilization vs. Turnaround timeCPU Utilization vs. Turnaround time
We have 5 interactive jobs i1…i5 and one batch job b
Interactive jobs: 10% CPU; 20% disk I/O; 70% terminal I/O; total time for each job 10 sec.
Batch job: 90% CPU; 10% disk I/O; total time 50 sec.
Cannot run all in parallel !!Cannot run all in parallel !!
i1..i5i1..i5 in parallel - disk I/O is 100% utilized in parallel - disk I/O is 100% utilized bb and one and one ii in parallel - CPU is 100% utilized in parallel - CPU is 100% utilized
04/24/23 Operating Systems 2013 - Michael Elhadad, Meni Adler& Amnon Meisels 9
CPU utilization vs. Turnaround time CPU utilization vs. Turnaround time (II)(II)
2. b and each of the i’s (in turn) in parallel:UTUT = (50x(0.9+0.1))/50 = 100%TATA = (10+20+30+40+50+50)/6 = 33sec.
04/24/23 Operating Systems 2013 - Michael Elhadad, Meni Adler& Amnon Meisels 10
When are Scheduling Decisions made ?When are Scheduling Decisions made ?1. Process switches from RunningRunning to WaitingWaiting (e.g. I/O)2. New process created (e.g. fork)3. Process switches from RunningRunning to Ready Ready (clock.. )4. Process switches from WaitingWaiting to Ready Ready (e.g. IO
completion)5. Process terminatesterminates
Types of Scheduling:Types of Scheduling: Preemptive scheduling: process preepmtion may be
initiated by scheduler When for non-preemptive scheduling? only 1 And 5 .
04/24/23 Operating Systems 2013 - Michael Elhadad, Meni Adler& Amnon Meisels 11
Scheduling: outlineScheduling: outline
Scheduling criteriaScheduling algorithmsUnix schedulingLinux schedulingWin NT scheduling
04/24/23 Operating Systems 2013 - Michael Elhadad, Meni Adler& Amnon Meisels 12
Processes get the CPU in the order they request it and run until they release it
Ready processes form a FIFO queue
04/24/23 Operating Systems 2013 - Michael Elhadad, Meni Adler& Amnon Meisels 13
FCFS may cause long waiting timesFCFS may cause long waiting times
Process Burst time (milli)
P1
P2
P3
24
3
3
If they arrive in the order P1, P2, P3, we get:
0 24 27 30
P1 P2 P3
Average waiting time: (0+24+27) / 3 = 17
04/24/23 Operating Systems 2013 - Michael Elhadad, Meni Adler& Amnon Meisels 14
FCFS may cause long waiting times FCFS may cause long waiting times (cont’d)(cont’d)
Process Burst time (milli)P1P2P3
2433
If they arrive in the order P1, P2, P3, average waiting time 17
What if they arrive in the order P2, P3, P1?
0 3 30
P1P2 P3
Average waiting time:6
(0+3+6) / 3 = 3
04/24/23 Operating Systems 2013 - Michael Elhadad, Meni Adler& Amnon Meisels 15
(Non preemptive) Shortest Job First (SJF) (Non preemptive) Shortest Job First (SJF) schedulingscheduling The CPU is assigned to the process that has the smallest
next CPU burst In some cases, this quantity is known or can be
approximatedProcess Burst time (milli)
ABC
524
D 1
CB0 5
A Average waiting time: 8.75
7D
11 12
D AB0 1
Average waiting time: 5.75
7C
12
SJF schedule
3
FCFS schedule
04/24/23 Operating Systems 2013 - Michael Elhadad, Meni Adler& Amnon Meisels 16
Approximating next CPU-burst durationApproximating next CPU-burst duration
Can be done by using the length of previous CPU burstso tn = actual length of nth CPU burst
o Tn+1 = predicted value for the next CPU burst
o for 0 1 define the exponential average:
o Tn+1 = tn+ (1 - ) Tn
o determines the relative weight of recent bursts
04/24/23 Operating Systems 2013 - Michael Elhadad, Meni Adler& Amnon Meisels 17
Move processes that used their quanta to a lower class Net result - longer runs for CPU-bound processes; higher priority
for I/O-bound processes for a CPU burst of 100 time quanta - 7 switches instead of 100
(in RR)
04/24/23 Operating Systems 2013 - Michael Elhadad, Meni Adler& Amnon Meisels 43
Highest Response Ratio Next (HRRN)Highest Response Ratio Next (HRRN)
Choose next process with the highest ratio of:
Similar to non-preemptive SJF but avoids starvation
time spent waiting + expected service timeexpected service time
04/24/23 Operating Systems 2013 - Michael Elhadad, Meni Adler& Amnon Meisels 44
Feedback Scheduling: exampleFeedback Scheduling: example
Demote jobs that have been running longer Need not know remaining process execution time Always perform a higher-priority job (preemptive) Aging may be used to avoid starvation
quantum=8
quantum=16
FCFS
Higher priority
Lower priority
04/24/23 Operating Systems 2013 - Michael Elhadad, Meni Adler& Amnon Meisels 45
Two-Level SchedulingTwo-Level Scheduling Recall that processes can be either in memory or
swapped out (to disk) Schedule memory-resident processes by a CPU (low-
level) scheduler as before Swap in/out by a Memory (high-level) scheduler using:
o Elapsed time since process swapped in/outo CPU time allocated to processo Process memory size o Process priority
04/24/23 Operating Systems 2013 - Michael Elhadad, Meni Adler& Amnon Meisels 46
Two level schedulingTwo level scheduling
CPU
Memorydisk
Memory scheduler
CPU scheduler
04/24/23 Operating Systems 2013 - Michael Elhadad, Meni Adler& Amnon Meisels 47
Scheduling in Batch Systems (2)Scheduling in Batch Systems (2)
Three level scheduling
04/24/23 Operating Systems 2013 - Michael Elhadad, Meni Adler& Amnon Meisels 48
Scheduling in Real-Time SystemsScheduling in Real-Time Systems
Must respond to external events within fixed time (CD player, autopilot, robot control,…)
Schedulable real-time system Given
o m periodic eventso event i occurs with period Pi and requires Ci seconds
Then the load can only be handled if
1
1m
i
i i
CP
04/24/23 Operating Systems 2013 - Michael Elhadad, Meni Adler& Amnon Meisels 49
Different scheduling algorithms for threads/processes possible No way to preempt user threads Thread context switch much faster Application-specific scheduling possible
Scheduling within a quantum
04/24/23 Operating Systems 2013 - Michael Elhadad, Meni Adler& Amnon Meisels 50
Scheduler may prefer switches within same process Context switch more expensive A blocking thread does not block all process threads
Scheduling within a quantum
04/24/23 Operating Systems 2013 - Michael Elhadad, Meni Adler& Amnon Meisels 51
Scheduling: outlineScheduling: outline
Scheduling criteriaScheduling algorithmsUnix schedulingLinux SchedulingWin NT scheduling
04/24/23 Operating Systems 2013 - Michael Elhadad, Meni Adler& Amnon Meisels 52
Scheduling in UnixScheduling in Unix Two-level scheduling Low level (CPU) scheduler uses multiple queues to select
the next process, out of the processes in memory, to get a time quantum.
High level (memory) scheduler moves processes from memory to disk and back, to enable all processes their share of CPU time
Low-level scheduler keeps queues for each priority Processes in user mode have positive priorities Processes in kernel mode have negative priorities (lower is
higher)
04/24/23 Operating Systems 2013 - Michael Elhadad, Meni Adler& Amnon Meisels 53
Unix priority queuesUnix priority queues
04/24/23 Operating Systems 2013 - Michael Elhadad, Meni Adler& Amnon Meisels 54
Unix low-level Scheduling AlgorithmUnix low-level Scheduling AlgorithmPick process from highest (non-empty) priority queueRun for 1 quantum (usually 100 ms.), or until it blocksIncrement CPU usage count every clock tickEvery second, recalculate priorities:
o Divide cpu usage by 2o New priority = base + cpu_usage + niceo Base is negative if the process is released from waiting in
kernel modeUse round robin for each queue (separately)
04/24/23 Operating Systems 2013 - Michael Elhadad, Meni Adler& Amnon Meisels 55
Unix low-level scheduling Algorithm - I/OUnix low-level scheduling Algorithm - I/O Blocked processes are removed from queue, but when the
blocking event occurs, are placed in a high priority queue The negative priorities are meant to release processes
quickly from the kernel Negative priorities are hardwired in the system, for example,
-5 for Disk I/O is meant to give high priority to a process released from disk I/O
Interactive processes get good service, CPU bound processes get whatever service is left...
04/24/23 Operating Systems 2013 - Michael Elhadad, Meni Adler& Amnon Meisels 56
Priority Calculation in UnixPriority Calculation in Unix
Pj(i)= Priority of process j at beginning of interval iBasej = Base priority of process jUj(i) = Processor utilization of process j in interval iGuk(i) = Total processor utilization of all processes in group k during
interval iCPUj(i) = Exponentially weighted average processor utilization by
process j through interval iGCPUk(I)= Exponentially weighted average total processor utilization of group k
through interval iWk = Weighting assigned to group k, with the constraint that 0 Wk 1
and
2)1(
2)1()1(
2)1(
2)1(
)(
4)1(
2)1(
)(
iGCPUiGUiGCPU
iCPUiUiCPU
WiGCPUiCPU
BaseiP
kkk
jjj
k
kjjj
k
kw 1
04/24/23 Operating Systems 2013 - Michael Elhadad, Meni Adler& Amnon Meisels 57
Scheduling: outlineScheduling: outline
Scheduling criteriaScheduling algorithmsUnix scheduling Linux SchedulingWin NT scheduling
04/24/23 Operating Systems 2013 - Michael Elhadad, Meni Adler& Amnon Meisels 58
Three classes of threadsThree classes of threads Real-time FIFO
o Have highest priority, can only be preempted by a higher-priority real-time FIFO thread
Real-time round robino Assigned time quantum
Timesharingo Priorities 100-139
Priority determines the number of clock ticks (jiffys) assigned to a round-robin or timesharing thread
04/24/23 Operating Systems 2013 - Michael Elhadad, Meni Adler& Amnon Meisels 59
Linux runqueuesLinux runqueuesPer-CPU runqueue
<…>
Active
Expired
<…>
Array[0]
Array[1]
Priority 0
Priority 139
T
Priority 0
Priority 139
T T T
T T
T T
T
T
04/24/23 Operating Systems 2013 - Michael Elhadad, Meni Adler& Amnon Meisels 60
Linux runqueues Linux runqueues (cont'd)(cont'd)Per-CPU runqueue
<…>
ActiveExpired
<…>
Array[0]
Array[1]
Priority 0
Priority 139
T
Priority 0
Priority 139
T T T
T T
T T
T
T
Scheduler selects tasks from highest-priority active queueo If time-slice expires – moved to an expired listo If blocked then, when event arrives, returned to active queue (with smaller
time-slice)
When no more active tasks – swap active and expired pointers
04/24/23 Operating Systems 2013 - Michael Elhadad, Meni Adler& Amnon Meisels 61
Multiprocessor supportMultiprocessor support Runqueue per processor
Affinity scheduling
Perform periodic load-balancing between runqueueso But in most cases, a process will run on the same process it ran
before.
04/24/23 Operating Systems 2013 - Michael Elhadad, Meni Adler& Amnon Meisels 62
Scheduling: outlineScheduling: outline
Scheduling criteriaScheduling algorithmsUnix scheduling Linux SchedulingWin NT scheduling
04/24/23 Operating Systems 2013 - Michael Elhadad, Meni Adler& Amnon Meisels 63
WINDOWS NT SchedulingWINDOWS NT Scheduling
Mapping of Win32 priorities to Windows 2000 priorities
04/24/23 Operating Systems 2013 - Michael Elhadad, Meni Adler& Amnon Meisels 64
WINDOWS NT Scheduling WINDOWS NT Scheduling (II)(II)
Windows 2000 supports 32 priorities for threads
04/24/23 Operating Systems 2013 - Michael Elhadad, Meni Adler& Amnon Meisels 65
Windows NT scheduling: dynamic changesWindows NT scheduling: dynamic changes
Priority is raised:o Upon I/O completion (disk: 1, serial line: 2, keyboard: 6, sound card:
8)o Upon event reception (semaphore, mutex…): by 2 if in the
foreground process, by 1 otherwiseo If a thread waits `too much’ – it is moved to priority 15 for two
quanta (for solving priority inversion – see next slide)
Priority is decreased:o By 1 if quantum is fully usedo Never bellow base value (normal thread priority)
04/24/23 Operating Systems 2013 - Michael Elhadad, Meni Adler& Amnon Meisels 66
An example of priority inversionAn example of priority inversion