unit 2

44
Unit 2 Unit 2 Process and Process Process and Process Scheduling Scheduling Btech IT Btech IT Trim VI Trim VI Faculty: Prof. Sonia Relan Faculty: Prof. Sonia Relan

Upload: vikasonia

Post on 03-Dec-2014

240 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Unit 2

Unit 2Unit 2Process and Process Process and Process

SchedulingSchedulingBtech IT Btech IT

Trim VITrim VI

Faculty: Prof. Sonia RelanFaculty: Prof. Sonia Relan

Page 2: Unit 2

17/02/1017/02/10 Faculty: Prof. Sonia RelanFaculty: Prof. Sonia Relan 22

ContentsContents

Process DescriptionProcess Description Process Control blockProcess Control block Threads and Thread managementThreads and Thread management Comparison between threads and processes.Comparison between threads and processes. Process scheduling: Types.Process scheduling: Types. Study and comparison of various scheduling algorithms.Study and comparison of various scheduling algorithms.

Page 3: Unit 2

17/02/1017/02/10 Faculty: Prof. Sonia RelanFaculty: Prof. Sonia Relan 33

Process conceptProcess concept

A process includes:A process includes: program counter program counter StackStack data sectiondata sectionProcess states Process states which characterize the behaviour of processes. which characterize the behaviour of processes. Data structures Data structures used to manage processes.used to manage processes. Ways in which the OS uses these data structures to control Ways in which the OS uses these data structures to control

process execution. process execution. Discuss process management in UNIX SVR4.Discuss process management in UNIX SVR4.

Page 4: Unit 2

17/02/1017/02/10 Faculty: Prof. Sonia RelanFaculty: Prof. Sonia Relan 44

Process in memoryProcess in memory

Page 5: Unit 2

17/02/1017/02/10 Faculty: Prof. Sonia RelanFaculty: Prof. Sonia Relan 55

Process stateProcess state As a process executes, it changes As a process executes, it changes statestate The process’s current activity is known as state of The process’s current activity is known as state of

process. process. Each process may be in one of the following states.Each process may be in one of the following states.1.1. New/dormant stateNew/dormant state::

– The process is being created, The process is being created, – Yet not submitted to the OS.Yet not submitted to the OS.– Dormant, because sometimes not tracked by the OS.Dormant, because sometimes not tracked by the OS.

2. Ready2. Ready: : – The process is waiting to be assigned to a processor.The process is waiting to be assigned to a processor.– Usually process is ready after creation.Usually process is ready after creation.

3. R3. Runningunning::– Instructions are being executed.Instructions are being executed.

4. W4. Waiting/Suspendedaiting/Suspended: : – The process is waiting for some event to occur, other The process is waiting for some event to occur, other

than CPU, such as I/O completion, Synchronization than CPU, such as I/O completion, Synchronization signal.signal.

5. Terminated5. Terminated: : – The process has finished execution.The process has finished execution.

Page 6: Unit 2

17/02/1017/02/10 Faculty: Prof. Sonia RelanFaculty: Prof. Sonia Relan 66

Diagram of Process StateDiagram of Process State

Page 7: Unit 2

17/02/1017/02/10 Faculty: Prof. Sonia RelanFaculty: Prof. Sonia Relan 77

Process CreationProcess Creation

The OS builds a data structure to manage the processThe OS builds a data structure to manage the process Traditionally, the OS created all processesTraditionally, the OS created all processes

– But it can be useful to let a running process create But it can be useful to let a running process create anotheranother

This action is called This action is called process spawningprocess spawning– Parent ProcessParent Process is the original, creating, process is the original, creating, process– Child ProcessChild Process is the new process is the new process

In Unix, each process is identified by its process identifiers In Unix, each process is identified by its process identifiers which is a unique integer. Here each process is created by which is a unique integer. Here each process is created by FORKFORK

