Download as pdf or txt
Download as pdf or txt
You are on page 1of 50

Module II

• Process Scheduling: FCFS, SJF, Priority, Round-Robin


Multilevel Queue and Multilevel Feedback Queue Scheduling.
Thread: Structure. User and kernel level threads, multi-threading models,
multiprocessor scheduling.
Deadlines in a real time system represents the time at which specific
tasks has to be completed.
Scheduling algorithms
In a multitasking system, a mechanism within an OS, called a
scheduler is responsible for determining the order and the
duration of tasks to run on the CPU.
✓The scheduler selects which tasks will be in what states
(Ready,Runing,Delayed etc ).
✓Loading and saving the TCB information for each task.
✓In some OSes the same scheduler allocates the CPU to a process that
is loaded into memory and ready to run.
✓In some other OSes a dispatcher (a separate scheduler) is
responsible for the actual allocation of the CPU to the process.
Two Approaches in Scheduling Algorithms
1. Non-preemptive scheduling: When a process enters the state of running, the
state of that process is not deleted from the scheduler until it finishes its
service time.
a. First-Come-First-Serve (FCFS)/ Run-To-Completion
b. Shortest Job First (SJN) /Shortest Process Next (SPN) /Run-To-
Completion
2. Preemptive scheduling:The pre-emptive scheduling is prioritized. The
highest priority process should always be the process that is currently
utilized.
a. Round Robin Scheduling
b. Priority (Preemptive) Scheduling
Terms related to Scheduling algorithms
Arrival Time (AT) - The time at which the process or task entered at the
queue.
Burst Time(BT) – The time required by a particular process or task to
complete itself.
Complete Time (CT) – It is the time, when the process or task is finished by
the CPU.
Turn Around time (TAT) – The time for which the process was inside the
queue. The total time spent by a process or task inside the queue.
TAT= CT-AT
Waiting Time (WT) – Time when the process or task was inside the queue
but did not get the CPU time.
WT= TAT-BT
a. First-Come-First-Serve (FCFS)/ Run-To-Completion
• “Run until Done:” FIFO algorithm
• one program runs non-preemtively until it is finished
• Tasks in the READY queue are executed in the order they entered the queue.
• Tasks are run until completion when they are READY to be run.
• Simple, fair, but poor performance. Average queueing time may be long.

Current
Task
MASTER
CPU

No context switching
after this point
1. Problem of FCFS
Draw the Gantt chart of the FCFS scheduling system and also calculate the
following parameters.
a.) the total and average turn around time
b.) The total and average waiting time
Arrival Time Burst Time
Process
(AT) (msec) (BT) (msec)
P1 0 5
P2 1 3
P3 2 8
P4 3 6
Process Arrival Burst Complete Turn Around Waiting
Solution Time (AT) Time(BT) Time (CT) time (TAT) Time (WT)
CT-AT TAT-BT
P1 0 5 5 5 0
P2 1 3 8 7 4
P3 2 8 16 14 6
P4 3 6 22 19 13
At t=0 only the process P1 has started and executing
Total TAT = 5+7+14+19= 45msec
At t=5 P1 completed the execution, Avg. TAT = 45/4= 11.25 msec
P2 starts at 1st sec, P3 at 2nd sec and P4 at 3rd sec.
Total WT= 0+4+6+13= 23msec
Avg. WT = 23/4= 5.75 msec
Gantt Chart
0 5 8 16 22
P1 P2 P3 P4
Advantages & Disadvantages of FCFS
Advantage of FCFS:
✓Easy to implement.
✓ It is very simple.
✓ First come, first served
✓ Easy to understand

Drawback of FCFS:
❖Trouble some for time sharing systems.
❖The average waiting time is very high, this may impact on the performance of the CPU.
❖The process will run until it finishes.
❖Short processes which are at the back of the queue have to wait for the long process at the
front to finish
b. Shortest Job First (SJF)/ Shortest Process First(SPF)/ Run-To-Completion

• In which, the tasks with the shortest execution time are executed first.
• It requires the knowledge of the service time for each process.
• Optimal for minimizing queueing time, but impossible to implement.
Tries to predict the process to schedule based on previous history.
1. Draw the Gantt chart of the SJF (Non-Preemptive)scheduling system and also
calculate the following parameters.
a.) the total and average turn around time
b.) The total and average waiting time

Arrival Time Burst Time


Process
(AT) (msec) (BT) (msec)
P1 0 7
P2 2 4
P3 4 1
P4 5 4
Process Arrival Burst Complete Turn Around Waiting
Solution Time (AT) Time(BT) Time (CT) time (TAT) Time (WT)
CT-AT TAT-BT
P1 0 7 7 7 0
P2 2 4 12 10 6
P3 4 1 8 4 3
P4 5 4 16 11 7
At t=0 only the process P1 has started and executing
At t=7 P1 completed the execution, Total TAT = 7+10+4+11= 32msec
Avg. TAT = 32/4= 8 msec
P2 burst time 4 sec, P3 1 sec and P4 4 sec.
P2 P3 P4
Total WT= 0+6+3+7= 16msec
4 1 4 Avg. WT = 16/4= 4 msec

