• No results found

Types of Memory Allocation Schemes

N/A
N/A
Protected

Academic year: 2021

Share "Types of Memory Allocation Schemes"

Copied!
15
0
0

Loading.... (view fulltext now)

Full text

(1)

Memory Management: Early Systems Memory Management: Early Systems Single-User Conf

Single-User Configurations igurations → Fixed Partitions → Dynam→ Fixed Partitions → Dynamic Partitions → Relocic Partitions → Relocatable Dynamicatable Dynamic Partitions

Partitions

“Memory is the primary and fundamental power, without which there could be no “Memory is the primary and fundamental power, without which there could be no other intellectual operation.”

other intellectual operation.” —Samuel Johnson (1709–1784)—Samuel Johnson (1709–1784) Types of memory allocation

Types of memory allocation schemes:schemes: •

• Single-user systemsSingle-user systems •

• Fixed partitionsFixed partitions •

• Dynamic partitionsDynamic partitions •

• Relocatable dynamic partitionsRelocatable dynamic partitions 1. Single-User Contiguous Scheme:

1. Single-User Contiguous Scheme: Program is loaded in its entirety into memory andProgram is loaded in its entirety into memory and allocated as much contiguous space in memory as it needs.

allocated as much contiguous space in memory as it needs. •

• Jobs processed sequentially in  Jobs processed sequentially in single-user systemssingle-user systems •

• Requires minimaRequires minimal work by l work by the Memory Managerthe Memory Manager •• Register to store the base addressRegister to store the base address

•• AccumulatoAccumulator to r to keep track of the keep track of the program sizeprogram size Disadvantages of Single-User Contiguous Scheme: Disadvantages of Single-User Contiguous Scheme:

• Doesn’t support multiprogrammingDoesn’t support multiprogramming •

• Not cost effectiveNot cost effective 2. Fixed Partitions:

2. Fixed Partitions: Main memory is partitioned; one Main memory is partitioned; one partition/jopartition/job.b. •

• Allows multiprogrammingAllows multiprogramming •

• Partition sizes remain static unless and until computer system id shut down,Partition sizes remain static unless and until computer system id shut down, reconfigured

reconfigured, , and restartedand restarted •

• Requires protection of the Requires protection of the job’s memory spacejob’s memory space •

• Requires matching job size with Requires matching job size with partition sizepartition size  To allocate memory spaces to

 To allocate memory spaces to jobs, the operating system’s Memory Manager must jobs, the operating system’s Memory Manager must keepkeep a table as

a table as shown below:shown below:

Disadvantages: Disadvantages:

• Requires entire program to be Requires entire program to be stored contiguouslystored contiguously •

•  Jobs are allocated space on the  Jobs are allocated space on the basis of first available partition of required sizebasis of first available partition of required size   T

  Tablable e 2.12.1: : A A simsimpliplifiefied d fifixexed d parpartittition ion mememormoryy table with the free partition shaded

table with the free partition shaded

F

Figurigure e 2.1: Main 2.1: Main memormemory y use during use during fixfixeded

partition allocation of Table 2.1

partition allocation of Table 2.1

NOTE: Job 3 must wait even though 70K of free NOTE: Job 3 must wait even though 70K of free sp

spacace e is is avavaiailalablble e inin PParartititition on 1 1 whwherere e JoJob b 11 occupies only 30K of the 100K

(2)

• Works well only if Works well only if all of the jobs are of the same sizall of the jobs are of the same size or if the sizes are known aheade or if the sizes are known ahead of time

of time •

• Arbitrary partition sizes lead to Arbitrary partition sizes lead to undesired resultsundesired results •

•  Too small a partition size results in large jobs having longer turnaround time Too small a partition size results in large jobs having longer turnaround time •

•  Too large a partition size results in memory waste or  Too large a partition size results in memory waste or internal fragmentatiinternal fragmentationon 3. Dynamic Partitions:

3. Dynamic Partitions: Jobs are given only as Jobs are given only as much memory as they request when theymuch memory as they request when they are loaded

are loaded •

• Available memory is kept in contiguous blocksAvailable memory is kept in contiguous blocks •

• Memory waste is Memory waste is comparatively smallcomparatively small Disadvantages:

Disadvantages: •

• Fully utilizes memory only when the first jobs are Fully utilizes memory only when the first jobs are loadedloaded •

• Subsequent allocatioSubsequent allocation leads n leads to memory waste or to memory waste or external fragmentationexternal fragmentation

Best-Fit versus First-Fit Allocation Best-Fit versus First-Fit Allocation Free partitions are allocated on the

Free partitions are allocated on the following basis:

following basis:

First-fit memory allocation:

First-fit memory allocation: FirstFirst

partition fitting the partition fitting the requirements

requirements Figure 2.2: Main memory use during dynamic partition allocation

(3)

• Leads to fast allocation of memoryLeads to fast allocation of memory space

space Advantage:

Advantage: Faster in making allocationFaster in making allocation

Disadvantage:

Disadvantage: Leads to memory wasteLeads to memory waste

Best-fit memory allocation:

Best-fit memory allocation: Smallest partition fittingSmallest partition fitting the

the requirementrequirementss •

• Results in least wasted Results in least wasted spacespace •

• Internal fragmentation reduced but not eliminateInternal fragmentation reduced but not eliminate Advantage:

Advantage: Makes the best use Makes the best use of memory spaceof memory space Disadvantage:

Disadvantage: Slower in making allocationSlower in making allocation

Algorithm for First-Fit: Algorithm for First-Fit:

• Assumes Memory Manager keeps two lists, one for freeAssumes Memory Manager keeps two lists, one for free memory and one for busy memory blocks

memory and one for busy memory blocks •

• Loop compares the size of each job to the size of eachLoop compares the size of each job to the size of each memory block until a block is found that’s large enough memory block until a block is found that’s large enough to fit the job

to fit the job •

• Job is stored into that  Job is stored into that block of memoryblock of memory •

• Memory Manager movesMemory Manager moves out of the loop to fetch the out of the loop to fetch the next job from the entry next job from the entry queue

queue •

• If the entire list is searchedIf the entire list is searched in vain, then the job is in vain, then the job is placed into a waiting placed into a waiting queue

queue •

• The Memory Manager then The Memory Manager then fetches the next job and fetches the next job and repeats the process repeats the process

• Algorithm for Best-Fit:Algorithm for Best-Fit:

 – 

 –  Goal: find the smallest memory block into which the Goal: find the smallest memory block into which the job will fitjob will fit

Figure 2.3: An example of a first-fit

Figure 2.3: An example of a first-fit

free scheme

free scheme

  T

  Tabable le 2.2.2: 2: StStatatus us of of eaeachch

mem

memory ory bloblock ck befbeforore e anandd

after a request is made for a

after a request is made for a

bl

blocock k of of 20200 0 spspacaces es ususiningg

the first-fit algorithm

the first-fit algorithm

Figure 2.3: An example of a best-fit free

(4)

 – 

 –  Entire table must be Entire table must be searched before allocationsearched before allocation

• Hypothetical allocation schemes:Hypothetical allocation schemes:

 – 

 –  Next-fit:Next-fit: Starts searching from last allocated block, for the next Starts searching from last allocated block, for the next available block whenavailable block when a new job arrives

a new job arrives

 – 

 –  Worst-fit:Worst-fit:Allocates the largest free available block to the Allocates the largest free available block to the new jobnew job •

• Opposite of best-fitOpposite of best-fit •

• Good way to explore the theory of memory allocation; might not be the best choice forGood way to explore the theory of memory allocation; might not be the best choice for an actual system

an actual system

Deallocation:

Deallocation: Freeing an allocated memory spaceFreeing an allocated memory space

 For fixed-partition systemFor fixed-partition system:: •

• Straightforward processStraightforward process •

• When job completeWhen job completes, Memory Manager resets the s, Memory Manager resets the statstatus us of the of the job’job’s s memomemoryry block to “free”

block to “free” •

• Any code—for example, binary values with 0 indicating free and 1 indicating busyAny code—for example, binary values with 0 indicating free and 1 indicating busy —may be used

—may be used

 For dynamic-partition systemFor dynamic-partition system::

• Algorithm tries to combine free Algorithm tries to combine free areas of memory whenever areas of memory whenever possiblepossible •

• Three cases: Three cases: Case 1:

Case 1: When the block to When the block to be deallocated is adjacent to another free be deallocated is adjacent to another free blockblock Case 2:

Case 2: When the block to be deallocated is between two free blocksWhen the block to be deallocated is between two free blocks Case 3:

Case 3: When the block to When the block to be deallocated is isolated from other free blocksbe deallocated is isolated from other free blocks Case 1: Joining Two Free Blocks

Case 1: Joining Two Free Blocks

 Change list must reflect starting address of the new free Change list must reflect starting address of the new free blockblock •

• In the In the examexample, 7600—wple, 7600—which was the hich was the addraddress of ess of the first instruthe first instructioction n of the of the jobjob that just released this block

that just released this block

 Memory block size for the new free space must be changed to show its newMemory block size for the new free space must be changed to show its new size—that is, the combined total of the

size—that is, the combined total of the two free partitionstwo free partitions •

• In the example, (200 + 5)In the example, (200 + 5)

  T

  Tabable le 2.32.3: : StaStatus tus of of eaeachch

memo

memory ry block before andblock before and

after a request is made for

after a request is made for

a a mmememorory y bblolocck k of of 202000 sp spacaces es ususining g ththe e bebestst-f-fitit algorithm algorithm

(5)

Case 2: Joining Three Free Blocks Case 2: Joining Three Free Blocks

Deallocated memory space is between two free memory blocks Deallocated memory space is between two free memory blocks

 Change list to reflect the Change list to reflect the starting address of the new free blockstarting address of the new free block •

• In the example, 7560— which was the In the example, 7560— which was the smallest beginning addrsmallest beginning addressess

 Sizes of the three Sizes of the three free partitions must be combinedfree partitions must be combined •

• In the example, (20 + 20 + In the example, (20 + 20 + 205)205)

 Combined entry is given the status of Combined entry is given the status of null entrynull entry •

• In the example, 7600In the example, 7600

Case 3: Deallocating an Isolated Block  Case 3: Deallocating an Isolated Block  Space to be deallocated is isolated from other

Space to be deallocated is isolated from other free areasfree areas

 System learns that the memory block to be released is not adjacent to anySystem learns that the memory block to be released is not adjacent to any free blocks of memory, it

free blocks of memory, it is between two other busy areasis between two other busy areas

 Must search the table for a null entryMust search the table for a null entry

 Null entry in the busy list occurs when a memory block between two otherNull entry in the busy list occurs when a memory block between two other busy memory blocks is returned to the free

busy memory blocks is returned to the free listlist

(LEFT) Table 2.4: Original free list before

(LEFT) Table 2.4: Original free list before

deallocation

deallocation

for Case 1

for Case 1

(TOP) Table 2.5: Free list after deallocation for