Once the OS decides to create a new process it:Once the OS decides to create a new process it:– Assigns a unique process identifierAssigns a unique process identifier– Allocates space for the processAllocates space for the process– Initializes process control blockInitializes process control block– Sets up appropriate linkagesSets up appropriate linkages– Creates or expand other data structuresCreates or expand other data structures

Page 8: Unit 2

17/02/1017/02/10 Faculty: Prof. Sonia RelanFaculty: Prof. Sonia Relan 88

Process TerminationProcess Termination

There must be some way that a process can indicate There must be some way that a process can indicate completion.completion.

This indication may be:This indication may be:– A HALT instruction generating an interrupt alert to the A HALT instruction generating an interrupt alert to the

OS.OS.– A user action (e.g. log off, quitting an application)A user action (e.g. log off, quitting an application)– A fault or errorA fault or error– Parent process terminatingParent process terminating

Page 9: Unit 2

17/02/1017/02/10 Faculty: Prof. Sonia RelanFaculty: Prof. Sonia Relan 99

Modes of ExecutionModes of Execution

Most processors support at least two modes of executionMost processors support at least two modes of execution User modeUser mode

– Less-privileged modeLess-privileged mode– User programs typically execute in this modeUser programs typically execute in this mode

System modeSystem mode– More-privileged modeMore-privileged mode– Kernel of the operating systemKernel of the operating system

Page 10: Unit 2

17/02/1017/02/10 Faculty: Prof. Sonia RelanFaculty: Prof. Sonia Relan 1010

Process Control Block (PCB)Process Control Block (PCB)

OS collects and keep the information about each process in OS collects and keep the information about each process in a record or a data structure called process control block a record or a data structure called process control block (PCB) or Task control block.(PCB) or Task control block.

Hence a separate PCB for each processHence a separate PCB for each process Created when a user creates a process, and is terminated Created when a user creates a process, and is terminated

from the system when the process is killed.from the system when the process is killed. Created and manage by the operating system.Created and manage by the operating system. Contains the process elementsContains the process elements Allows support for multiple processesAllows support for multiple processes

Page 11: Unit 2

17/02/1017/02/10 Faculty: Prof. Sonia RelanFaculty: Prof. Sonia Relan 1111

Process Control Block (PCB)Process Control Block (PCB)

Information stored in a PCB includes:Information stored in a PCB includes:

1.1. Process-IdProcess-Id

2.2. Process stateProcess state

3.3. Process PriorityProcess Priority

4.4. Program counterProgram counter

5.5. Memory pointersMemory pointers

6.6. CPU registersCPU registers

7.7. CPU scheduling informationCPU scheduling information

8.8. Memory-management informationMemory-management information

9.9. Accounting informationAccounting information

10.10. I/O status informationI/O status information

Page 12: Unit 2

17/02/1017/02/10 Faculty: Prof. Sonia RelanFaculty: Prof. Sonia Relan 1212

Context switchContext switch

The process of switching the CPU from an old process to an The process of switching the CPU from an old process to an new process is called the context switch.new process is called the context switch.

The context of the interrupted process must requires saving The context of the interrupted process must requires saving the state.the state.

Context switch time depends on the memory speed, no. of Context switch time depends on the memory speed, no. of CPU registers, special instructions.CPU registers, special instructions.

Page 13: Unit 2

17/02/1017/02/10 Faculty: Prof. Sonia RelanFaculty: Prof. Sonia Relan 1313

CPU Switch From Process to CPU Switch From Process to ProcessProcess

Page 14: Unit 2

17/02/1017/02/10 Faculty: Prof. Sonia RelanFaculty: Prof. Sonia Relan 1414

Process Scheduling QueuesProcess Scheduling Queues

Job queueJob queue – set of all processes in the system – set of all processes in the system Ready queueReady queue – set of all processes residing in main – set of all processes residing in main

