scheduling kshama desai bijal shah kishore putta kashyap sheth

36
SCHEDULING SCHEDULING Kshama Desai Kshama Desai Bijal Shah Bijal Shah Kishore Putta Kishore Putta Kashyap Sheth Kashyap Sheth

Upload: ella-egleston

Post on 15-Dec-2015

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: SCHEDULING Kshama Desai Bijal Shah Kishore Putta Kashyap Sheth

SCHEDULINGSCHEDULING

Kshama DesaiKshama DesaiBijal ShahBijal Shah

Kishore PuttaKishore PuttaKashyap ShethKashyap Sheth

Page 2: SCHEDULING Kshama Desai Bijal Shah Kishore Putta Kashyap Sheth

SCHEDULINGSCHEDULING

Scheduling manages CPU allocation among a Scheduling manages CPU allocation among a group of ready processes and threads.group of ready processes and threads.

Concept Of Scheduling:Concept Of Scheduling:Based on mechanism of context switching.Based on mechanism of context switching.Requirement of shared resources by processesRequirement of shared resources by processes

and threads.and threads.

Page 3: SCHEDULING Kshama Desai Bijal Shah Kishore Putta Kashyap Sheth

MultiprogrammingMultiprogramming

Allows many processes to load and time-Allows many processes to load and time-multiplexing of the threads.multiplexing of the threads.

Per CPU, a single thread executes at any Per CPU, a single thread executes at any given time.given time.

Need for threads to perform concurrent I/O Need for threads to perform concurrent I/O operations.operations.

High CPU multiplexing rate gives the High CPU multiplexing rate gives the impression of concurrent execution of impression of concurrent execution of processes / threads.processes / threads.

Page 4: SCHEDULING Kshama Desai Bijal Shah Kishore Putta Kashyap Sheth

Scheduling Policies and MechanismScheduling Policies and MechanismScheduling Policy determines the thread to Scheduling Policy determines the thread to which the CPU should be allocated and its time which the CPU should be allocated and its time of execution.of execution.

Scheduling mechanisms determine the way Scheduling mechanisms determine the way process manager multiplexes the CPU and the process manager multiplexes the CPU and the state of the thread.state of the thread.

Thread SchedulingThread Scheduling

Scheduler determines the transition of thread Scheduler determines the transition of thread from the running state to the ready state.from the running state to the ready state.

Page 5: SCHEDULING Kshama Desai Bijal Shah Kishore Putta Kashyap Sheth

Steps in Thread SchedulingSteps in Thread Scheduling

Wait in Ready list for CPU allocation.Wait in Ready list for CPU allocation.

On CPU allocation, state of the thread On CPU allocation, state of the thread changes from ready to running state.changes from ready to running state.

During execution, thread waits in resource During execution, thread waits in resource manager’s pool on subsequent request for an manager’s pool on subsequent request for an unavailable resource (if needed).unavailable resource (if needed).

After complete execution, it leaves the CPU, After complete execution, it leaves the CPU, or returns to ready state.or returns to ready state.

Page 6: SCHEDULING Kshama Desai Bijal Shah Kishore Putta Kashyap Sheth

Job

Simpler Processor Scheduling ModelSimpler Processor Scheduling Model

Ready List Scheduler CPU

Resource Manager

Resources

JobJob

Job

Job

Done

Blocked

Running

RequestAllocate

NewThread

Preemption / Yield

Ready

Page 7: SCHEDULING Kshama Desai Bijal Shah Kishore Putta Kashyap Sheth

Running Thread Ceases CPU use for following Running Thread Ceases CPU use for following

reasons:reasons:

Thread completes execution and leaves the Thread completes execution and leaves the system.system.

Thread requests an unavailable resource. State Thread requests an unavailable resource. State is changed to Blocked. On availability of is changed to Blocked. On availability of resource, state is changed to Ready state.resource, state is changed to Ready state.

Thread voluntarily decides to release CPUThread voluntarily decides to release CPU

System Preempts the thread by changing its System Preempts the thread by changing its state from Running to Readystate from Running to Ready

Page 8: SCHEDULING Kshama Desai Bijal Shah Kishore Putta Kashyap Sheth

Scheduling MechanismsScheduling MechanismsScheduler is implemented in the hardware as well as inScheduler is implemented in the hardware as well as inthe OS software.the OS software.Scheduler implements 3 mechanisms:Scheduler implements 3 mechanisms:a) Enqueuer b) Context Switching c) Dispatch element .a) Enqueuer b) Context Switching c) Dispatch element .