(TOP) Table 2.5: Free list after deallocation for

Case 1

Case 1

 Table 2.6: Original free list before deallocation for

 Table 2.6: Original free list before deallocation for

Case 2

Case 2

 Table 2.7: Free list after job has released

 Table 2.7: Free list after job has released

memory

memory

 Table 2.8: Original free list before deallocation for

 Table 2.8: Original free list before deallocation for

Case 3

Case 3

  Th

  The e job to job to be be deadeallollocatcated is ed is of size 445 of size 445 andand begin

begins s at at localocation 8805. tion 8805. The The asteasterisk indicatrisk indicateses the soon-to-be-free memory block.

the soon-to-be-free memory block.

 Table 2.9: Memory list before

 Table 2.9: Memory list before

deallocation

(6)

4. Relocatable Dynamic Partitions: 4. Relocatable Dynamic Partitions:

• MemoMemory ry ManaManager ger relorelocates programcates programs s toto gather together all of the empty

gather together all of the empty blocksblocks •

• Compact the empty blocks to make oneCompact the empty blocks to make one b

blloocck k oof f mmeemmoorry y llaarrgge e eennoouuggh h ttoo ac

accocommmmododatate e sosome me or or alall l of of ththe e jojobsbs waiting to get in

waiting to get in Compaction:

Compaction: ReReclclaiaimiming ng frfragagmementnteded sections of the memory space sections of the memory space •

• EEvveerry y pprrooggrraam m iin n mmeemmoorry y mmuusst t bbee relocated so they are contiguous

relocated so they are contiguous •

• OOpeperraatitinng g ssysystetem m mmuusst t ddisisttiningguuisishh between addresses and data values

between addresses and data values

•• EvEverery y adaddrdresess s mmusust t be be adadjujuststed ed toto account for the program’s new location account for the program’s new location in memory

in memory

•• Data values must be left Data values must be left alonealone Compaction issues:

Compaction issues: •

• What goes on behind the What goes on behind the scenes when relocation and compaction take place?scenes when relocation and compaction take place? •

• What keeps track of What keeps track of how far each job has how far each job has moved from its original storage area?moved from its original storage area? •

• What lists have to be What lists have to be updated?updated? What lists have to

What lists have to be updated?be updated? •

• Free list must show the partition for the Free list must show the partition for the new block of free new block of free memorymemory •

• Busy list must show the new locations for all of the jobs already in process that wereBusy list must show the new locations for all of the jobs already in process that were relocated

relocated •

• Each job will have a new address except for those that were already at Each job will have a new address except for those that were already at the lowest memorythe lowest memory locations

locations Special-p

Special-purpose registers are urpose registers are used for used for relocation:relocation: •

• Bounds registerBounds register

•• Stores highest location accessible by each Stores highest location accessible by each programprogram •

• Relocation registerRelocation register

•• Contains the value that must be added to each address referenced in the program so it willContains the value that must be added to each address referenced in the program so it will be able to

be able to access the correct memory addresses after relocationaccess the correct memory addresses after relocation

•• If the program isn’t relocated, the value stored If the program isn’t relocated, the value stored in the program’s relocation register is zeroin the program’s relocation register is zero  Table 2.10: Busy list after the job has released its

 Table 2.10: Busy list after the job has released its

memor

memoryy. . The The asteasterisk indicatrisk indicates es the the new new nullnull

entry in the busy list.

entry in the busy list.

 T

 Table 2.11: able 2.11: FFree list ree list after the after the job has job has releareleasedsed

its memory. The asterisk indicates the new free

its memory. The asterisk indicates the new free

block entry replacing the null entry

(7)

Figure 2.7: Three snapshots of memory before and after compaction

(8)

• Compacting and relocating optimizCompacting and relocating optimizes the use es the use of memory and thus improvesof memory and thus improves throughput

throughput •

• Options Options for when anfor when and how often it sd how often it should be done:hould be done: •• When a certain percentage of memory is busyWhen a certain percentage of memory is busy •• When there are jobs waiting to get inWhen there are jobs waiting to get in

•• After a prescribed amount of time has elapsedAfter a prescribed amount of time has elapsed Goal:

Goal: Optimize processinOptimize processing time and memory use g time and memory use while keeping overhead as low as possiblewhile keeping overhead as low as possible Summary

Summary •

• Four memory management techniques were used in Four memory management techniques were used in early systems: single-userearly systems: single-user systems, fixed partitions, dynamic

systems, fixed partitions, dynamic partitions, and relocatable dynamic partitionspartitions, and relocatable dynamic partitions •

• Memory waste in dynamic partitions is comparatively small as compared to fixedMemory waste in dynamic partitions is comparatively small as compared to fixed partitions

partitions •

• First-fit is faster in making allocation but leads to First-fit is faster in making allocation but leads to memory wastememory waste •

• Best-fit makes the best use of Best-fit makes the best use of memory space but slower in making allocationmemory space but slower in making allocation •

• Compacting and relocating optimizCompacting and relocating optimizes the use es the use of memory and thus improvesof memory and thus improves throughput

throughput •

• All All techniques reqtechniques require that the entuire that the entire program mire program must:ust: a)

a) Be loaded inBe loaded into memorto memoryy b)

b) Be stoBe stored contired contiguouslyguously c)

c) Remain in mRemain in memory until the joemory until the job is completedb is completed •

• Each technique puts severe restrictions on the size of the Each technique puts severe restrictions on the size of the jobs: can only be jobs: can only be as largeas large as the

as the largest partitions in memorylargest partitions in memory

