0% found this document useful (0 votes)
2 views

5_1__Process_Scheduling

The document discusses various aspects of process scheduling in operating systems, including different types of scheduling (long-term, mid-term, short-term, and I/O scheduling) and their impact on system performance. It outlines key concepts such as CPU utilization, turnaround time, and various scheduling algorithms like First-Come, First-Served, Shortest Job First, and Round Robin. Additionally, it addresses challenges like starvation and fairness in scheduling, along with advanced techniques like multilevel queues and fair-share scheduling.

Uploaded by

veryimpulsive04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

5_1__Process_Scheduling

The document discusses various aspects of process scheduling in operating systems, including different types of scheduling (long-term, mid-term, short-term, and I/O scheduling) and their impact on system performance. It outlines key concepts such as CPU utilization, turnaround time, and various scheduling algorithms like First-Come, First-Served, Shortest Job First, and Round Robin. Additionally, it addresses challenges like starvation and fairness in scheduling, along with advanced techniques like multilevel queues and fair-share scheduling.

Uploaded by

veryimpulsive04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

i n g

u l
e d
c h
S
Scheduling
• Processes in different state maintain Queue.
• The different queues are maintained for different
purpose eg.
– Ready Queue : Processes waiting for CPU
– Blocked : processes waiting for I/O to complete
• Transition form a state where queue is
maintained to next state involves decision
making such as
– When to move process from one state to another
– Which process to move
• When transitions occur, OS may be required to
carry out some house keeping activity such as
context switch, Mode switch etc. These activities
are considered as overhead and must be carried
out in efficient manner
What is scheduling ?
• Scheduling is matter of managing queues
to minimize queuing delay and to optimize
performance in queuing environment

• Scheduling affects the performance of the


system because it determines which
process will wait and which will progress
Types of Scheduling( Based on frequency of
invocation of scheduler)
• Long term scheduling: Decision to add to the pool of
processes to be executed

• Mid term: The decision to add to the number of


processes that are partially or fully in main memory

• Short Term : Which process will execute on


processor

• I/O scheduling : Which process’s pending I/O


request is handled by an available I/O device.
Long-Term Scheduling

• Determines which programs are admitted to the system


for processing

• Controls the degree of multiprogramming

• More processes, smaller percentage of time each


process is executed

• 2 decisions involved in Long term Scheduling


– OS can take one or more additional processes
– Which job or jobs to accept and turn into processes.
Medium-Term Scheduling

• Part of the swapping function

• Based on the need to manage the degree of


multiprogramming
Short-Term Scheduling

• Known as the dispatcher


• Executes most frequently
• Invoked when an event occurs
– Clock interrupts
– I/O interrupts
– Operating system calls
– Signals
CPU Scheduling
• Maximize CPU utilization with multi programming.
• CPU–I/O Burst Cycle – Process execution consists of a
cycle of CPU execution and I/O wait.
• Processes Can be

– CPU Bound : Processes spend bulk of its time


executing on processor and do very little I/O
– I/O Bound : Process spend very little time executing
on processor and do lot of I/O operation

• Mix of processes in the system can not be predicted


Assumption: CPU Bursts
Weighted toward small bursts
CPU Scheduler
• Selects
– the processes in memory that are ready to execute
– allocates the CPU to one of them.
• CPU scheduling decisions may take place when
a process:
1. Switches from running to waiting state.
2. Switches from running to ready state.
3. Switches from waiting to ready.
4. Terminates.
• Scheduling under 1 and 4 is non preemptive.
• All other scheduling is preemptive.
Dispatcher
• Dispatcher module gives control of the
CPU to the process selected by the
short-term scheduler; this involves:
– switching context
– switching to user mode
– jumping to the proper location in the user
program to restart that program
• Dispatch latency – time it takes for the
dispatcher to stop one process and start
another running
Performance Measures
• CPU utilization – keep the CPU as busy as possible. CPU
utilization vary from 0 to 100. In real systems
– it varies from 40 (lightly loaded) to 90 (heavily loaded)
• Throughput – Number of processes that complete their
execution per time unit.
• Turnaround time – amount of time to execute a particular
process (interval from time of submission to time of
completion of process).
• Waiting time – amount of time a process has been waiting in
the ready queue (sum of the periods spend waiting in the
ready queue).
Performance Measures