Sequence of Schedule MechanismSequence of Schedule MechanismEnqueuer mechanism places the process into Ready Enqueuer mechanism places the process into Ready state and decides its priority.state and decides its priority.Context switch element helps to remove a process from Context switch element helps to remove a process from CPU and bring in a new process.CPU and bring in a new process.Dispatch element allocates CPU to the new incoming Dispatch element allocates CPU to the new incoming process.process.

Page 9: SCHEDULING Kshama Desai Bijal Shah Kishore Putta Kashyap Sheth

CPU SHARINGCPU SHARINGTwo types of Schedulers:Two types of Schedulers:

Voluntary CPU sharing (Non-preemptive Voluntary CPU sharing (Non-preemptive scheduler)scheduler)

Involuntary CPU sharing (Preemptive Involuntary CPU sharing (Preemptive scheduler)scheduler)

Voluntary CPU Sharing:Voluntary CPU Sharing:

Hardware includes special Hardware includes special yieldyield machine machine instruction.instruction.

Address of the next instruction is saved in Address of the next instruction is saved in a designated memory location.a designated memory location.

Page 10: SCHEDULING Kshama Desai Bijal Shah Kishore Putta Kashyap Sheth

Disadvantage of yield instruction:Disadvantage of yield instruction:

Failure of periodic access of a process to yieldFailure of periodic access of a process to yield

instruction blocks other processes frominstruction blocks other processes from

executing CPU, until the process exits orexecuting CPU, until the process exits or

requests a resource. requests a resource.

Solution of this problem is if the system gives Solution of this problem is if the system gives self-interrupt.self-interrupt.

Page 11: SCHEDULING Kshama Desai Bijal Shah Kishore Putta Kashyap Sheth

Involuntary CPU sharingInvoluntary CPU sharing

Incorporates Incorporates interval timerinterval timer device. device.

Interval of time is decided by the system Interval of time is decided by the system programmer.programmer.

Internal timer invokes the scheduler periodicallyInternal timer invokes the scheduler periodically

Advantage :Advantage :

Process executing in an infinite loop cannot blockProcess executing in an infinite loop cannot block

other processes from running (executing ) theother processes from running (executing ) the

CPU.CPU.

Page 12: SCHEDULING Kshama Desai Bijal Shah Kishore Putta Kashyap Sheth

PerformancePerformanceScheduler affects the performance of a Scheduler affects the performance of a

multiprogrammed CPU to a great extent.multiprogrammed CPU to a great extent.

Imbalanced process selection for CPU Imbalanced process selection for CPU execution by a scheduler will lead to execution by a scheduler will lead to Starvation. Starvation. This leads to the need for This leads to the need for strategy selection of schedulersstrategy selection of schedulers

Page 13: SCHEDULING Kshama Desai Bijal Shah Kishore Putta Kashyap Sheth

Strategy SelectionStrategy SelectionCriteria to select a scheduling strategy Criteria to select a scheduling strategy

depends on the goals of the OS.depends on the goals of the OS.

Scheduling algorithms for modern OS use Scheduling algorithms for modern OS use internal priorities.internal priorities.

Involuntary CPU sharing have time Involuntary CPU sharing have time quantum / timeslice length. Optimal quantum / timeslice length. Optimal schedule can be computed, provided there schedule can be computed, provided there is no new entry in the ready list while prior is no new entry in the ready list while prior present processes are being served.present processes are being served.

Page 14: SCHEDULING Kshama Desai Bijal Shah Kishore Putta Kashyap Sheth

Model For Scheduling Model For Scheduling Performance metrics to compare scheduling strategies:Performance metrics to compare scheduling strategies:

Service TimeService TimeWait timeWait timeTurnaround time.Turnaround time.

Process model and the metrics are used to compare the Process model and the metrics are used to compare the performance characteristics of each algorithm.performance characteristics of each algorithm.

The general model must fit each specific class of the OSThe general model must fit each specific class of the OS environment.environment.

Turnaround time is the most critical performance Turnaround time is the most critical performance metric in batch multiprogrammed system.metric in batch multiprogrammed system.Time sharing systems focus on single phase of thread Time sharing systems focus on single phase of thread executionexecution..

Page 15: SCHEDULING Kshama Desai Bijal Shah Kishore Putta Kashyap Sheth