F

Fiigguurre e 2.2.8: 8: CCoonntteenntts s oof f 

r

relelococatatioion n rregegisisteter r anandd

close-up of Job 4 memory

close-up of Job 4 memory

ar

area ea (a) (a) befbeforore e rereloclocatationion

and (b) after relocation and

and (b) after relocation and

compaction

(9)

Processor Management Processor Management •

• Program (Job):Program (Job):

 – 

 –  A unit of A unit of work that has been submitted by user work that has been submitted by user to an operating systemto an operating system  – 

 –  An inactive unit, such as a file stored on a diskAn inactive unit, such as a file stored on a disk

• Process (Task):Process (Task):

 – 

 –  An active entity, which requires a set of resources, including a processor andAn active entity, which requires a set of resources, including a processor and special registers, to perform its

special registers, to perform its functionfunction  – 

 –  A single instance of an A single instance of an executable programexecutable program

• Processor (CPU):Processor (CPU): performs calculations and executes performs calculations and executes programsprograms

 – 

 –  In In single-ussingle-user systems:er systems: •

• Processor is busy only when user is executing a job, at all other times itProcessor is busy only when user is executing a job, at all other times it is idle

is idle •

• Processor management is simpleProcessor management is simple  – 

 –  In a In a multiprogramminmultiprogramming environment:g environment: •

• Processor must be allocated to each job in Processor must be allocated to each job in a fair and efficient mannera fair and efficient manner •

• Requires schedulinRequires scheduling policy and g policy and a scheduling algorithma scheduling algorithm

• Interrupt:Interrupt: A hardware sA hardware signal that susignal that suspends pends execution of a pexecution of a program and arogram and activatesctivates the execution of interrupt handler

the execution of interrupt handler •

• Context Switch:Context Switch: Saving a job’s Saving a job’s processing informprocessing information in ation in its PCB when its PCB when interruptedinterrupted

• Context switching occurs in all Context switching occurs in all preemptivepreemptive

 Job Scheduling Versus Process Scheduling  Job Scheduling Versus Process Scheduling •

• Processor Manager has two sub Processor Manager has two sub managers:managers:

 – 

 –   Job  Job ScheduleScheduler:r:

•• In charge of job schedulingIn charge of job scheduling

•• Initiates the job based on certain criteriaInitiates the job based on certain criteria  – 

 –  Process Scheduler:Process Scheduler:

•• In charge of In charge of process schedulingprocess scheduling

•• Assigns the CPU to execute processes of those jobs placed on READYAssigns the CPU to execute processes of those jobs placed on READY queue by Job Scheduler

queue by Job Scheduler  Job Scheduler

 Job Scheduler (High level scheduler):(High level scheduler):  – 

 –  Initiates the job based on certain criteriaInitiates the job based on certain criteria  – 

 –  Puts jobs in a sequence that uses all system’s resources as fully as possiblePuts jobs in a sequence that uses all system’s resources as fully as possible  – 

 –  Strives for balanced mix of jobs with large I/O interaction and jobs with lots of Strives for balanced mix of jobs with large I/O interaction and jobs with lots of  computation

computation  – 

 –   Tries to keep most system components busy most of  Tries to keep most system components busy most of timetime Process Scheduler

Process Scheduler (Low level scheduler):(Low level scheduler):  – 

 –  Determines which jobs will get the CPU, when, Determines which jobs will get the CPU, when, and for how longand for how long  – 

 –  Decides when processing should be interruptedDecides when processing should be interrupted  – 

 –  Determines which queues the job should be moved to Determines which queues the job should be moved to during its executionduring its execution  – 

 –  Recognizes when a job has concluded and should be terminatedRecognizes when a job has concluded and should be terminated

• I/O-bound jobsI/O-bound jobs have many brief CPU cycles and long I/O cycles, e.g., printing ahave many brief CPU cycles and long I/O cycles, e.g., printing a series of documents

series of documents •

• CPU-bound jobsCPU-bound jobs have long CPU cycles and shorter I/O cycles, e.g., finding the firsthave long CPU cycles and shorter I/O cycles, e.g., finding the first 300 prime numbers

300 prime numbers •

•  Total effect of all  Total effect of all CPU cycles, from both I/O-bound and CPU-bound jobs, approximatesCPU cycles, from both I/O-bound and CPU-bound jobs, approximates a

a Poisson distribution curvePoisson distribution curve

10

10

Figure 4.1: Distribution of CPU cycle

Figure 4.1: Distribution of CPU cycle

F

Figuigure re 4.24.2: : A A typtypicaicall

j

joob b ((oor r pprroocceessss))

ch

changanges es stastatus tus as as itit

mo

moveves s ththrrouough gh ththee

system from HOLD to

system from HOLD to

FINISHED

(10)

• Middle level schedulerMiddle level scheduler (third layer): Used in a (third layer): Used in a highly interactive environmenthighly interactive environment

 – 

 –  Removes active jobs from Removes active jobs from memory to reduce degree memory to reduce degree of multiprogrammof multiprogramminging  – 

 –  Allows jobs to be completed fasterAllows jobs to be completed faster

 Job and Process Status  Job and Process Status •

•  Job status: Job status: A job takes one of the following states as it moves through the systemA job takes one of the following states as it moves through the system

 – 

 –  HOLD, READY, WAITING, RUNNING, FINISHEDHOLD, READY, WAITING, RUNNING, FINISHED

• TransitionTransition from one status to another is from one status to another is initiated by either the Job Schedulerinitiated by either the Job Scheduler (JS)(JS) oror the Process Scheduler