memory, ready and waiting to executememory, ready and waiting to execute Device queuesDevice queues – set of processes waiting for an I/O device – set of processes waiting for an I/O device Processes migrate among the various queuesProcesses migrate among the various queues

DispatcherDispatcher is a small program which switches the is a small program which switches the processor from one process to anotherprocessor from one process to another

Page 15: Unit 2

17/02/1017/02/10 Faculty: Prof. Sonia RelanFaculty: Prof. Sonia Relan 1515

Using Two QueuesUsing Two Queues

Page 16: Unit 2

17/02/1017/02/10 Faculty: Prof. Sonia RelanFaculty: Prof. Sonia Relan 1616

Representation of Process Representation of Process SchedulingScheduling

Page 17: Unit 2

17/02/1017/02/10 Faculty: Prof. Sonia RelanFaculty: Prof. Sonia Relan 1717

Unix SVR4Unix SVR4System V Release 4System V Release 4

Uses the model of fig3.15b where most of the OS executes Uses the model of fig3.15b where most of the OS executes in the user processin the user process

System Processes - Kernel mode onlySystem Processes - Kernel mode only User ProcessesUser Processes

– User mode to execute user programs and utilitiesUser mode to execute user programs and utilities– Kernel mode to execute instructions that belong to the Kernel mode to execute instructions that belong to the

kernel.kernel.

Page 18: Unit 2

17/02/1017/02/10 Faculty: Prof. Sonia RelanFaculty: Prof. Sonia Relan 1818

UNIX Process State Transition UNIX Process State Transition DiagramDiagram

Page 19: Unit 2

17/02/1017/02/10 Faculty: Prof. Sonia RelanFaculty: Prof. Sonia Relan 1919

UNIX Process StatesUNIX Process States

Page 20: Unit 2

17/02/1017/02/10 Faculty: Prof. Sonia RelanFaculty: Prof. Sonia Relan 2020

A Unix ProcessA Unix Process

A process in UNIX is a set of data structures that provide A process in UNIX is a set of data structures that provide the OS with all of the information necessary to manage and the OS with all of the information necessary to manage and dispatch processes. dispatch processes.

Two process are unique in UNIX. Two process are unique in UNIX. Process 0: a special process that is created when system Process 0: a special process that is created when system

reboots, it is a swapper process. It spawns Process 1.reboots, it is a swapper process. It spawns Process 1. Process 1 is init process; all other processes in the system Process 1 is init process; all other processes in the system

have process 1 as an ancestor.have process 1 as an ancestor. When a new interactive user logs onto the system, it is When a new interactive user logs onto the system, it is

process 1 that creates a user process for that user.process 1 that creates a user process for that user.

Page 21: Unit 2

17/02/1017/02/10 Faculty: Prof. Sonia RelanFaculty: Prof. Sonia Relan 2121

SchedulingScheduling

Scheduling refers to the set of policies and mechanisms Scheduling refers to the set of policies and mechanisms built into the OS.built into the OS.

A scheduler in an OS module that selects the next job to be A scheduler in an OS module that selects the next job to be admitted into the system and the next process to run.admitted into the system and the next process to run.

3 Types of Schedulers may exist in a complex OS.3 Types of Schedulers may exist in a complex OS.

1.1. Long term SchedulerLong term Scheduler

2.2. Medium term SchedulerMedium term Scheduler

3.3. Short term schedulerShort term scheduler

Page 22: Unit 2

17/02/1017/02/10 Faculty: Prof. Sonia RelanFaculty: Prof. Sonia Relan 2222

Types of SchedulerTypes of Scheduler

Medium term scheduler

Long term scheduler

Short term scheduler

Page 23: Unit 2

17/02/1017/02/10 Faculty: Prof. Sonia RelanFaculty: Prof. Sonia Relan 2323

Schedulers (Cont.)Schedulers (Cont.)

Short-term scheduler is invoked very frequently Short-term scheduler is invoked very frequently (milliseconds) (milliseconds) (must be fast) (must be fast)