Job

Simpler Processor Scheduling ModelSimpler Processor Scheduling Model

Ready List Scheduler CPU

Resource Manager

Resources

JobJob

Job

Job

Done

Blocked

Running

RequestAllocate

NewThread

Preemption / Yield

Ready

Page 16: SCHEDULING Kshama Desai Bijal Shah Kishore Putta Kashyap Sheth

Types of Scheduling MethodsTypes of Scheduling Methods

Non- Pre-emptive Non- Pre-emptive

Pre-emptivePre-emptive

Page 17: SCHEDULING Kshama Desai Bijal Shah Kishore Putta Kashyap Sheth

Non-PreemptiveNon-Preemptive

Non-Pre-emptive:Non-Pre-emptive: Once a process enters the running state, it is not Once a process enters the running state, it is not

removed from the processor until it has completed removed from the processor until it has completed its service time.its service time.

There are 4 Non-Pre-emptive strategies:There are 4 Non-Pre-emptive strategies:

1.1. First Come First Serve (FCFS)First Come First Serve (FCFS)

2.2. Shortest Job Next (SJN)Shortest Job Next (SJN)

3.3. Priority Scheduling Priority Scheduling

4.4. Deadline SchedulingDeadline Scheduling

Page 18: SCHEDULING Kshama Desai Bijal Shah Kishore Putta Kashyap Sheth

Pre-emptivePre-emptive

Based on prioritize computation.Based on prioritize computation.Process with highest priority should Process with highest priority should always be the one using the CPU.always be the one using the CPU.If a process is currently using the CPU and If a process is currently using the CPU and a new processor with higher priority enters a new processor with higher priority enters the ready list, the process on the the ready list, the process on the processor should be removed and processor should be removed and returned to the ready list until it is once returned to the ready list until it is once again the highest priority process in the again the highest priority process in the system.system.

Page 19: SCHEDULING Kshama Desai Bijal Shah Kishore Putta Kashyap Sheth

First Come First Serve (FCFS)First Come First Serve (FCFS)

Processes are assigned the CPU in the Processes are assigned the CPU in the order they request it.order they request it.

If a running process blocks, the first If a running process blocks, the first process on the queue is run the next. process on the queue is run the next. When this blocked process becomes When this blocked process becomes ready, like a newly arrived job, it is put on ready, like a newly arrived job, it is put on the end of the queue.the end of the queue.

Page 20: SCHEDULING Kshama Desai Bijal Shah Kishore Putta Kashyap Sheth

FCFS ExampleFCFS Example

Example loadExample load

i t(pi) i t(pi)

0 350

2 475

4 75

1 125

3 250

0 350 475 950 1200 1275

P0 P1 P2 P3 P4

Page 21: SCHEDULING Kshama Desai Bijal Shah Kishore Putta Kashyap Sheth

Turnaround TimeTurnaround TimeAverage of finishing times:Average of finishing times:

TTTRndTRnd (P0) = t(P0) = 350(P0) = t(P0) = 350

TTTRndTRnd (P1) = t(P1) + (P1) = t(P1) + TTTRndTRnd (P0) = 125 + 350 = 475(P0) = 125 + 350 = 475

TTTRndTRnd (P2) = t(P2) + (P2) = t(P2) + TTTRndTRnd (P1) = 475 + 475 = 950(P1) = 475 + 475 = 950

TTTRndTRnd (P3) = t(P3) + (P3) = t(P3) + TTTRndTRnd (P2) = 250 + 950 = 1200(P2) = 250 + 950 = 1200

TTTRndTRnd (P4) = t(P4) + (P4) = t(P4) + TTTRndTRnd (P3) = 75 + 950 = 1275(P3) = 75 + 950 = 1275

Average turnaround time:Average turnaround time:

TTTRndTRnd = (350 + 475 + 950 + 1200 + 1275)/5= (350 + 475 + 950 + 1200 + 1275)/5

= 4250/5 = 4250/5 = 850= 850

FCFS ExampleFCFS Example

Page 22: SCHEDULING Kshama Desai Bijal Shah Kishore Putta Kashyap Sheth

Wait TimeWait TimeAverage time in wait before first run:Average time in wait before first run: From the Gantt chart:From the Gantt chart:

WW(P0) = 0(P0) = 0

WW(P1) = (P1) = TTTRndTRnd (P0) = 350(P0) = 350