the Process Scheduler (PS)(PS)::  – 

 –  HOLD to READY: JS, HOLD to READY: JS, using a predefined policyusing a predefined policy  – 

 –  READY to RUNNING: PS, using some predefined algorithmREADY to RUNNING: PS, using some predefined algorithm  – 

 –  RUNNING back to READY: PS, RUNNING back to READY: PS, according to some predefined time limit or otheraccording to some predefined time limit or other criterion

criterion  – 

 –  RUNNING to WAITING: PS, and is initiated by an instruction in the jobRUNNING to WAITING: PS, and is initiated by an instruction in the job  – 

 –  WAITING to READY: PS, and WAITING to READY: PS, and is initiated by signal from I/O is initiated by signal from I/O device manager thatdevice manager that I/O request has been satisfied and job

I/O request has been satisfied and job can continue.can continue.  – 

 –  RUNNING to FINISHED: PS or RUNNING to FINISHED: PS or JS, if job is JS, if job is finished or error has occurredfinished or error has occurred Process Control Blocks

Process Control Blocks •

• Process Control Block (PCB):Process Control Block (PCB): Data structure that contains basic info about the jobData structure that contains basic info about the job including

including  – 

 –  What it isWhat it is  – 

 –  Where it’s goingWhere it’s going  – 

 –  How much of its How much of its processing has been completedprocessing has been completed  – 

 –  Where it’s storedWhere it’s stored  – 

 –  How much it has spent in using resourcesHow much it has spent in using resources

• Contents of Process Control Block (PCB):Contents of Process Control Block (PCB):

 – 

 –  Process identificationProcess identification  – 

 –  Process statusProcess status (HOLD, READY, RUNNING,(HOLD, READY, RUNNING, WAITING)

WAITING)  – 

 –  Process stateProcess state (process status word, register(process status word, register contents, main memory info, resources, process contents, main memory info, resources, process priority)

priority)  – 

 –  AccountingAccounting (CPU time, total time, memory(CPU time, total time, memory occupancy, I/O operations, number of

occupancy, I/O operations, number of input recordsinput records read, etc.)

read, etc.)

PCBs and Queuing PCBs and Queuing •

• PCB of a job:PCB of a job: Contains all of the data about the jobContains all of the data about the job needed by the operating system to manage the

needed by the operating system to manage the processing of the job

processing of the job  – 

 –  Created when job scheduler accepts the jobCreated when job scheduler accepts the job  – 

 –  Updated as job goes from beginning to end Updated as job goes from beginning to end of of  its execution

its execution •

• QueuesQueues use PCBs to track jobsuse PCBs to track jobs

 – 

 – PCBs, not jobs, are linked PCBs, not jobs, are linked to form queuesto form queues – Queues must be managed by

– Queues must be managed by process schedulinprocess scheduling policiesg policies and algorithms

and algorithms

Process Scheduling Policies Process Scheduling Policies Figure 4.3: Contents of 

Figure 4.3: Contents of 

each job’s Process Control

each job’s Process Control

Block

(11)

• Operating system must resolve three Operating system must resolve three limitations of limitations of  a system before scheduling all jobs in a

a system before scheduling all jobs in a multi- multi-programming environment:

programming environment:  – 

 – Finite number of resources (e.g., Finite number of resources (e.g., disk drives,disk drives, printers, and tape drives)

printers, and tape drives)  – 

 – Some resources can’t be shared once they’reSome resources can’t be shared once they’re allocated (e.g., printers)

allocated (e.g., printers)  – 

 – Some resources require operator intervention (e.g.,Some resources require operator intervention (e.g., tape drives)

tape drives)

A

A good process scheduling policygood process scheduling policy should:should: •

• Maximize throughputMaximize throughput by running as many jobs as by running as many jobs as possible in a given amount of possible in a given amount of  time

time  – 

 –  Minimize response timeMinimize response time by quickly turning around by quickly turning around interactive requestsinteractive requests  – 

 –  Minimize turnaround timeMinimize turnaround time by moving entire jobs in and out of systemby moving entire jobs in and out of system quickly

quickly  – 

 –  Minimize waiting timeMinimize waiting time by moving jobs out of READY queue as quickly asby moving jobs out of READY queue as quickly as possible

possible  – 

 –  Maximize CPU efficiencyMaximize CPU efficiency by keeping CPU busy 100 by keeping CPU busy 100 percent of timepercent of time  – 

 –  Ensure fairnessEnsure fairness for all jobs by giving every one an equal amount of CPU andfor all jobs by giving every one an equal amount of CPU and I/O time

I/O time •

• Need for Interrupts:Need for Interrupts: When a job claims CPU When a job claims CPU for a very long for a very long time before issuing antime before issuing an I/O request

I/O request  – 

 –  Builds up READY queue & empties I/O Builds up READY queue & empties I/O queuesqueues  – 

 –  Creates an unacceptable imbalance in the systemCreates an unacceptable imbalance in the system

• Process Scheduler uses interrupts when a predetermined slice of Process Scheduler uses interrupts when a predetermined slice of time has expiredtime has expired

 – 

 –  Suspends all activity on the currently running jobSuspends all activity on the currently running job  – 

 –  Reschedules it into the READY queueReschedules it into the READY queue Types of Scheduling Policies:

Types of Scheduling Policies:

o

o Preemptive scheduling policy:Preemptive scheduling policy:

 Interrupts procesInterrupts processing of a sing of a job and transfers the CPU to job and transfers the CPU to another jobanother job

o