Long-term scheduler is invoked very infrequently (seconds, Long-term scheduler is invoked very infrequently (seconds, minutes) minutes) (may be slow) (may be slow)

The long-term scheduler controls the The long-term scheduler controls the degree of degree of multiprogrammingmultiprogramming

Processes can be described as either:Processes can be described as either:– I/O-bound processI/O-bound process – spends more time doing I/O than – spends more time doing I/O than

computations, many short CPU burstscomputations, many short CPU bursts– CPU-bound processCPU-bound process – spends more time doing – spends more time doing

computations; few very long CPU burstscomputations; few very long CPU bursts

Page 24: Unit 2

17/02/1017/02/10 Faculty: Prof. Sonia RelanFaculty: Prof. Sonia Relan 2424

ThreadThread

Resource ownership - process includes a virtual address Resource ownership - process includes a virtual address space to hold the process imagespace to hold the process image

Scheduling/execution- follows an execution path that may be Scheduling/execution- follows an execution path that may be interleaved with other processesinterleaved with other processes

These two characteristics are treated independently by the These two characteristics are treated independently by the operating systemoperating system

Dispatching is associated with a thread or lightweight processDispatching is associated with a thread or lightweight process Resource of ownership is associated with a process or taskResource of ownership is associated with a process or task

MS-DOS supports a single user process and threadMS-DOS supports a single user process and thread UNIX supports multiple user processes but only supports one UNIX supports multiple user processes but only supports one

thread per processthread per process Java run-time environment is a single process with multiple Java run-time environment is a single process with multiple

threadsthreads

Page 25: Unit 2

17/02/1017/02/10 Faculty: Prof. Sonia RelanFaculty: Prof. Sonia Relan 2525

Threads and ProcessesThreads and Processes

Page 26: Unit 2

17/02/1017/02/10 Faculty: Prof. Sonia RelanFaculty: Prof. Sonia Relan 2626

Benefits of ThreadsBenefits of Threads

Takes less time to create a new thread than a processTakes less time to create a new thread than a process Less time to terminate a thread than a processLess time to terminate a thread than a process Less time to switch between two threads within the same Less time to switch between two threads within the same

process.process. Since threads within the same process share memory and Since threads within the same process share memory and

files, they can communicate with each other without files, they can communicate with each other without invoking the kernelinvoking the kernel

Suspending a process involves suspending all threads of Suspending a process involves suspending all threads of the process since all threads share the same address spacethe process since all threads share the same address space

Termination of a process terminates all threads within the Termination of a process terminates all threads within the processprocess

Page 27: Unit 2

17/02/1017/02/10 Faculty: Prof. Sonia RelanFaculty: Prof. Sonia Relan 2727

Preemptive vs. NonPreemptivePreemptive vs. NonPreemptive

Preemptive:Preemptive: When process enters into ready queue, its priority is When process enters into ready queue, its priority is

compared with the priority of the currently running processcompared with the priority of the currently running process And if priority of a newly arrived process is higher than the And if priority of a newly arrived process is higher than the

priority of the currently running process, the job in service priority of the currently running process, the job in service is interrupted and return to the queue.is interrupted and return to the queue.

Drawback is starvation/indefinite blocking.Drawback is starvation/indefinite blocking. Solution is aging.Solution is aging.

Nonpreemptive:Nonpreemptive: A nonpreemptive priority scheduling algorithm will simple A nonpreemptive priority scheduling algorithm will simple

put the new process at the head of the ready queue.put the new process at the head of the ready queue.

Page 28: Unit 2

17/02/1017/02/10 Faculty: Prof. Sonia RelanFaculty: Prof. Sonia Relan 2828

Different Scheduling AlgorithmsDifferent Scheduling Algorithms

Diff. scheduling algorithms for deciding which of the Diff. scheduling algorithms for deciding which of the process in the ready queue is to be allocated to the CPU.process in the ready queue is to be allocated to the CPU.