0 7 8 12 16
P1 P3 P2 P4
Advantages & Disadvantages of SJF
Advantages
• It can give minimum average waiting time.
• Its Simple.
• It is suitable for very little time processes.
Drawbacks
✓If short processes are continually added, the processes which
require a long time to run will starve.
✓The total execution time of a job must be known before execution.
a. Round Robin/FIFO (First In, First Out) Scheduling
No Priority for the task.
key
High q Common time slices for
=PREEMPTION
each task. If the time slice
over, preempts the task
Q = Quantum

✓ It implements a FIFO queue that stores ready processes (processes ready to be executed).
✓ Processes are added to the queue at the end of the queue.
✓ In the FIFO system, all processes are treated equally regardless of their workload or interactivity.
✓ Each process in the FIFO queue is allocated an equal time slice.
✓ An interrupt is generated at the end of each of these intervals to start the pre-emption process.
✓ If a process that is currently running isn’t finished executing by the end of its allocated time slice, it is
preempted and returned to the back of the queue to complete executing the next time its turn comes around.
✓ If a process finishes running before the end of its allocated time slice, the process voluntarily releases the
processor, and the scheduler then assigns the next process of the FIFO queue to the processor.
1. Draw the Gantt chart of the Round Robin scheduling system and also calculate
the following parameters.
a.) the total and average turn around time
b.) The total and average waiting time

Arrival Time Burst Time


Process
(AT) (msec) (BT) (msec)
P0 0 7
P1 2 4
P2 4 1

Solution
Process Arrival Burst Complete Turn Around Waiting Time
Solution Time (AT) Time(BT) Time (CT) time (TAT) (WT)
CT-AT TAT-BT
P0 0 7 12 12 5
P1 2 4 11 9 5
P2 4 1 10 6 5

Total TAT = 27msec


P0 P1 P0 P2 P1 P0 Avg. TAT = 27/3=9msec

Total WT= 15msec


Avg. TAT = 15/3= 5 msec

0 3 6 9 10 11 12
P0 P1 P0 P2 P1 P0
Merits & Demerits of Round Robin Scheduling

Merits
✓ Very simple to implement
✓ Strict "first come, first served" nature
✓ It's fair; each job gets an equal shot at the CPU.

Demerits
✓ No Priority for tasks
✓ Unequal Task Lengths
✓ More context switches/Task Switches
✓ More waiting time for not completed tasks
b. Priority (Preemptive) Scheduling
• Every process is assigned a priority.
• The processes with the highest priority always preempt lower priority
processes.
• A running task can be forced to block by the scheduler if a higher
priority task becomes ready to run.

High key
1. Draw the Gantt chart of the priority based (non Preemptive) scheduling
system and also calculate the following parameters.
a.) the total and average turn around time
b.) The total and average waiting time

Arrival Time Burst Time


Priority Process
(AT) (msec) (BT) (msec)
3 P1 0 7
2 P2 2 4
4 P3 4 1
1 P4 5 4
Priority Process Arrival
Time (AT)
Burst
Time(BT)
Complete
Time (CT)
Turn Around Waiting
time (TAT) Time (WT)
Solution
CT-AT TAT-BT
3 P1 0 7 7 7 0
2 P2 2 4 15 13 9
4 P3 4 1 16 12 11
1 P4 5 4 11 6 2
At t=0 only the process P1 has started and executing
At t=7 P1 completed the execution, Total TAT = 7+13+12+6= 38msec
Avg. TAT = 38/4= 9.5msec
P2 arrived at 2 sec, P3 at 4 sec and P4 at 5 sec.
Total WT= 0+9+11+2= 22msec
Lower value(1) has highest priority and higher value
Avg. WT = 22/4= 5.5msec
(4) has lowest priority
0 7 11 15 16
P1 P4 P2 P3
Advantages and disadvantages of priority scheduling.
Advantages of Priority Scheduling-
✓ Simplicity.
✓Reasonable support for priority.
✓Suitable for applications with varying time and resource requirements.
Disadvantages of Priority Scheduling-
• Indefinite blocking or Process starvation.
• Priority Inversion problem (Solution to priority inversion is priority inheritance,
semaphores, mutex)
• A priority scheduling can leave some low priority waiting processes indefinitely
for CPU.
• If the system eventually crashes then all unfinished low priority processes gets
lost.
Multilevel Queue Scheduling
• In a multilevel queue-scheduling algorithm, processes are permanently assigned to a
queue on entry to the system.
• Processes do not move between queues.
• This setup has the advantage of low scheduling overhead, but the disadvantage of being
inflexible.
• Ready Queue is divided into separate queues for each class of processes, All three
different type of processes have their own queue. Each queue has its own Scheduling
algorithm
• Fixed priority preemptive scheduling method – Each queue has absolute priority
over lower priority queue. Let us consider following priority order queue 1 > queue 2
> queue 3.
• Time slicing – In this method each queue gets certain portion of CPU time and can
use it to schedule its own processes.
Multilevel Feedback Queue Scheduling
• Multilevel feedback queue scheduling, allows a process to move between queues.
• The idea is to separate processes with different CPU-burst characteristics.
• If a process uses too much CPU time, it will be moved to a lower-priority queue.
• Similarly, a process that waits too long in a lower-priority queue may be moved to
a higher-priority queue.
• This form of aging prevents starvation.
A multilevel feedback queue scheduler is defined by the following parameters:

• The number of queues.


• The scheduling algorithm for each queue.
• The method used to determine when to upgrade a process to a higher-priority
queue.
• The method used to determine when to demote a process to a lower-priority
queue.
• The method used to determine which queue a process will enter when that
process needs service.
Threads
Difference between process and thread
• Process: Processes are basically the programs that are dispatched from the
ready state and are scheduled in the CPU for execution. PCB(Process
Control Block) holds the concept of process. The process takes more time
to terminate and it is isolated means it does not share the memory with
any other process. The process can have the following states new, ready,
running, waiting, terminated, and suspended.

• Thread: Thread is the segment of a process which means a process can


have multiple threads and these multiple threads are contained within a
process. A thread has three states: Running, Ready, and Blocked.
• Example: Opening a new browser (say Chrome, etc) is an
example of creating a process. At this point, a new process will
start to execute. On the contrary, opening multiple tabs in the
browser is an example of creating the thread.
Threads
• The general structure of a process assumes that the process has
an address space with a single sequential execution sequence.
Which leads to low resource utilization
• Thread is a mechanism to provide multiple execution control to
the processes
• Thread has the following benefits:
• Responsiveness
• Resource Sharing
• Economy
• Utilization of MP Architectures
• Threads are classified as:
• user threads and
• kernel threads
Single and Multithreaded Processes
User Level Threads
• Thread management done by user-level threads library rather than via
systems calls.

• Therefore, thread switching does not need to call operating system and
to cause interrupt to the kernel.

• In fact, the kernel knows nothing about user-level threads and


manages them as if they were single-threaded processes.
Advantages :
• The most obvious advantage of this technique is that a user-level threads package can
be implemented on an Operating System that does not support threads.

• User-level threads do not require any modification to operating systems.


• Simple Representation : Each thread is represented simply by a PC, registers, stack
and a small control block, all stored in the user process address space.

• Simple Management : This simply means that creating a thread, switching between
threads and synchronization between threads can all be done without intervention of
the kernel.
• Fast and Efficient : Thread switching is not more expensive than a procedure call.
Disadvantages :
• There is a lack of coordination between threads and
operating system kernel. Therefore, process as whole gets
one time slice irrespective of whether process has one
thread or even 1000 threads within it. It is up to each thread
to relinquish control to other threads.
• User-level threads require non-blocking systems call i.e., a
multithreaded kernel. Otherwise, entire process will be
blocked in the kernel, even if there are runable threads left
in the processes. For example, if one thread causes a page
fault, the process gets blocked.
Three primary thread libraries:
• POSIX Pthreads
• Win32 threads
• Java threads
Kernel Level Threads

• The kernel knows about the threads and manages them.


• The kernel has a thread table that keeps track of all the threads in the system.
• In addition, the kernel also maintains the traditional process table to keep track of
the processes.
Advantages:
• Because kernel has full knowledge of all threads, Scheduler may decide to
give more time to a process having large number of threads than process
having small number of threads.
• Kernel-level threads are especially good for applications that frequently block.
Disadvantages:

• The kernel-level threads are slow and inefficient. For instance, threads
operations are hundred of times slower than that of user-level threads.
• Since kernel must manage and schedule threads as well as processes. It
requires a full thread control block (TCB) for each thread to maintain
information about threads.
Multithreading Models
• Many-to-One

• One-to-One

• Many-to-Many
Many-to-One

• Many user-level threads mapped to


single kernel thread
• Examples:
• Solaris Green Threads
• GNU Portable Threads
One-to-One

• Each user-level thread maps to kernel


thread
• Examples
• Windows NT/XP/2000
• Linux
• Solaris 9 and later
Many-to-Many Model

• Allows many user level threads to be


mapped to many kernel threads
• Allows the operating system to create
a sufficient number of kernel threads
• Solaris prior to version 9
• Windows NT/2000 with the Thread
Fiber package
Summary
• Types of processor
• Scheduling algorithms
scheduling • Short-term scheduling
• Long-term criteria
scheduling • The use of priorities
• Medium-term • Alternative scheduling
scheduling policies
• Short-term • Performance
scheduling comparison
• Traditional UNIX • Fair-share scheduling
scheduling

You might also like