types of memory allocation schemes

17
Memory Management: Early Systems Single-User Configurations → Fixed Partitions → Dynamic Partitions → Relocatable Dynamic Partitions “Memory is the primary and fundamental power, without which there could be no other intellectual operation.” —Samuel Johnson (1709–1784) Types of memory allocation schemes: Single-user systems Fixed partitions Dynamic partitions Relocatable dynamic partitions 1. Single-User Contiguous Scheme: Program is loaded in its entirety into memory and allocated as much contiguous space in memory as it needs. Jobs processed sequentially in single-user systems Requires minimal work by the Memory Manager • Register to store the base address • Accumulator to keep track of the program size Disadvantages of Single-User Contiguous Scheme: Doesn’t support multiprogramming Not cost effective 2. Fixed Partitions: Main memory is partitioned; one partition/job. Allows multiprogramming Partition sizes remain static unless and until computer system id shut down, reconfigured, and restarted Requires protection of the job’s memory space Requires matching job size with partition size To allocate memory spaces to jobs, the operating system’s Memory Manager must keep a table as shown below: Table 2.1: A simplified fixed partition memory table with the free partition shaded Figure 2.1: Main memory use during fixed partition allocation of Table 2.1 NOTE: Job 3 must wait even though 70K of free space is available in Partition 1 where Job 1 occupies only 30K of the 100K available

Upload: michael-john-sabido

Post on 28-Nov-2014

6.703 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Types of Memory Allocation Schemes

Memory Management: Early Systems

Single-User Configurations → Fixed Partitions → Dynamic Partitions → Relocatable Dynamic Partitions

“Memory is the primary and fundamental power, without which there could be no other intellectual operation.” —Samuel Johnson (1709–1784)

Types of memory allocation schemes: Single-user systems Fixed partitions Dynamic partitions Relocatable dynamic partitions

1. Single-User Contiguous Scheme: Program is loaded in its entirety into memory and allocated as much contiguous space in memory as it needs. Jobs processed sequentially in single-user systems Requires minimal work by the Memory Manager

• Register to store the base address • Accumulator to keep track of the program size

Disadvantages of Single-User Contiguous Scheme: Doesn’t support multiprogramming Not cost effective

2. Fixed Partitions: Main memory is partitioned; one partition/job. Allows multiprogramming Partition sizes remain static unless and until computer system id shut down, reconfigured, and

restarted Requires protection of the job’s memory space Requires matching job size with partition size

To allocate memory spaces to jobs, the operating system’s Memory Manager must keep a table as shown below:

Disadvantages: Requires entire program to be stored contiguously Jobs are allocated space on the basis of first available partition of required size Works well only if all of the jobs are of the same size or if the sizes are known ahead of time

Table 2.1: A simplified fixed partition memory table with the free partition shaded

Figure 2.1: Main memory use during fixed partition allocation of Table 2.1

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

available in Partition 1 where Job 1 occupies only 30K of the 100K available

Page 2: Types of Memory Allocation Schemes

Arbitrary partition sizes lead to undesired results Too small a partition size results in large jobs having longer turnaround time Too large a partition size results in memory waste or internal fragmentation

3. Dynamic Partitions: Jobs are given only as much memory as they request when they are loaded Available memory is kept in contiguous blocks Memory waste is comparatively small

Disadvantages: Fully utilizes memory only when the first jobs are loaded Subsequent allocation leads to memory waste or external fragmentation

Best-Fit versus First-Fit Allocation

Free partitions are allocated on the following basis:First-fit memory allocation: First partition fitting the

requirements Leads to fast allocation of memory spaceAdvantage: Faster in making allocation

Disadvantage: Leads to memory waste

Figure 2.2: Main memory use during dynamic partition allocation

Page 3: Types of Memory Allocation Schemes

Best-fit memory allocation: Smallest partition fitting the requirements Results in least wasted space Internal fragmentation reduced but not eliminateAdvantage: Makes the best use of memory spaceDisadvantage: Slower in making allocation

Algorithm for First-Fit:

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

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

Job is stored into that block of memory Memory Manager moves out of