1.1. Round Robin SchedulingRound Robin Scheduling

2.2. Priority based SchedulingPriority based Scheduling

3.3. First come First Served Scheduling FCFSFirst come First Served Scheduling FCFS

4.4. Shortest Job First (SJF)Shortest Job First (SJF)

5.5. Multilevel Queue Scheduling (MLQ)Multilevel Queue Scheduling (MLQ)

6.6. MLQ with feedback Scheduling.MLQ with feedback Scheduling.

Page 29: Unit 2

17/02/1017/02/10 Faculty: Prof. Sonia RelanFaculty: Prof. Sonia Relan 2929

Basic TerminologiesBasic Terminologies

CPU UtilizationCPU Utilization– Gives information of how much time CPU is busyGives information of how much time CPU is busy

ThroughputThroughput– a measure of work done in a per unit of timea measure of work done in a per unit of time

Turn around timeTurn around time– Time taken by the process, from the moment it is Time taken by the process, from the moment it is

submitted to the ready queue until its execution is submitted to the ready queue until its execution is completed by a systemcompleted by a system

– Sum of Waiting time and Execution time.Sum of Waiting time and Execution time. Waiting timeWaiting time

– Amount of time that a process spends waiting in the Amount of time that a process spends waiting in the ready queueready queue

Response timeResponse time– In interactive systems it is defined as the time from the In interactive systems it is defined as the time from the

submission of a request to the first response is produced.submission of a request to the first response is produced.Hence maximizing the CPU Utilization and throughput, and Hence maximizing the CPU Utilization and throughput, and

to minimize TT, waiting time, and response timeto minimize TT, waiting time, and response time

Page 30: Unit 2

17/02/1017/02/10 Faculty: Prof. Sonia RelanFaculty: Prof. Sonia Relan 3030

FCFSFCFS

The simplest Scheduling algorithmThe simplest Scheduling algorithm Selects process in the order of arrival, managed with a FIFO queue.Selects process in the order of arrival, managed with a FIFO queue. May result in quite long average waiting time.May result in quite long average waiting time. For Ex. consider the following set of processes that arrive at time 0, For Ex. consider the following set of processes that arrive at time 0,

with the length of CPU burst time given in time units.with the length of CPU burst time given in time units. Process Process FCFS Scheduling algorithm is Non preemptive. FCFS Scheduling algorithm is Non preemptive.

ProcessProcess Burst TimeBurst Time

P1P1 3232

P2P2 44

P3P3 44

Page 31: Unit 2

17/02/1017/02/10 Faculty: Prof. Sonia RelanFaculty: Prof. Sonia Relan 3131

FCFS contd.FCFS contd.

Ex. Contd. If the process arrive one after another in the order P1, Ex. Contd. If the process arrive one after another in the order P1, P2, P3, the result shown in the following Gantt chart.P2, P3, the result shown in the following Gantt chart.

Turnaround time for process P1= W.T + B.T = 0Turnaround time for process P1= W.T + B.T = 0 TT for P2 = 36TT for P2 = 36 TT for P3 = 40TT for P3 = 40 Avg TT = (0+36+40)/3 = 36 time units.Avg TT = (0+36+40)/3 = 36 time units. Waiting time for process P1 = 0 time units.Waiting time for process P1 = 0 time units. W.T for P2 = 32W.T for P2 = 32 WT for P3 = 36WT for P3 = 36 Avg WT = (0+32+36) / 3 =22.66 time units.Avg WT = (0+32+36) / 3 =22.66 time units. Solve the processes with opposite order and compare TT and WT.Solve the processes with opposite order and compare TT and WT.

P1P1 P2P2 P3P3

0 32 36

40

Page 32: Unit 2

17/02/1017/02/10 Faculty: Prof. Sonia RelanFaculty: Prof. Sonia Relan 3232