WW(P2) = (P2) = TTTRndTRnd (P1) = 475(P1) = 475

WW(P3) = (P3) = TTTRndTRnd (P2) = 950(P2) = 950

WW(P4) = (P4) = TTTRndTRnd (P3) = 1200(P3) = 1200

Average wait time:Average wait time:W W = (0 + 350 + 475 + 950 + 1200)/5 = 2975/5 = = (0 + 350 + 475 + 950 + 1200)/5 = 2975/5 =

595595

FCFS ExampleFCFS Example

Page 23: SCHEDULING Kshama Desai Bijal Shah Kishore Putta Kashyap Sheth

Advantages and Disadvantages of Advantages and Disadvantages of FCFSFCFS

AdvantagesAdvantages Easy to understand and easy to programEasy to understand and easy to program It is fairIt is fair Requires only single linked list to keep track of all Requires only single linked list to keep track of all

ready processes ready processes

DisadvantagesDisadvantages Does not perform well in real systemsDoes not perform well in real systems Ignores the service time request and all other criteria Ignores the service time request and all other criteria

that may influence the performance with respect to that may influence the performance with respect to the turnaround or waiting time.the turnaround or waiting time.

Page 24: SCHEDULING Kshama Desai Bijal Shah Kishore Putta Kashyap Sheth

Shortest Job Next (SJN)Shortest Job Next (SJN)

Here each process is associated with its Here each process is associated with its length.length.

The ready queue is maintained in the The ready queue is maintained in the order of increasing job lengths.order of increasing job lengths.

When current process is done, pick the When current process is done, pick the one at the head of the queue and run it.one at the head of the queue and run it.

Page 25: SCHEDULING Kshama Desai Bijal Shah Kishore Putta Kashyap Sheth

SJN ExampleSJN Example

Example loadExample loadi t(pi) i t(pi)

0 350

2 475

4 75

1 125

3 250

0 75 200 450 800 1275

P4 P1 P3 P0 P2

Average turnaround time:TTRnd = (800 + 200 + 1275 + 450 + 75)/5= 2800/5 = 560Average wait time:W = (450 + 74 + 800 + 200 + 0)/5 = 1525/5 = 305

Page 26: SCHEDULING Kshama Desai Bijal Shah Kishore Putta Kashyap Sheth

AdvantagesAdvantages Minimizes wait timeMinimizes wait time

DisadvantagesDisadvantages Long running threads may starveLong running threads may starve

NOTE: NOTE: 1)1) It requires prior knowledge of service time It requires prior knowledge of service time 2)2) Optimal only when all the jobs are available Optimal only when all the jobs are available

simultaneously.simultaneously.

Advantages and Disadvantages of Advantages and Disadvantages of SJNSJN

Page 27: SCHEDULING Kshama Desai Bijal Shah Kishore Putta Kashyap Sheth

Priority SchedulingPriority Scheduling

Each process is assigned a priority and Each process is assigned a priority and the runnable process with highest priority the runnable process with highest priority is allowed to run.is allowed to run.

Priorities can be assigned statically or Priorities can be assigned statically or dynamicallydynamically With static priority, starvation is possibleWith static priority, starvation is possible Dynamic (internal) priority solves the problem Dynamic (internal) priority solves the problem

of starvationof starvation

Page 28: SCHEDULING Kshama Desai Bijal Shah Kishore Putta Kashyap Sheth

Priority SchedulingPriority Scheduling

Example load:Example load:i t(pi) Priority i t(pi) Priority

0 350 5

2 475 3

4 75 4

1 125 2

3 250 1

0 250 375 850 925 1275

P3 P1 P2 P4 P0

Average turnaround time:TTRnd = (1275 + 375 + 850 + 250 + 925)/5= 3675/5 = 735Average wait time:W = (925 + 250 + 375 + 0 + 850)/5 = 2400/5 = 480

Page 29: SCHEDULING Kshama Desai Bijal Shah Kishore Putta Kashyap Sheth

Deadline SchedulingDeadline Scheduling

For hard real-time systems, process must For hard real-time systems, process must finish by a certain timefinish by a certain time

Turnaround time and wait times are Turnaround time and wait times are irrelevantirrelevant

We need to know maximum service time We need to know maximum service time for each processfor each process

Deadline must be met for each period in a Deadline must be met for each period in a process’s lifeprocess’s life