the loop to fetch the next job from the entry queue

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

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

Algorithm for Best-Fit:– Goal: find the smallest memory block into which the job will fit– Entire table must be searched before allocation

Hypothetical allocation schemes:– Next-fit: Starts searching from last allocated block, for the next available block when a new job arrives– Worst-fit: Allocates the largest free available block to the new job• Opposite of best-fit• Good way to explore the theory of memory allocation; might not be the best choice for an actual system

Figure 2.3: An example of a first-fit free scheme

Table 2.2: Status of each memory block before and after a request is made for a block of 200 spaces using the first-fit algorithm

Figure 2.3: An example of a best-fit free scheme

Page 4: Types of Memory Allocation Schemes

Deallocation: Freeing an allocated memory space For fixed-partition system:

Straightforward process When job completes, Memory Manager resets the status of the job’s memory block to “free” Any code—for example, binary values with 0 indicating free and 1 indicating busy—may be used

For dynamic-partition system: Algorithm tries to combine free areas of memory whenever possible Three cases:

Case 1: When the block to be deallocated is adjacent to another free blockCase 2: When the block to be deallocated is between two free blocksCase 3: When the block to be deallocated is isolated from other free blocks

Case 1: Joining Two Free Blocks Change list must reflect starting address of the new free block

In the example, 7600—which was the address of the first instruction of the job that just released this block

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

In the example, (200 + 5)

Table 2.3: Status of each memory block before and after a request is made for a memory block of 200 spaces using the best-fit algorithm

(LEFT) Table 2.4: Original free list before deallocation for Case 1

(TOP) Table 2.5: Free list after deallocation for Case 1

Page 5: Types of Memory Allocation Schemes

Case 2: Joining Three Free BlocksDeallocated memory space is between two free memory blocks

Change list to reflect the starting address of the new free block In the example, 7560— which was the smallest beginning address

Sizes of the three free partitions must be combined In the example, (20 + 20 + 205)

Combined entry is given the status of null entry In the example, 7600

Case 3: Deallocating an Isolated BlockSpace to be deallocated is isolated from other free areas

System learns that the memory block to be released is not adjacent to any free blocks of memory, it is between two other busy areas

Must search the table for a null entry Null entry in the busy list occurs when a memory block between two other busy memory blocks is

returned to the free list

Table 2.6: Original free list before deallocation for Case 2

Table 2.7: Free list after job has released memory

Table 2.8: Original free list before deallocation for Case 3

The job to be deallocated is of size 445 and begins at location 8805. The asterisk indicates the soon-to-be-free memory block.

Table 2.9: Memory list before deallocation

Table 2.10: Busy list after the job has released its memory. The asterisk indicates the new null entry in the busy list.

Page 6: Types of Memory Allocation Schemes

4. Relocatable Dynamic Partitions: Memory Manager relocates programs to gather

together all of the empty blocks Compact the empty blocks to make one block of

memory large enough to accommodate some or all of the jobs waiting to get in

Compaction: Reclaiming fragmented sections of the memory space

Every program in memory must be relocated so they are contiguous

Operating system must distinguish between addresses and data values • Every address must be adjusted to account for

the program’s new location in memory• Data values must be left alone

Compaction issues: What goes on behind the scenes when relocation and compaction take place? What keeps track of how far each job has moved from its original storage area? What lists have to be updated?

What lists have to be updated? Free list must show the partition for the new block of free memory Busy list must show the new locations for all of the jobs already in process that were relocated Each job will have a new address except for those that were already at the lowest memory locations

Special-purpose registers are used for relocation: Bounds register

• Stores highest location accessible by each program Relocation register

• Contains the value that must be added to each address referenced in the program so it will be able to access the correct memory addresses after relocation

• If the program isn’t relocated, the value stored in the program’s relocation register is zero

Table 2.11: Free list after the job has released its memory. The asterisk indicates the new free block entry replacing the null entry

Figure 2.7: Three snapshots of memory before and after compaction

Page 7: Types of Memory Allocation Schemes