Round Robin SchedulingRound Robin Scheduling

One of the oldest, simplest, fairest, and most widely used One of the oldest, simplest, fairest, and most widely used algorithm.algorithm.

Specially designed for interactive environments, such as time- Specially designed for interactive environments, such as time- sharing systems.sharing systems.

Primary requirement is to provide reasonably good response Primary requirement is to provide reasonably good response time and share resources equally among all users.time and share resources equally among all users.

So Each process is assigned a time interval, called its time So Each process is assigned a time interval, called its time quantum or time slice to get execute. quantum or time slice to get execute.

If a process needs more time to complete after exhausting its If a process needs more time to complete after exhausting its time slice , it is placed at the end of the ready queue.time slice , it is placed at the end of the ready queue.

Hence ready queue is treated as circular queue.Hence ready queue is treated as circular queue. RR scheduling algorithm is preemptive.RR scheduling algorithm is preemptive. The efficiency of the RR algorithm depends heavily on the size of The efficiency of the RR algorithm depends heavily on the size of

the time quantum,the time quantum, Setting the time quantum too short causes too many process Setting the time quantum too short causes too many process

switches and lowers the CPU performance, switches and lowers the CPU performance, Too long may cause poor performance to short interactive Too long may cause poor performance to short interactive

requests.requests.

Page 33: Unit 2

17/02/1017/02/10 Faculty: Prof. Sonia RelanFaculty: Prof. Sonia Relan 3333

RR contd.RR contd.

For Ex. Consider the set of For Ex. Consider the set of processes that arrive at time processes that arrive at time 0, with the length of the CPU 0, with the length of the CPU burst time given in time units, burst time given in time units, quantum = 5 time unitsquantum = 5 time units

Gantt Chart:Gantt Chart:

TT(p1) = 33TT(p1) = 33 TT(p2) = 9TT(p2) = 9 TT(p3) = 13TT(p3) = 13 Avg TT = 18.33 Avg TT = 18.33

ProcessProcess Burst TimeBurst Time

P1P1 2525

P2P2 44

P3P3 44

PP11 P2P2 P3P3 P1P1 P1P1 P1P1 P1P1

0 5 9 13 18 23 28 33

WT(p1) = 8

WT(p2) = 5

WT(p3) = 9

Avg WT = 7.33 time units

Page 34: Unit 2

17/02/1017/02/10 Faculty: Prof. Sonia RelanFaculty: Prof. Sonia Relan 3434

Priority based Scheduling Priority based Scheduling

Each process is assigned a priority level and the scheduler always Each process is assigned a priority level and the scheduler always chooses the higher priority process from the ready queue.chooses the higher priority process from the ready queue.

Equal priority process are scheduled using FCFS algorithmEqual priority process are scheduled using FCFS algorithm Priority based scheduling may be preemptive or Non preemptive.Priority based scheduling may be preemptive or Non preemptive. In some systems low numbers are used to represent higher priority, In some systems low numbers are used to represent higher priority,

while in some higher numbers are used to represent higher priority.while in some higher numbers are used to represent higher priority. We will consider low numbers represent higher priority.We will consider low numbers represent higher priority. For Ex. For Ex. Consider the set of processes that arrive at time 0, For Ex. For Ex. Consider the set of processes that arrive at time 0,

with the length of the CPU burst time given in time units, with the length of the CPU burst time given in time units,

ProcessProcess Burst Burst TimeTime

PriorityPriority

P1P1 1111 44

P2P2 22 11

P3P3 44 44

P4P4 22 55

P5P5 66 33

Page 35: Unit 2

17/02/1017/02/10 Faculty: Prof. Sonia RelanFaculty: Prof. Sonia Relan 3535

Priority BasedPriority Based

If the process arrive in the If the process arrive in the order P1, P2, P3, P4, P5, using order P1, P2, P3, P4, P5, using priority based scheduling we priority based scheduling we get the Gantt chart:get the Gantt chart:

Avg WT= (0 +2 +8 +19+23) Avg WT= (0 +2 +8 +19+23) =10.5 time units=10.5 time units

P2P2 P5P5 P1P1 P3P3 P4P4

0 2 8 19 23 25

Page 36: Unit 2

17/02/1017/02/10 Faculty: Prof. Sonia RelanFaculty: Prof. Sonia Relan 3636

Priority BasedPriority Based

Lower priority process can suffer from starvation. Lower priority process can suffer from starvation. Hence solution is aging, in which the priority of each Hence solution is aging, in which the priority of each

process is gradually increased after the process spends a process is gradually increased after the process spends a certain amount of time.certain amount of time.

Page 37: Unit 2

17/02/1017/02/10 Faculty: Prof. Sonia RelanFaculty: Prof. Sonia Relan 3737

Shortest job First SchedulingShortest job First Scheduling

Available CPU is assigned to the next process that has the Available CPU is assigned to the next process that has the smallest CPU Burst.smallest CPU Burst.

Can be implemented in either the non-preemptive or Can be implemented in either the non-preemptive or preemptive mode.preemptive mode.

With nonpreemptive, the SJF scheduler is invoked whenever a With nonpreemptive, the SJF scheduler is invoked whenever a job is completed or the running process surrenders control to job is completed or the running process surrenders control to the OS.the OS.

With preemption, SJF is known as Shortest remaining itme With preemption, SJF is known as Shortest remaining itme next (SRTN) scheduling , next (SRTN) scheduling , – In this a new arrived process burst time is compared with In this a new arrived process burst time is compared with

the remaining processor burst time.the remaining processor burst time.– If preempted, the running process joins the ready queue.If preempted, the running process joins the ready queue.

SJF is the optimal since gives the minimum average waiting SJF is the optimal since gives the minimum average waiting time for a given set of processes.time for a given set of processes.

Real difficulty is knowing the length of the next CPU request, Real difficulty is knowing the length of the next CPU request, hence used in long term scheduling.hence used in long term scheduling.

Page 38: Unit 2

17/02/1017/02/10 Faculty: Prof. Sonia RelanFaculty: Prof. Sonia Relan 3838

SJF contd.SJF contd. Tn+1 = Tn+1 = X tn + (1 - X ) TnX tn + (1 - X ) Tn

Tn+1:- predicted Value for the next CPU burst.Tn+1:- predicted Value for the next CPU burst. tn :- length of the nth CPU burst.tn :- length of the nth CPU burst. Tn :- stores the past historyTn :- stores the past history X :- relative weight of recent and past history lise in between 0 & 1.X :- relative weight of recent and past history lise in between 0 & 1. A preemptive SJF algorithm can preempt the running process, whereas A preemptive SJF algorithm can preempt the running process, whereas

nonpreemptive will allow the running process to finish its CPU burst.nonpreemptive will allow the running process to finish its CPU burst. For Ex. Consider the foll. ProcessFor Ex. Consider the foll. Process

ProcessProcess Arrival Arrival TimeTime

Burst TimeBurst Time

P1P1 00 1212

P2P2 11 1010

P3P3 11 88

P4P4 22 77

P5P5 44 44

Page 39: Unit 2

17/02/1017/02/10 Faculty: Prof. Sonia RelanFaculty: Prof. Sonia Relan 3939

Non Preemptive SJFNon Preemptive SJF

Ex. Consider the following processes. Schedule them according to Ex. Consider the following processes. Schedule them according to Preemptive SJF, Nonpreemptive SJF, Also find Avg. Turn around Preemptive SJF, Nonpreemptive SJF, Also find Avg. Turn around time and Avg. Waiting time.time and Avg. Waiting time.

P1P1 P3P3 P4P4 P3P3 P2P2

0 12 16 23 31 41

ProcessProcess Arrival Arrival TimeTime