• Response time – amount of time it takes from


when a request was submitted until the first
response is produced (not output ).
• Optimization criteria
– Max CPU utilization, Max throughput, Min
turnaround time, Min waiting time, Min
response time
• Fairness
• Balancing resources
• Predictability
Scheduling Assumptions
• Many implicit assumptions for CPU scheduling:
– One program per user
– One thread per program
– Programs are independent
• Clearly, these are unrealistic but they simplify the problem so it can be
solved
– For instance: is “fair” about fairness among users or programs?
• If I run one compilation job and you run five, you get five times as
much CPU on many operating systems
Scheduling Algorithms
• First – Come, First – Served Scheduling (Run until done )
– Example: Process Burst Time
P1 24
P2 3
P3 3
– Suppose that the processes arrive in the order: P1 ,
P2 , P3
The Gantt Chart for the schedule is:
P1 P2 P3
0 24 27 30

– Waiting time for P1 = 0; P2 = 24; P3 = 27


– Average waiting time: (0 + 24 + 27)/3 = 17
Turn around time:
P1=24 P2=27 P3=30

Normalized turn around time : is turn around


time divided by CPU burst.

P1=24/24=1 P2 = 27/3 =9 P3=30/3=10


Suppose that the processes arrive in the order
P2 , P3 , P1 .
• The Gantt chart for the schedule is:

P2 P3 P1
0 3 6 30
Waiting time for P1 = 6; P2 = 0; P3 = 3
Average waiting time: (6 + 0 + 3)/3 = 3
Much better than previous case.
Convoy effect short process behind long
process
Favors CPU bound processes
FCFS scheduling
• In early systems, FCFS meant one
program
scheduled until done (including I/O)
• Now, means keep CPU until thread blocks
FCFS scheduling
• FCFS is non preemptive : once the CPU
has been allocated to a process, the
process keeps the CPU till it finishes or it
requests the I/O
• It is not suited for time sharing system
• Average wait time is not minimal as it
depends on arrival and CPU burst of
arriving processes
Shortest-Job-First (SJF) Scheduling
• Associate with each process the length of its
next CPU burst. Use these lengths to schedule
the process with the shortest time.
• Two schemes:
– Non preemptive (SPN) – once CPU given to the
process it cannot be preempted until completes its
CPU burst.
– Preemptive – if a new process arrives with CPU burst
length, less than remaining time of current executing
process, preempt. This scheme is know as the
Shortest-Remaining-Time-First (SRTF).
• SJF is optimal – gives minimum average waiting
time for a given set of processes.
• Optimal average response time
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
• SJF (non-preemptive)

P1 P3 P2 P4
0 7 8 12 16

• Average waiting time = (0 + 6 + 3 + 7)/4 = 4


Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
• SJF (preemptive)

P1 P2 P3 P2 P4 P1
0 2 4 5 7 11 16

• Average waiting time = (9 + 1 + 0 +2)/4 = 3


Predicting the Length of the Next CPU Burst
• Adaptive: Changing policy based on past behavior
– Works because programs have predictable
behavior
– If program was I/O bound in past, likely in future
– If computer behavior were random, it will not help
• Example: SJF with estimated burst length
– Use an estimator function on previous bursts:
Let tn-1, tn-2, tn-3, etc. be previous CPU burst
lengths.
– Estimate next burst τn = f(tn-1, tn-2, tn-3, …)
– Function f could be one of many different time
series estimation schemes (Kalman filters,
exponential average etc.)
Determining Length of Next CPU Burst
• Can be done by using the length of previous CPU bursts, using
exponential averaging.