Compacting and relocating optimizes the use of memory and thus improves throughput Options for when and how often it should be done:

• When a certain percentage of memory is busy• When there are jobs waiting to get in• After a prescribed amount of time has elapsed

Goal: Optimize processing time and memory use while keeping overhead as low as possible

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

dynamic partitions, and relocatable dynamic partitions Memory waste in dynamic partitions is comparatively small as compared to fixed partitions First-fit is faster in making allocation but leads to memory waste Best-fit makes the best use of memory space but slower in making allocation Compacting and relocating optimizes the use of memory and thus improves throughput All techniques require that the entire program must:

a) Be loaded into memoryb) Be stored contiguouslyc) Remain in memory until the job is completed

Each technique puts severe restrictions on the size of the jobs: can only be as large as the largest partitions in memory

Figure 2.8: Contents of relocation register and close-up of Job 4 memory area (a) before relocation and (b) after relocation and compaction

Page 8: Types of Memory Allocation Schemes

Processor Management Program (Job):

– A unit of work that has been submitted by user to an operating system– An inactive unit, such as a file stored on a disk

Process (Task):– An active entity, which requires a set of resources, including a processor and special registers, to

perform its function– A single instance of an executable program

Processor (CPU): performs calculations and executes programs– In single-user systems:

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

– In a multiprogramming environment:• Processor must be allocated to each job in a fair and efficient manner• Requires scheduling policy and a scheduling algorithm

Interrupt: A hardware signal that suspends execution of a program and activates the execution of interrupt handler

Context Switch: Saving a job’s processing information in its PCB when interrupted• Context switching occurs in all preemptive

Job Scheduling Versus Process Scheduling Processor Manager has two sub managers:

– Job Scheduler: • In charge of job scheduling• Initiates the job based on certain criteria

– Process Scheduler: • In charge of process scheduling• Assigns the CPU to execute processes of those jobs placed on READY queue by Job

SchedulerJob Scheduler (High level scheduler):

– Initiates the job based on certain criteria– Puts 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 computation– Tries to keep most system components busy most of time

Process Scheduler (Low level scheduler):– Determines which jobs will get the CPU, when, and for how long– Decides when processing should be interrupted– Determines which queues the job should be moved to during its execution– Recognizes when a job has concluded and should be terminated I/O-bound jobs have many brief CPU cycles and long I/O cycles, e.g., printing a series of documents CPU-bound jobs have long CPU cycles and shorter I/O cycles, e.g., finding the first 300 prime numbers Total effect of all CPU cycles, from both I/O-bound and CPU-bound jobs, approximates a Poisson

distribution curve

10

Figure 4.1: Distribution of CPU cycle times

Figure 4.2: A typical job (or process) changes status as it moves through the system from HOLD to FINISHED

Page 9: Types of Memory Allocation Schemes

Middle level scheduler (third layer): Used in a highly interactive environment – Removes active jobs from memory to reduce degree of multiprogramming – Allows jobs to be completed faster

Job and Process Status Job status: A job takes one of the following states as it moves through the system

– HOLD, READY, WAITING, RUNNING, FINISHED Transition from one status to another is initiated by either the Job Scheduler (JS) or the Process Scheduler

(PS):– HOLD to READY: JS, using a predefined policy– READY to RUNNING: PS, using some predefined algorithm– RUNNING back to READY: PS, according to some predefined time limit or other criterion– RUNNING to WAITING: PS, and is initiated by an instruction in the job– WAITING to READY: PS, and is initiated by signal from I/O device manager that I/O request has

been satisfied and job can continue.– RUNNING to FINISHED: PS or JS, if job is finished or error has occurred

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

– What it is– Where it’s going– How much of its processing has been completed– Where it’s stored– How much it has spent in using resources

Contents of Process Control Block (PCB):– Process identification– Process status (HOLD, READY, RUNNING, WAITING)– Process state (process status word, register contents, main

memory info, resources, process priority)– Accounting (CPU time, total time, memory occupancy, I/O

operations, number of input records read, etc.)PCBs and Queuing