Burst Burst TimeTime

P1P1 00 88

P2P2 11 44

P3P3 22 99

P4P4 33 55

Gantt Chart:-

Page 40: Unit 2

17/02/1017/02/10 Faculty: Prof. Sonia RelanFaculty: Prof. Sonia Relan 4040

Multilevel Queue SchedulingMultilevel Queue Scheduling

Ready queue is partitioned into separate queues:Ready queue is partitioned into separate queues:– foreground (interactive)foreground (interactive)– background (batch)background (batch)

Each queue has its own scheduling algorithmEach queue has its own scheduling algorithm– foreground – RRforeground – RR– background – FCFSbackground – FCFS

Scheduling must be done between the queuesScheduling must be done between the queues– Fixed priority scheduling; (i.e., serve all from foreground Fixed priority scheduling; (i.e., serve all from foreground

then from background). Possibility of starvation.then from background). Possibility of starvation.– Time slice – each queue gets a certain amount of CPU Time slice – each queue gets a certain amount of CPU

time which it can schedule amongst its processes; i.e., time which it can schedule amongst its processes; i.e., 80% to foreground in RR80% to foreground in RR

– 20% to background in FCFS20% to background in FCFS

Page 41: Unit 2

17/02/1017/02/10 Faculty: Prof. Sonia RelanFaculty: Prof. Sonia Relan 4141

MLQ contd.MLQ contd.

Page 42: Unit 2

17/02/1017/02/10 Faculty: Prof. Sonia RelanFaculty: Prof. Sonia Relan 4242

MLQ with feedback SchedulingMLQ with feedback Scheduling

This algorithm may be used to increase the effectiveness This algorithm may be used to increase the effectiveness and adaptiveness of scheduling.and adaptiveness of scheduling.

A process can move between the various queues; aging can A process can move between the various queues; aging can be implemented this waybe implemented this way

Multilevel-feedback-queue scheduler defined by the Multilevel-feedback-queue scheduler defined by the following parameters:following parameters:– number of queuesnumber of queues– scheduling algorithms for each queuescheduling algorithms for each queue– method used to determine when to upgrade a processmethod used to determine when to upgrade a process– method used to determine when to demote a processmethod used to determine when to demote a process– method used to determine which queue a process will method used to determine which queue a process will

enter when that process needs serviceenter when that process needs service Most complex to design, since instead of only one queue, Most complex to design, since instead of only one queue,

the OS will have to maintain three queue headers or the no. the OS will have to maintain three queue headers or the no. of queue can vary based on the issue of the OS design.of queue can vary based on the issue of the OS design.

Page 43: Unit 2

17/02/1017/02/10 Faculty: Prof. Sonia RelanFaculty: Prof. Sonia Relan 4343

Example of Multilevel Feedback Example of Multilevel Feedback QueueQueue

Three queues: Three queues:

– QQ00 – RR with time quantum 8 milliseconds – RR with time quantum 8 milliseconds

– QQ11 – RR time quantum 16 milliseconds – RR time quantum 16 milliseconds

– QQ22 – FCFS – FCFS SchedulingScheduling

– A new job enters queue A new job enters queue QQ00 which is servedwhich is served FCFS. When it FCFS. When it gains CPU, job receives 8 milliseconds. If it does not gains CPU, job receives 8 milliseconds. If it does not finish in 8 milliseconds, job is moved to queue finish in 8 milliseconds, job is moved to queue QQ11..

– At At QQ11 job is again served FCFS and receives 16 additional job is again served FCFS and receives 16 additional milliseconds. If it still does not complete, it is preempted milliseconds. If it still does not complete, it is preempted and moved to queue and moved to queue QQ22..

Page 44: Unit 2

17/02/1017/02/10 Faculty: Prof. Sonia RelanFaculty: Prof. Sonia Relan 4444

Multilevel Feedback QueuesMultilevel Feedback Queues