• α =0
– τn+1 = τn, Recent history does not count.
• α =1
– τn+1 = tn Only the actual last CPU burst counts.
• If we expand the formula, we get:
τn+1 = α tn+(1 - α) α tn -1 + …
+(1 - α )j α tn -j + …
+(1 - α )n+1 τ0
• Since both α and (1 - α) are less than or equal to 1, each successive
term has less weight than its predecessor.
Priority Scheduling
• A priority number (integer) is associated with
each process
• The CPU is allocated to the process with the
highest priority (smallest integer ≡ highest
priority).
– Preemptive
– Non preemptive
• SJF is a priority scheduling where priority is
the predicted next CPU burst time.
• Problem ≡ Starvation – low priority processes
may never execute.
• Solution ≡ Aging – as time progresses
increase the priority of the process.
Round Robin (RR)
• Each process gets a small unit of CPU time (time
quantum), usually 10-100 milliseconds. After this
time has elapsed, the process is preempted and
added to the end of the ready queue.
• If there are n processes in the ready queue
– Time quantum is q
– each process gets 1/n of the CPU time in chunks of at
most q time units at once.
– No process waits more than (n-1)q time units.
• Performance
– q large ⇒ FIFO
– q small ⇒ q must be large with respect to context switch,
otherwise overhead is too high.
Process Burst Time
P1 53
P2 8
P3 68
P4 24
• The Gantt chart is: Q=20

P1 P2 P3 P4 P1 P3 P4 P1 P3 P3
0 20 28 48 68 88 108 112 125 145 153

• Typically, higher average turnaround than SJF, but


better response.
Example of RR with Time Quantum = 20
• Example: Process Burst Time
P1 53
P2 8
P3 68
P4 24
– The Gantt chart is:

P1 P2 P3 P4 P1 P3 P4 P1 P3 P3

0 20 28 48 68 88 108 112 125 145 153


– Waiting time for P1=(68-20)+(112-88)=72
P2=(20-0)=20
P3=(28-0)+(88-48)+(125-108)=85
P4=(48-0)+(108-68)=88
– Average waiting time = (72+20+85+88)/4=66¼
– Average completion time = (125+28+153+112)/4 = 104½
• Thus, Round-Robin Pros and Cons:
– Better for short jobs, Fair (+)
– Context-switching time adds up for long jobs (-)
How a Smaller Time Quantum Increases Context Switches
Turnaround Time Varies With The Time Quantum
RR Contd….
• In practice, need to balance short-job
performance and long-job throughput:
– Typical context-switching overhead is 0.1ms –
1ms
– Roughly 1% overhead due to context-switching
Round Robin: Issues
• Favors CPU-bound processes
– An I/O bound process will use CPU for a time less
than the time quantum and gets blocked for I/O
– A CPU-bound process run for all its complete time
slice and is put back into the ready queue (thus getting
in front of blocked processes)
• A solution: virtual round robin
– When a I/O has completed, the blocked process is
moved to an auxiliary queue which gets preference over
the main ready queue
– A process dispatched from the auxiliary queue runs no
longer than the basic time quantum minus the time spent
running since it was selected from the ready queue

35
Highest Response Ratio Next

• Uses normalized turn around time which is the ratio of turn


around time to actual service time. For each individual
process we would like to minimize the ratio.
• Choose next process with the greatest value of Response
ratio.where Response ratio is defined as
• Response Ratio= (time spent waiting + expected
service time) / expected service time
• This method accounts for the age of the process and the
shorter jobs are favored.
Multilevel Queue
• It partitions ready queue into several separate queues
• Simple example: Ready queue is partitioned into separate
queues based on differing response time need of process
foreground (interactive)& background (batch)
• Each queue has its own scheduling algorithm,
foreground – RR
background – FCFS
• Scheduling must be done between the queues( alternatives)
– Each Queue has absolute priority (starvation)
– Time slice among the queues e.g.
80% to foreground in RR & 20% to background Job
Multilevel Queue

• The processes are permanently assigned