Page 30: SCHEDULING Kshama Desai Bijal Shah Kishore Putta Kashyap Sheth

Deadline Scheduling ExampleDeadline Scheduling Example

Example loadExample loadi t(pi) Deadline i t(pi) Deadline

0 350 575

2 475 1050

4 75 200

1 125 550

3 250 (none)

0 75 200 550 1025 1275

P4 P1 P0 P2 P3

Page 31: SCHEDULING Kshama Desai Bijal Shah Kishore Putta Kashyap Sheth

Round RobinRound Robin

Most widely used scheduling algorithmMost widely used scheduling algorithm

It tries to be fair by equally distributing the It tries to be fair by equally distributing the processing time among all the processesprocessing time among all the processes

When the process uses up its quantum, it When the process uses up its quantum, it is put on the end of the ready listis put on the end of the ready list

Page 32: SCHEDULING Kshama Desai Bijal Shah Kishore Putta Kashyap Sheth

How to choose the length of the How to choose the length of the quantum?quantum?

Setting the quantum length too short Setting the quantum length too short causes many process switches and lowers causes many process switches and lowers the CPU efficiencythe CPU efficiencySetting the quantum length too long may Setting the quantum length too long may cause poor response to short interactive cause poor response to short interactive requestsrequestsSolution:Solution: Around 20-50 msec is a reasonable Around 20-50 msec is a reasonable

compromise.compromise.

Page 33: SCHEDULING Kshama Desai Bijal Shah Kishore Putta Kashyap Sheth

Multiple-Level Queues Multiple-Level Queues

It is an extension of priority schedulingIt is an extension of priority schedulingThe ready queue is partitioned into separate The ready queue is partitioned into separate queues;queues; Foreground Foreground BackgroundBackground

Scheduling must be done between the queuesScheduling must be done between the queuesIt uses 2 strategies of scheduling;It uses 2 strategies of scheduling; One to select the queueOne to select the queue Another to select the process in the queue such as Another to select the process in the queue such as

Round RobinRound Robin

Page 34: SCHEDULING Kshama Desai Bijal Shah Kishore Putta Kashyap Sheth

LINUX Scheduling MechanismLINUX Scheduling Mechanism

Linux threads are kernel threads hence Linux threads are kernel threads hence scheduling is based on threads.scheduling is based on threads.It is based on time-sharing techniques.It is based on time-sharing techniques.Each thread has a scheduling priorityEach thread has a scheduling priority Default value is 20, but can be altered using the Default value is 20, but can be altered using the nice nice

(value)(value) system call to a value of 20-value system call to a value of 20-value Value must be in the range -20 to +19, hence the Value must be in the range -20 to +19, hence the

range of priority is between 1 and 40range of priority is between 1 and 40

Quality of service is proportional to priority.Quality of service is proportional to priority.The scheduler keeps track of what processes The scheduler keeps track of what processes are doing and adjust the priorities periodically, are doing and adjust the priorities periodically, i.e., the priorities are dynamici.e., the priorities are dynamic

Page 35: SCHEDULING Kshama Desai Bijal Shah Kishore Putta Kashyap Sheth

Windows NT Scheduling Windows NT Scheduling MechanismMechanism

Windows NT is a pre-emptive Windows NT is a pre-emptive multithreading OS.multithreading OS.

Even here the unit of scheduling is thread.Even here the unit of scheduling is thread.

It uses 32 numerical thread priorities from It uses 32 numerical thread priorities from 1 to 31 (0 being reserved for system use).1 to 31 (0 being reserved for system use). 16 to 31 for use by time critical operations16 to 31 for use by time critical operations 1 to 15 (dynamic priorities) for program 1 to 15 (dynamic priorities) for program

threads of typical applications.threads of typical applications.

Page 36: SCHEDULING Kshama Desai Bijal Shah Kishore Putta Kashyap Sheth

ReferencesReferences

Nutt, Gary. Operating Systems. Third Nutt, Gary. Operating Systems. Third Edition, Pearson Education Inc, 2004.Edition, Pearson Education Inc, 2004.

Tanenbaum, Andrew. Modern Operating Tanenbaum, Andrew. Modern Operating Systems, Prentice-Hall Of India Pvt. Ltd.Systems, Prentice-Hall Of India Pvt. Ltd.

httphttp://www.windowsitpro.com/Article/ArticleID/://www.windowsitpro.com/Article/ArticleID/302/302.html302/302.html