o Nonpreemptive scheduling policy:Nonpreemptive scheduling policy:

 Functions without external Functions without external interruptsinterrupts

Process Scheduling Algorithms Process Scheduling Algorithms Types of Process Scheduling Algorithms:

Types of Process Scheduling Algorithms: •

• First Come, First Served (FCFS)First Come, First Served (FCFS) •

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

• Priority SchedulingPriority Scheduling •

• Shortest Remaining Time (SRT)Shortest Remaining Time (SRT) •

• Round RobinRound Robin •

• Multiple Level QueuesMultiple Level Queues First-Come,

First-Come, First-ServeFirst-Servedd •

• NonpreemptiveNonpreemptive •

• Handles jobs according to their arrival time: the earlier they Handles jobs according to their arrival time: the earlier they arrive, the soonerarrive, the sooner they’re served

they’re served •

• Simple algorithm to implement: uses a FIFO queueSimple algorithm to implement: uses a FIFO queue •

• Good for batcGood for batch systems; unach systems; unacceptable for ceptable for interactive syinteractive systemsstems •

•  Turnaround time is unpredictable Turnaround time is unpredictable  Jobs arrival sequence: A, B, C

 Jobs arrival sequence: A, B, C •

•  Job A has a CPU cycle of 15  Job A has a CPU cycle of 15 millisecondsmilliseconds

Figure 4.4: Queuing paths from HOLD to

Figure 4.4: Queuing paths from HOLD to

FINISHED

FINISHED

F

Figure 4.5: Timeline for job sequence A, igure 4.5: Timeline for job sequence A, B, C B, C using theusing the

FCFS algorithm

FCFS algorithm

Average turnaround time:

(12)

Arrival

Arrival

time:

time:

0

0

1

1

2

2

3

3

 

Job:

 

Job:

A

A

B

B

C

C

D

D

CPU

CPU

cycle:

cycle:

6

6

3

3

1

1

4

4

 

Job:

 

Job:

A

A

B

B

C

C

D

D

  T

  T

urnaround:

urnaround:

14

14

4

4

1

1

•  Job B has a CPU cycle of 2  Job B has a CPU cycle of 2 milliseconmillisecondsds

•  Job C has a CPU cycle of  Job C has a CPU cycle of 11

millisecond millisecond

 Jobs arrival sequence: C, B, A  Jobs arrival sequence: C, B, A

•  Job A has a CPU cycle of 15 Job A has a CPU cycle of 15 milliseconds

milliseconds •

•  Job B has a CPU cycle of 2  Job B has a CPU cycle of 2 milliseconmillisecondsds •

•  Job C has a CPU cycle of  Job C has a CPU cycle of 1 millisecond1 millisecond Shortest Job Next (SJN)

Shortest Job Next (SJN) •

• NonpreemptiveNonpreemptive •

• Handles jobs based on length of their Handles jobs based on length of their CPU cycle timeCPU cycle time •

• Easiest to implement in Easiest to implement in batch environmentsbatch environments •

• Doesn’t work in Doesn’t work in interactive systemsinteractive systems •

• Optimal only when all jobs are available at same time and the CPU estimates areOptimal only when all jobs are available at same time and the CPU estimates are available and accurate

available and accurate

Four batch jobs A, B, C, D, all in the READY queue Four batch jobs A, B, C, D, all in the READY queue   Job:   Job: A A B B C C DD CPU cycle: CPU cycle: 5 5 2 2 6 6 44 Priority Scheduling Priority Scheduling • • NonpreemptiveNonpreemptive •

• Gives preferential treatment to important jobsGives preferential treatment to important jobs

o

o Programs with highest priority are processed firstPrograms with highest priority are processed first o

o Not interrupted until CPU cycles are completed or a Not interrupted until CPU cycles are completed or a natural wait occursnatural wait occurs

• FCFS policy is used if FCFS policy is used if two or more jobs with equal two or more jobs with equal priority in READY queuepriority in READY queue •

• SystSystem em admiadministrnistrator ator or or ProcProcessor Manager essor Manager use use diffdifferent methods erent methods of of assiassigningningg priorities

priorities

Shortest Remaining Time Shortest Remaining Time •

• Preemptive version of the SJN algorithmPreemptive version of the SJN algorithm •

• Processor allocated to job closest Processor allocated to job closest to completionto completion

o

o Current job can be preempted if newer Current job can be preempted if newer job in READY queue has shorter time tojob in READY queue has shorter time to

completion completion •

• Cannot be implemented in interactive Cannot be implemented in interactive systemsystem

o

o Requires advance knowledge of the CPU time required to Requires advance knowledge of the CPU time required to finish each jobfinish each job

• SRT involves more overhead than SJNSRT involves more overhead than SJN

o

o OS monitors CPU time for all OS monitors CPU time for all jobs in READY queue and performs contextjobs in READY queue and performs context

switching switching

F

Figure 4.6: Timeline for job sequence C, igure 4.6: Timeline for job sequence C, B, A B, A using theusing the

FCFS algorithm

FCFS algorithm

Average turnaround time:

Average turnaround time:

7.3 s

7.3 s

Figure 4.7: Ti

Figure 4.7: Timeline for job sequence B, D, A, meline for job sequence B, D, A, Average turnaround time:Average turnaround time:C using the SJNC using the SJN

Figure 4.8: Timeline for job sequence A, B, C, D using the preemptive

Figure 4.8: Timeline for job sequence A, B, C, D using the preemptive

SRT algorithm

SRT algorithm

Arrival

Arrival

time:

time:

0

0

1

1

2

2