to one queue
• The processes from one queue donot
move to another queue
• This has advantage of low scheduling
overhead but is Inflexible
Multilevel Feedback Queue
– A process can move between the various queues; aging can be implemented
this way.
– Multilevel-feedback-queue scheduler defined by the following parameters:
• number of queues
• scheduling algorithms for each queue
• method used to determine when to upgrade a process
• method used to determine when to demote a process
• method used to determine which queue a process will enter when that
process needs service
– Three queues:
• Q0 – time quantum 8 milliseconds Q1 – time quantum 16
milliseconds Q2 – FCFS
– Scheduling
• A new job enters queue Q0 which is served FCFS. When it gains
CPU, job receives 8 milliseconds. If it does not finish in 8
milliseconds, job is moved to queue Q1.
• At Q1 job is again served and receives 16 additional milliseconds.
If it still does not complete, it is preempted and moved to queue
Q2.
Fair-share Scheduling
• Fairness ?
– User
– Process
• User’s application runs as a collection of processes (sets)
• User is concerned about the performance of the application made up of
a set of processes
• Need to make scheduling decisions based on process sets(groups)

•Think of processes as part of a group


•Each group has a specified share of the machine time it is allowed to use
•Priority is based on the time this processes is active, and the time the
other processes in the group have been active
Fair Share Scheduling
• Values defined
– Pj(i) =Priority of process j at beginning of ith interval
– Uj (i) =Processor use by process j during ith interval
– GUk(i) =Processor use by group k during ith interval
– CPUj(i) =Exponentially weighted average for process j from
beginning to the start of ith interval
– GCPUk(i) =Exponentially weighted average for group k from
beginning to the start of ith interval
– Wk =Weight assigned to group k, 0≤ Wk ≤1, ΣkWk=1
=> CPUj(1)=0, GCPUk(1)=0, i=1,2,3,….
Fair Share Scheduling
• Calculations (done each second):
– Pj(i) = Basej + CPUj(i)/2 + GCPUk(i)/(4*Wk)

– CPUj(i) = Uj(i-1)/2 +CPUj(i-1)/2

– GCPUk(i) = GUk(i-1)/2 + GCPUk(i-1)/2


Fair Share Example
• Three processes A, B, C; B,C are in one group; A is by itself
• Both groups get 50% weighting
Traditional UNIX Scheduling
• Multilevel queue using round robin within each of the
priority queues
• Priorities are recomputed once per second
• Base priority divides all processes into fixed bands of
priority levels
• Adjustment factor used to keep process in its assigned
band (called nice)
Bands
• Decreasing order of priority
– Swapper
– Block I/O device control
– File manipulation
– Character I/O device control
– User processes
• Values
• Pj(i) =Priority of process j at start of ith interval
• Uj(i) =Processor use by j during the ith interval
– Calculations (done each second):
• CPUj = Uj(i-1)/2 +CPUj(i-1)/2
• Pj = Basej + CPUj/2 + nicej
Multiprocessor Scheduling Issues
• Processors are functionally identical ?
– Homogeneous
– Heterogeneous
• System in which i/o is attached to private
bus of a processor.
• Keep all processor equally busy .
– Load balancing
Approach to MP scheduling
• Asymmetric multiprocessing
– All scheduling , I/O handling, System activity is handled by one
processor
– Other processor are used for executing user code
– Simple as only one processor accesses the system data structure (
reduced need for data sharing)
• Symmetric multiprocessing
– Each processor is self scheduling
• Can have single common queue for all processor
• alternatively individual queue for each processor
– Multiple scheduler, updating common data structure ( concurrency issues)
– Processor affinity
• Soft affinity
• Hard affinity
– NUMA & CPU scheduling
– Load balancing
Load balancing
• In SMP load balancing is necessary only
when each processor has independent
(private)ready queue
• Push/pull migration
– A specific task checks periodically the load of
each processor .
• In case of imbalance , moves process from overload
to idle processor.
• Pull occurs when Idle processor pulls a process from
a busy processor

You might also like