3. Operating System Concepts
Basic Concepts
• Maximum CPU utilization obtained with multiprogramming
• I/O Burst phase: – when the process waits for some IO
operation
– Process execution consists of a cycle of CPU execution and I/O
wait.
• CPU burst phase :distribution or when the process is allocated
CPU and other resource is executing
6. CPU Scheduler
• Selects from among the processes in memory that are ready to
execute, and allocates the CPU to one of them.
• CPU scheduling decisions may take place when a process:
1. Switches from running to waiting state.( I/O wait)
2. Switches from running to ready state.( Interrupt)
3. Switches from waiting to ready.(IO complete)
4. Process terminates.
Scheduling under 1 and 4 is nonpreemptive.
• Nonpreemptive: there is no choice for CPU scheduling ( Stop)
– Terminate or waiting state
– new process present in queue.
• All other scheduling is preemptive(2 and 3):
– Process has choice CPU scheduling and CPU is execute
based on the priority.
7. Dispatcher
• Dispatcher module gives control of the CPU to the process
selected by the short-term scheduler; this functionality involves:
– switching context: switching from one process to another
process
– switching to user mode: switching to kernel mode to user
mode
– jumping to the proper location in the user program to
restart that program
• Dispatch latency – time it takes to stop one process and start
another running process.
8. Scheduling Criteria
• CPU utilization – keep the CPU as busy as possible
• Throughput – # of processes that complete their execution per
time unit
• Turnaround time – amount of time to execute a particular process
• Waiting time – amount of time a process has been waiting in the
ready queue
• Response time – amount of time it takes from when a request
was submitted until the first response is produced, not output
(for time-sharing environment)
9. Optimization Criteria
• Max CPU utilization
• Max throughput
• Min turnaround time
• Min waiting time
• Min response time
10. FCFS: First Come First Serve
• First Come First Serve, is just like FIFO(First in First out) Queue
data structure, where the data element which is added to the
queue first, is the one who leaves the queue first.
• This is used in Batch Systems.
• It's easy to understand and implement programmatically, using
a Queue data structure, where a new process enters through
the tail of the queue, and the scheduler selects process from
the head of the queue.
• A perfect real life example of FCFS scheduling is buying tickets
at ticket counter.
• Burst time: the time CPU taking control of process
11. First-Come, First-Served (FCFS) Scheduling
• 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:
• Waiting time for P1 = 0; P2 = 24; P3 = 27
• Average waiting time: (0 + 24 + 27)/3 = 17
P1 P2 P3
24 27 30
0
12. FCFS Scheduling (Cont.)
Suppose that the processes will arrive in the order
P2 , P3 , P1 .
• The Gantt chart for the schedule is:
• 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
P1
P3
P2
6
3 30
0
13. FCFS
• Problems:
1. It is non-preemptive
2. Improper process Scheduling
3. Resource unitization in parallel is not possible, which
leads to convey effect and hence poor resource
utilization
– convey effect: in which whole os slows down due to few slow
process.
14. Shortest-Job-First (SJF) Scheduling
• It is the best approach to minimize waiting time.
• It select the waiting process with the smallest
execution time to execute next.
15. 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:
– nonpreemptive – 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.
16.
17. Disadvantage:
The total execution time of job must be known before
execution while it is not possible to perfectly predict
execution
18.
19. Shortest-Remaining-Time-First (SRTF).
• As you can see in the GANTT chart above, as P1 arrives first,
hence it's execution starts immediately, but just after 1 ms,
process P2 arrives with a burst time of 3 ms which is less than
the burst time of P1, hence the process P1(1 ms done, 20 ms
left) is preemptied and process P2 is executed.
• As P2 is getting executed, after 1 ms, P3 arrives, but it has a
burst time greater than that of P2, hence execution of P2
continues. But after another millisecond, P4 arrives with a burst
time of 2 ms, as a result P2(2 ms done, 1 ms left) is preemptied
and P4 is executed.
• After the completion of P4, process P2 is picked up and finishes,
then P2 will get executed and at last P1.
• The Pre-emptive SJF is also known as Shortest Remaining
Time First, because at any given point of time, the job with the
shortest remaining time is executed first.
20. Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
• SJF (non-preemptive)
• Average waiting time = (0 + 6 + 3 + 7)/4 - 4
Example of Non-Preemptive SJF
P1 P3 P2
7
3 16
0
P4
8 12
21. Example of Preemptive SJF
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
• SJF (preemptive)
• Average waiting time = (9 + 1 + 0 +2)/4 - 3
P1 P3
P2
4
2 11
0
P4
5 7
P2 P1
16
22. 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
– nonpreemptive
• 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.
23. Priority Scheduling
• Priority scheduling can be of two types:
– Preemptive Priority Scheduling: If the new process
arrived at the ready queue has a higher priority than the
currently running process, the CPU is preempted, which
means the processing of the current process is stoped
and the incoming new process with higher priority gets
the CPU for its execution.
– Non-Preemptive Priority Scheduling: In case of non-
preemptive priority scheduling algorithm if a new
process arrives with a higher priority than the current
running process, the incoming process is put at the
head of the ready queue, which means after the
execution of the current process it will be processed.
24. Characteristics of Priority Scheduling
• A CPU algorithm that schedules processes based on priority.
• It used in Operating systems for performing batch processes.
• If two jobs having the same priority are READY, it works on a
FIRST COME, FIRST SERVED basis.
• In priority scheduling, a number is assigned to each process
that indicates its priority level.
• Lower the number, higher is the priority.
• In this type of scheduling algorithm, if a newer process arrives,
that is having a higher priority than the currently running
process, then the currently running process is preempted.
25. Example of Priority Scheduling Algorithm
• Consider the above table of processes with their
respective CPU burst times and the priorities.
26. Problem with Priority Scheduling
Algorithm
• In priority scheduling algorithm, the chances of indefinite
blocking or starvation.
• A process is considered blocked when it is ready to run but
has to wait for the CPU as some other process is running
currently.
• But in case of priority scheduling if new higher priority
processes keeps coming in the ready queue then the
processes waiting in the ready queue with lower priority
may have to wait for long durations before getting the CPU
for execution.
27. Using Aging Technique with Priority
Scheduling
• To prevent starvation of any process, we can use the
concept of aging where we keep on increasing the
priority of low-priority process based on the its waiting
time.
• For example, if we decide the aging factor to be 0.5 for
each day of waiting, then if a process with
priority 20(which is comparitively low priority) comes in
the ready queue. After one day of waiting, its priority is
increased to 19.5 and so on.
• Doing so, we can ensure that no process will have to
wait for indefinite time for getting CPU time for
processing.
28. Round Robin Scheduling
• CPU is assigned to the process on the basis of
FCFS for a fixed amount of time.
• This fixed amount of time is called as time
quantum or time slice.
• After the time quantum expires, the running
process is preempted and sent to the ready
queue.
• Then, the processor is assigned to the next
arrived process.
• It is always preemptive in nature.
• Round Robin Scheduling is FCFS Scheduling
with preemptive mode.
29.
30. Advantages-
• It gives the best performance in terms of average response time.
• It is best suited for time sharing system, client server architecture
and interactive system.
Disadvantages-
• It leads to starvation for processes with larger burst time as they
have to repeat the cycle many times.
• Its performance heavily depends on time quantum.
• Priorities can not be set for the processes.
31. Note(RRS)
With decreasing value
of time quantum,
•Number of context
switch increases
•Response time
decreases
•Chances of starvation
decreases
With increasing value
of time quantum,
•Number of context
switch decreases
•Response time
increases
•Chances of starvation
increases
•
32. Problem-01:
Process Id Arrival time Burst time
P1 0 5
P2 1 3
P3 2 1
P4 3 2
P5 4 3
Consider the set of 5 processes whose arrival time and burst time are given below-
If the CPU scheduling policy is Round Robin with time quantum = 2 unit, calculate the average waiting time and average turn around tim
33. Now, we know-
Turn Around time = Exit time – Arrival time
Waiting time = Turn Around time – Burst time
Process
Id
Exit time
Turn Around
time
Waiting time
P1 13
13 – 0 =
13
13 – 5 = 8
P2 12
12 – 1 =
11
11 – 3 = 8
P3 5 5 – 2 = 3 3 – 1 = 2
P4 9 9 – 3 = 6 6 – 2 = 4
P5 14
14 – 4 =
10
10 – 3 = 7
Average Turn Around time =
(13 + 11 + 3 + 6 + 10) / 5 = 43 / 5 = 8.6
unit
Average waiting time =
(8 + 8 + 2 + 4 + 7) / 5 = 29 / 5 = 5.8 unit
34. Process Id Arrival time Burst time
P1 0 4
P2 1 5
P3 2 2
P4 3 1
P5 4 6
P6 6 3
Consider the set of 6 processes whose arrival time and burst time are given below-
36. Process Id Arrival time Burst time
P1 0 4
P2 1 5
P3 2 2
P4 3 1
P5 4 6
P6 6 3
Consider the set of 5 proceses whose arrival time and burst time are given below-
If the CPU scheduling policy is Round Robin with time quantum = 2 unit, calculate the average waiting time and average turn around tim
37. Problem-01:
Process Id Arrival time Burst time
P1 0 5
P2 1 3
P3 2 1
P4 3 2
P5 4 3
Consider the set of 5 processes whose arrival time and burst time are given below-
If the CPU scheduling policy is Round Robin with time quantum = 2 unit, calculate the average waiting time and average turn around tim
38. Example: RR with Time Quantum = 20
Process Burst Time
P1 53
P2 17
P3 68
P4 24
• The Gantt chart is:
• Typically, higher average turnaround than SJF, but better
response.
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3
0 20 37 57 77 97 117 121 134 154 162
40. Multilevel Queue
• Ready queue is partitioned into separate queues:
foreground (interactive)
background (batch)
• Each queue has its own scheduling algorithm,
foreground – RR
background – FCFS
• Scheduling must be done between the queues.
– Fixed priority scheduling; i.e., serve all from foreground then
from background. Possibility of starvation.
– Time slice – each queue gets a certain amount of CPU time
which it can schedule amongst its processes; i.e.,
- 80% to foreground in RR
- 20% to background in FCFS
42. 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
44. Example of Multilevel Feedback Queue
• 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 FCFS and receives 16 additional
milliseconds. If it still does not complete, it is preempted
and moved to queue Q2.