3

3

 

Job:

 

Job:

A

A

B

B

C

C

D

D

CPU

CPU

cycle:

cycle:

6

6

3

3

1

(13)

Round Robin Round Robin

• PreemptivePreemptive •

• Used extensively in Used extensively in interactive systemsinteractive systems •

• Based on a predetermined slice of time Based on a predetermined slice of time (time quantum) that’s given to each job(time quantum) that’s given to each job •

• Size of time quantum crucial to system performanceSize of time quantum crucial to system performance

o

o Usually varies from 100 ms to 1-2 sUsually varies from 100 ms to 1-2 s

• Ensures CPU is equally shared among all active processes and is Ensures CPU is equally shared among all active processes and is not monopolized bynot monopolized by any one job

any one job

• If Job’s CPU cycle > If Job’s CPU cycle > time quantumtime quantum

o

o  Job is preempted and put at the end of the READY queue and Job is preempted and put at the end of the READY queue and

its information is saved in its PCB its information is saved in its PCB •

• If Job’s CPU cycle < If Job’s CPU cycle < time quantumtime quantum

o

o If job is finished, all resources allocated to it are released & completed job isIf job is finished, all resources allocated to it are released & completed job is

returned to user returned to user

o

o If interrupted by I/O request, then info is saved in PCB & it is linked at end of If interrupted by I/O request, then info is saved in PCB & it is linked at end of 

the appropriate I/O queue the appropriate I/O queue

o

o OncOnce e I/O I/O reqrequesuest t is is sasatistisfiefied, d, job returjob returns ns to to end of end of REREADY queuADY queue e to to awawaitait

allocation of CPU allocation of CPU

• EfficiencyEfficiency depends on the size of depends on the size of time quantum in relation to the average time quantum in relation to the average CPU cycleCPU cycle •

• If the quantum is too largeIf the quantum is too large - larger than most CPU - larger than most CPU cyclescycles

 – 

 –  Algorithm reduces to the FCFS Algorithm reduces to the FCFS schemescheme

• If the quantum is too smallIf the quantum is too small

 – 

 –  Amount of context switching slows down the execution of the Amount of context switching slows down the execution of the jobsjobs  – 

 –  Amount of overhead is Amount of overhead is dramaticaldramatically increasedly increased

• General rules of thumb for selecting the proper time quantum:General rules of thumb for selecting the proper time quantum:

 – 

 –  Should be long enough to allow 80% Should be long enough to allow 80% of CPU cycles to of CPU cycles to run to completionrun to completion  – 

 –  Should be at least 100 Should be at least 100 times longer than the time required to times longer than the time required to perform one contextperform one context switch

switch

Figure 4.9: Timeline for job sequence A, B, C, D using the

Figure 4.9: Timeline for job sequence A, B, C, D using the

nonpreemptive nonpreemptive SJN algorithm SJN algorithm

 

Job:

 

Job:

A

A

B

B

C

C

D

D

  Turnaround: 6 9 5

  Turnaround: 6 9 5

Figure 4.10: Timeline for job sequence A, B, C, D using

Figure 4.10: Timeline for job sequence A, B, C, D using

the preemptive round robin algorithm

the preemptive round robin algorithm

Arrival

Arrival

time: 0 1

time: 0 1

2

2

3

3

 

Job:

 

Job:

A

A

B

B

C

C

D

D

CPU

CPU

cycle:

cycle:

8

8

4

4

9

9

5

5

 

Job:

 

Job:

A

A

B

B

C

C

D

D

  T

  T

urnaround: 20

urnaround: 20

7

7

24

24

 Time slice: 4ms  Time slice: 4ms F

Figurigure e 4.114.11: : ConteContext xt switswitches for ches for job Ajob A

with three differe

with three different nt time quantumstime quantums. . InIn

(a) the

(a) the job finisjob finishes beforhes before e the the timtimee

quantum expires

quantum expires. In (b) . In (b) and (c), the timeand (c), the time

quantum expires first, interrupting the

quantum expires first, interrupting the

 job.

 job.

CPU cycle of job A: 8 ms CPU cycle of job A: 8 ms

(14)

•  These rules are flexible and depend on  These rules are flexible and depend on the systemthe system Multiple-Level Queues

Multiple-Level Queues •

• Work in conjunction with several Work in conjunction with several other schemesother schemes •

• Found in systems with jobs that can Found in systems with jobs that can be grouped according to a common characteristicbe grouped according to a common characteristic •

• Examples:Examples:

 – 

 –  Priority-based system with different queues for ePriority-based system with different queues for each priority levelach priority level  – 

 –  System with all CPU-bound jobs in one System with all CPU-bound jobs in one queue and all I/O-bound jobs in queue and all I/O-bound jobs in anotheranother  – 

 –  Hybrid system with batch jobs in background queue and interactive jobs in Hybrid system with batch jobs in background queue and interactive jobs in aa foreground queue

foreground queue •

• Four primary methods to the movement of jobs:Four primary methods to the movement of jobs:

 – 

 –  No Movement Between QueuesNo Movement Between Queues  – 

 –  Movement Between QueuesMovement Between Queues  – 

 –  Variable Time Quantum Per QueueVariable Time Quantum Per Queue  – 

 –  AgingAging

• No Movement Between Queues:No Movement Between Queues:

 – 

 –   The processor is allocated to the jobs in  The processor is allocated to the jobs in the high-priority queue in FCFS fashionthe high-priority queue in FCFS fashion  – 

 –  Allocated to jobs in lower priority queues only when Allocated to jobs in lower priority queues only when the high priority queuesthe high priority queues are empty