PCB of a job: Contains all of the data about the job needed by the operating system to manage the processing of the job– Created when job scheduler accepts the job– Updated as job goes from beginning to end of its execution

Queues use PCBs to track jobs– PCBs, not jobs, are linked to form queues– Queues must be managed by process scheduling policies and algorithms

Process Scheduling Policies Operating system must resolve three limitations of a system

before scheduling all jobs in a multi-programming environment:– Finite number of resources (e.g., disk drives, printers, and tape

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

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

Figure 4.3: Contents of each job’s Process Control Block

Figure 4.4: Queuing paths from HOLD to FINISHED

Page 10: Types of Memory Allocation Schemes

A good process scheduling policy should: Maximize throughput by running as many jobs as possible in a given amount of time

– Minimize response time by quickly turning around interactive requests– Minimize turnaround time by moving entire jobs in and out of system quickly– Minimize waiting time by moving jobs out of READY queue as quickly as possible– Maximize CPU efficiency by keeping CPU busy 100 percent of time– Ensure fairness for all jobs by giving every one an equal amount of CPU and I/O time

Need for Interrupts: When a job claims CPU for a very long time before issuing an I/O request– Builds up READY queue & empties I/O queues– Creates an unacceptable imbalance in the system

Process Scheduler uses interrupts when a predetermined slice of time has expired– Suspends all activity on the currently running job– Reschedules it into the READY queue

Types of Scheduling Policies:o Preemptive scheduling policy:

Interrupts processing of a job and transfers the CPU to another jobo Nonpreemptive scheduling policy:

Functions without external interruptsProcess Scheduling Algorithms

Types of Process Scheduling Algorithms: First Come, First Served (FCFS) Shortest Job Next (SJN) Priority Scheduling Shortest Remaining Time (SRT) Round Robin Multiple Level Queues

First-Come, First-Served Nonpreemptive Handles jobs according to their arrival time: the earlier they arrive, the sooner they’re served Simple algorithm to implement: uses a FIFO queue Good for batch systems; unacceptable for interactive systems Turnaround time is unpredictable

Jobs arrival sequence: A, B, C Job A has a CPU cycle of 15 milliseconds Job B has a CPU cycle of 2 milliseconds Job C has a CPU cycle of 1 millisecond

Jobs arrival sequence: C, B, A Job A has a CPU cycle of 15 milliseconds Job B has a CPU cycle of 2 milliseconds Job C has a CPU cycle of 1 millisecond

Shortest Job Next (SJN) Nonpreemptive Handles jobs based on length of their CPU cycle time Easiest to implement in batch environments Doesn’t work in interactive systems Optimal only when all jobs are available at same time and the CPU estimates are available and accurate

Four batch jobs A, B, C, D, all in the READY queue Job: A B C DCPU cycle: 5 2 6 4

Priority Scheduling

Figure 4.5: Timeline for job sequence A, B, C using the FCFS algorithm

Average turnaround time: 16.67 s

Figure 4.6: Timeline for job sequence C, B, A using the FCFS algorithm

Average turnaround time: 7.3 s

Figure 4.7: Timeline for job sequence B, D, A, C using the SJN algorithmAverage turnaround time: 9 s

Page 11: Types of Memory Allocation Schemes

Arrival time: 0 1 2 3Job: A B C DCPU cycle: 6 3 1 4

Job: A B C DTurnaround: 14 4 1 6Average Turnaround: 6.25s

Nonpreemptive Gives preferential treatment to important jobs

o Programs with highest priority are processed firsto Not interrupted until CPU cycles are completed or a natural wait occurs

FCFS policy is used if two or more jobs with equal priority in READY queue System administrator or Processor Manager use different methods of assigning priorities

Shortest Remaining Time Preemptive version of the SJN algorithm Processor allocated to job closest to completion

o Current job can be preempted if newer job in READY queue has shorter time to completion Cannot be implemented in interactive system

o Requires advance knowledge of the CPU time required to finish each job SRT involves more overhead than SJN

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

Round Robin Preemptive Used extensively in interactive systems Based on a predetermined slice of time (time quantum) that’s given to each job Size of time quantum crucial to system performance

o Usually varies from 100 ms to 1-2 s Ensures CPU is equally shared among all active processes and is not monopolized by any one job

If Job’s CPU cycle > time quantumo Job is preempted and put at the end of the READY queue and

its information is saved in its PCB

If Job’s CPU cycle < time quantumo If job is finished, all resources allocated to it are released & completed job is returned to user

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

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

Arrival time: 0 1 2 3Job: A B C DCPU cycle: 6 3 1 4

Job: A B C DTurnaround: 6 9 5 11Average Turnaround: 7.75s

Figure 4.10: Timeline for job sequence A, B, C, D using the preemptive round robin algorithm

Arrival time: 0 1 2 3Job: A B C DCPU cycle: 8 4 9 5

Job: A B C DTurnaround: 20 7 24 22Average Turnaround: 18.25 s

Time slice: 4ms

Page 12: Types of Memory Allocation Schemes

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

o Once I/O request is satisfied, job returns to end of READY queue to await allocation of CPU

Efficiency depends on the size of time quantum in relation to the average CPU cycle If the quantum is too large - larger than most CPU cycles

– Algorithm reduces to the FCFS scheme If the quantum is too small

– Amount of context switching slows down the execution of the jobs– Amount of overhead is dramatically increased

General rules of thumb for selecting the proper time quantum:– Should be long enough to allow 80% of CPU cycles to run to completion– Should be at least 100 times longer than the time required to perform one context switch

These rules are flexible and depend on the system

Multiple-Level Queues Work in conjunction with several other schemes Found in systems with jobs that can be grouped according to a common characteristic Examples:

– Priority-based system with different queues for each priority level– System with all CPU-bound jobs in one queue and all I/O-bound jobs in another– Hybrid system with batch jobs in background queue and interactive jobs in a foreground queue

Four primary methods to the movement of jobs:– No Movement Between Queues– Movement Between Queues– Variable Time Quantum Per Queue– Aging

No Movement Between Queues:– The processor is allocated to the jobs in the high-priority queue in FCFS fashion – Allocated to jobs in lower priority queues only when the high priority queues are empty

Movement Between Queues– Adjusts the priorities assigned to each job– A job may also have its priority increased– Good in interactive systems

Variable Time Quantum Per Queue:– Each of the queues is given a time quantum twice as long as the previous queue

Figure 4.11: Context switches for job A with three different time quantums. In (a) the job finishes before the time quantum expires. In (b) and (c), the time quantum expires first, interrupting the job.

CPU cycle of job A: 8 ms

Page 13: Types of Memory Allocation Schemes

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

Aging:– System moves the old job to the next highest queue, and so on until it reaches the top queue– Ensures that jobs in the lower-level queues will eventually complete their execution

A Word About Interrupts• Types of Interrupts:

– Page interrupts to accommodate job requests– Time quantum expiration interrupts– I/O interrupts when READ or WRITE command is issued– Internal interrupts (synchronous interrupts) result from arithmetic operation or job instruction – Illegal arithmetic operations (e.g., dividing by 0).– Illegal job instructions (e.g., attempts to access protected storage locations)

• Interrupt handler: Control program that handles the interruption sequence of events• When operating system detects a nonrecoverable error, the interrupt handler follows this sequence:

– The type of interrupt is described and stored– The state of the interrupted process is saved– The interrupt is processed– The processor resumes normal operation

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

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

distribution curve Transition from one status to another is initiated by either Job Scheduler (JS) or Process Scheduler (PS) PCB of a job contains all data about the job needed by OS to manage the processing of the job A good process scheduling policy should maximize CPU efficiency by keeping CPU busy 100 percent of

time FIFO has simple algorithm to implement but turnaround time is unpredictable SJN minimizes average waiting time but results in infinite postponement of some jobs Priority scheduling ensures fast completion of important jobs but results in infinite postponement of

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

switching Efficiency in round robin policy depends on the size of time quantum in relation to the average CPU cycle Multiple-level queues counteract indefinite postponement with aging or other queue movement