are empty •

• Movement Between QueuesMovement Between Queues

 – 

 –  Adjusts the priorities assigned to each jobAdjusts the priorities assigned to each job  – 

 –  A job may A job may also have its priority increasedalso have its priority increased  – 

 –  Good in interactive systemsGood in interactive systems

• Variable Time Quantum Per Queue:Variable Time Quantum Per Queue:

 – 

 –  Each of the queues is Each of the queues is given a time quantum twice as long given a time quantum twice as long as the previous queueas the previous queue  – 

 –  CPU-bound job can execute for longer and CPU-bound job can execute for longer and longer periods of time, thus improving itslonger periods of time, thus improving its chances of finishing faster

chances of finishing faster •

• Aging:Aging:

 – 

 –  System moves the old job to the next highest queue, and so on until it reachesSystem moves the old job to the next highest queue, and so on until it reaches the top queue

the top queue  – 

 –  Ensures that jobs in the lower-level queues will eventually complete theirEnsures that jobs in the lower-level queues will eventually complete their execution

execution A Word About

A Word About InterruptsInterrupts •

• Types of Interrupts:Types of Interrupts:  – 

 –  Page interrupts to accommodate job Page interrupts to accommodate job requestsrequests  – 

 –   Time quantum expiration interrupts Time quantum expiration interrupts  – 

 –  I/O interrupts when READ or WRITE command is issuedI/O interrupts when READ or WRITE command is issued  – 

 –  Internal interrupts (synchronous interrupts) result from arithmetic Internal interrupts (synchronous interrupts) result from arithmetic operation oroperation or  job instruction

 job instruction  – 

 –  Illegal arithmetic operations (e.g., dividing by Illegal arithmetic operations (e.g., dividing by 0).0).  – 

 –  Illegal job instructions (e.g., attempts to Illegal job instructions (e.g., attempts to access protected storage locations)access protected storage locations) •

• Interrupt handler:Interrupt handler: Control program that handles the Control program that handles the interruption sequence of interruption sequence of  events

events •

• When operating system detects a When operating system detects a nonrecovernonrecoverable error, the able error, the interrupt handler followsinterrupt handler follows this sequence:

this sequence:  – 

 –   The type of interrupt is described and stored The type of interrupt is described and stored  – 

 –   The state of the interrupted proces The state of the interrupted process is saveds is saved  – 

 –   The interrupt is processed The interrupt is processed  – 

 –   The processor resumes normal operation The processor resumes normal operation Summary

(15)

• Process scheduler assigns the CPU to execute processes of those jobs placed onProcess scheduler assigns the CPU to execute processes of those jobs placed on READY queue by Job Scheduler

READY queue by Job Scheduler •

•  Total effect of all  Total effect of all CPU cycles, from both I/O-bound and CPU-bound jobs, approximatesCPU cycles, from both I/O-bound and CPU-bound jobs, approximates a Poisson distribution curve

a Poisson distribution curve •

•  Transition from one status to another is initiated by either Job Scheduler (JS) or Transition from one status to another is initiated by either Job Scheduler (JS) or Process Scheduler (PS)

Process Scheduler (PS) •

• PCB of a job contains all data about the job needed by OS to manage the processingPCB of a job contains all data about the job needed by OS to manage the processing of the job

of the job •

• A good process sA good process scheduling pocheduling policy should licy should maximize CPU efmaximize CPU efficiency by keeping CPficiency by keeping CPUU busy 100 percent of time

busy 100 percent of time •

• FIFO has simple algorithm to implement but turnaround time is unpredictableFIFO has simple algorithm to implement but turnaround time is unpredictable •

• SJN minimizes average waiting time but results in infinite postponement of some jobsSJN minimizes average waiting time but results in infinite postponement of some jobs •

• Priority scheduling ensures fast completion of important jobs but results in infinitePriority scheduling ensures fast completion of important jobs but results in infinite postponement of some jobs

postponement of some jobs •

• SRT ensures fast completion of short jobs but involves more overhead than SJN,SRT ensures fast completion of short jobs but involves more overhead than SJN, incurred by context switching

incurred by context switching •

• Efficiency in round robin policy depends on the size of time quantum in relation toEfficiency in round robin policy depends on the size of time quantum in relation to the average CPU cycle

the average CPU cycle •

• Multiple-level queues counteract indefinite postponement with aging or other queueMultiple-level queues counteract indefinite postponement with aging or other queue movement

References

Related documents

out-of-the-box components, provides you with the unique opportunity to launch your project quickly and ensure a high standard of quality and reliability.. across

The district cannot hire new staff, as it has reached the maximum number of permanent extension staff allowed by Government. It is only possible to add staff on temporary hired

This section contains the technical bulk of this article, as we collect and derive here the definitions and results about generalized test ideals we need to prove our main result

To this diluted cheese or fish sample 50 µL reaction solution are pipetted, mixed thoroughly and incubated for 20 minutes at room temperature in the dark.. 200 µL neutralizing

En total, el primer conjunto de representaciones que resalta es el que ubica a los manifestantes y a la protesta en lugares negativos: cerca del 45% del total de

The highest challenge reported by students is lack of time; this is consistent with other works (Lathey and Hodge 2001). Location of the library and poor attitude

restored/reconstructed prairies. These results suggest that purposefully created habitat in urban areas can support an important number of individual butterflies and butterfly

Also, the spectra in this phase can be used to map the density profile of the SN ejecta in the line-of-sight and so early data provide the best estimate of the kinetic energy because