memory management chapter 7. memory management it is the task carried out by the os with support...

41
Memory Management Chapter 7

Upload: samuel-kelly

Post on 02-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Memory Management Chapter 7. Memory Management It is the task carried out by the OS with support from hardware to accommodate multiple processes in main

Memory Management

Chapter 7

Page 2: Memory Management Chapter 7. Memory Management It is the task carried out by the OS with support from hardware to accommodate multiple processes in main

Memory Management

It is the task carried out by the OS with support from hardware to accommodate multiple processes in main memoryEffective memory management is vital in a multiprogramming systemEfficiently allocate memory to pack as many processes into memory as possible. This is essential for minimizing processor idle time due to I/O wait

Page 3: Memory Management Chapter 7. Memory Management It is the task carried out by the OS with support from hardware to accommodate multiple processes in main

Memory Management Requirements

Memory management policies must satisfy the following: Relocation Protection Sharing Logical Organization Physical Organization

Page 4: Memory Management Chapter 7. Memory Management It is the task carried out by the OS with support from hardware to accommodate multiple processes in main

RelocationAbility to relocate a process to a different area of memoryProgrammer cannot know where the program will be placed in memory when it is executedA process may be (often) relocated in main memory due to swappingSwapping enables the OS to have a larger pool of ready-to-execute processes Memory references in code (for both instructions and data) must be translated to actual physical memory address

Page 5: Memory Management Chapter 7. Memory Management It is the task carried out by the OS with support from hardware to accommodate multiple processes in main

Protection

Processes should not be able to reference memory locations in another process without permissionImpossible to check absolute addresses at compile time in programs because of relocation and dynamic address calculationAddress references must be checked at run time by hardwareMemory protection must be provided by the processor hardware rather than the OS

Page 6: Memory Management Chapter 7. Memory Management It is the task carried out by the OS with support from hardware to accommodate multiple processes in main

Sharing

Must allow several processes to access a common portion of main memory without compromising protection cooperating processes may need to

share access to the same data structure better to allow each process to access

the same copy of the program rather than have their own separate copy

Page 7: Memory Management Chapter 7. Memory Management It is the task carried out by the OS with support from hardware to accommodate multiple processes in main

Logical Organization

Main memory is organized as a linear address spaceHowever, users write programs in modules. Different degrees of protection for different

modules (read only, execute only, etc.) Modules can be shared among processes Modules can be written and compiled

independently and mutual references resolved at runtime

To effectively deal with user programs, the OS and hardware should support such modules

Page 8: Memory Management Chapter 7. Memory Management It is the task carried out by the OS with support from hardware to accommodate multiple processes in main

Physical Organization

Memory is organized in a hierarchySecondary memory is the long term store for programs and data while main memory holds program and data currently in useMoving information between these two levels of memory is a major concern of memory management (OS)It is highly inefficient to leave this responsibility to the application programmer

Page 9: Memory Management Chapter 7. Memory Management It is the task carried out by the OS with support from hardware to accommodate multiple processes in main

Simple Memory Management

In this chapter we study the simpler case where there is no virtual memory fixed partitioning dynamic partitioning simple paging simple segmentation

An executing process must be loaded entirely in main memory (if overlays are not used)Not used in modern OS, but they lay the ground for a proper discussion of virtual memory (next chapter)

Page 10: Memory Management Chapter 7. Memory Management It is the task carried out by the OS with support from hardware to accommodate multiple processes in main

Fixed Partitioning

Partition main memory into a set of non-overlapping regions called partitionsPartitions can be of equal or unequal sizes

Page 11: Memory Management Chapter 7. Memory Management It is the task carried out by the OS with support from hardware to accommodate multiple processes in main

Fixed Partitioning

Any process whose size is less than or equal to a partition size can be loaded into the partitionIf all partitions are occupied, the operating system can swap a process out of a partitionA program may be too large to fit in a partition, then the programmer must design the program with overlays when the module needed is not present the user

program must load that module into the program’s partition, overlaying whatever program or data are there

Page 12: Memory Management Chapter 7. Memory Management It is the task carried out by the OS with support from hardware to accommodate multiple processes in main

Fixed Partitioning

Main memory use is inefficient. Any program, no matter how small, occupies an entire partition. This phenomenon is called internal fragmentation (space is wasted internal to a partition).Unequal-size partitions reduces these problems but they still remain...Also, number of active processes is fixedFixed partitioning was used in early IBM’s OS/MFT (Multiprogramming with a Fixed number of Tasks)

Page 13: Memory Management Chapter 7. Memory Management It is the task carried out by the OS with support from hardware to accommodate multiple processes in main

Placement Algorithm with Fixed Partitions

Equal-size partitions If there is an available partition, a

process can be loaded into that partition because all partitions are of equal size, it

does not matter which partition is used If all partitions are occupied by blocked

processes, choose one process to swap out to make room for the new process

Page 14: Memory Management Chapter 7. Memory Management It is the task carried out by the OS with support from hardware to accommodate multiple processes in main

Placement Algorithm with Fixed Partitions

Unequal-size partitions: use of multiple queues

assign each process to the smallest partition within which it will fit

A queue for each partition size

tries to minimize internal fragmentation

Problem: some queues will be empty if no processes within a size range is present

Page 15: Memory Management Chapter 7. Memory Management It is the task carried out by the OS with support from hardware to accommodate multiple processes in main

Placement Algorithm with Fixed Partitions

Unequal-size partitions: use of a single queue

When it is time to load a process into main memory the smallest available partition that will hold the process is selected

increases the level of multiprogramming at the expense of internal fragmentation

Page 16: Memory Management Chapter 7. Memory Management It is the task carried out by the OS with support from hardware to accommodate multiple processes in main

Dynamic PartitioningPartitions are of variable length and numberEach process is allocated exactly as much memory as it requires – partition size same as process sizeEventually holes are formed in main memory. This is called external fragmentation (the holes are external to the partitions)Must use compaction to shift processes so they are contiguous and all free memory is in one blockUsed in IBM’s OS/MVT (Multiprogramming with a Variable number of Tasks)

Page 17: Memory Management Chapter 7. Memory Management It is the task carried out by the OS with support from hardware to accommodate multiple processes in main

Dynamic Partitioning: An Example

A hole of 64K is left after loading 3 processes: not enough room for another processEventually each process is blocked. The OS swaps out process 2 to bring in process 4 (128K)

Page 18: Memory Management Chapter 7. Memory Management It is the task carried out by the OS with support from hardware to accommodate multiple processes in main

Dynamic Partitioning: An Example

Another hole of 96K is createdEventually each process is blocked. The OS swaps out process 1 to bring in again process 2 and another hole of 96K is created...Compaction would produce a single hole of 256K

Page 19: Memory Management Chapter 7. Memory Management It is the task carried out by the OS with support from hardware to accommodate multiple processes in main

Placement AlgorithmUsed to decide which free block to allocate to a processGoal: to reduce usage of compaction (because it is time consuming).Possible algorithms: Best-fit: choose smallest

block that will fit First-fit: choose first block

large enough from beginning Next-fit: choose first block

large enough from last placement

Page 20: Memory Management Chapter 7. Memory Management It is the task carried out by the OS with support from hardware to accommodate multiple processes in main

Placement Algorithm: CommentsBest-fit searches for smallest block the fragment left behind is as small as possible main memory is quickly littered with holes too

small to hold any process, hence usually the worst algorithm

compaction generally needs to be done more often

First-fit favors allocation near the beginning simplest and usually the best algorithm tends to create less fragmentation then Next-fit

Next-fit often leads to allocation of the largest free block at the end of memory largest block at end is broken into small fragments requires more frequent compaction than First-fit

Page 21: Memory Management Chapter 7. Memory Management It is the task carried out by the OS with support from hardware to accommodate multiple processes in main

Replacement Algorithm

When all processes in main memory are blocked and there is insufficient memory for another process, the OS must choose which process to replace A process must be swapped out (to a Blocked-

Suspend state) and be replaced by a new process or a process from the Ready-Suspend queue

We will discuss later such algorithms for memory management schemes using virtual memory

Page 22: Memory Management Chapter 7. Memory Management It is the task carried out by the OS with support from hardware to accommodate multiple processes in main

Buddy System, I.

A reasonable compromise to overcome disadvantages of both fixed and variable partitioning schemesA modified form is used in Unix SVR4 for kernel memory allocationMemory blocks are available in size of 2K where L <= K <= U and where 2L = smallest size of block allocated 2U = largest size of block allocated

(generally, the entire memory available)

Page 23: Memory Management Chapter 7. Memory Management It is the task carried out by the OS with support from hardware to accommodate multiple processes in main

Buddy System, II.

We start with the entire block of size 2U

When a request of size S is made: If 2U-1 < S <= 2U then allocate the entire block of size

2U

Else, split this block into two buddies, each of size 2U-

1

If 2U-2 < S <= 2U-1 then allocate one of the two buddies

Otherwise one of the two buddies is split in half again

This process is repeated until the smallest block greater or equal to S is generated

Page 24: Memory Management Chapter 7. Memory Management It is the task carried out by the OS with support from hardware to accommodate multiple processes in main

Buddy System, III.

The OS maintains lists of holes (free blocks) the i-list is the list of holes of size 2i

A hole from the (i+1)-list may be removed by splitting it into two buddies of size 2i and putting them in the i-list

if a pair of buddies in the i-list become unallocated, they are removed from that list and coalesced into a single block in the (i+1)-list

Page 25: Memory Management Chapter 7. Memory Management It is the task carried out by the OS with support from hardware to accommodate multiple processes in main

Example of Buddy System

Page 26: Memory Management Chapter 7. Memory Management It is the task carried out by the OS with support from hardware to accommodate multiple processes in main

Relocation

Because of swapping and compaction, a process may occupy different main memory locations during its lifetimeHence physical memory references by a process cannot be fixedThis problem is solved by distinguishing between logical address and physical address

Page 27: Memory Management Chapter 7. Memory Management It is the task carried out by the OS with support from hardware to accommodate multiple processes in main

Address Types

A physical address (absolute address) is a physical location in main memoryA logical address is a reference to a memory location independent of the current assignment of program/data to memory Compilers produce code in which all memory references are logical addressesA relative address is an example of logical address in which the address is expressed as a location relative to some known point in the program (ex: the beginning)

Page 28: Memory Management Chapter 7. Memory Management It is the task carried out by the OS with support from hardware to accommodate multiple processes in main

Address Translation

Relative address is the most frequent type of logical address used in program modules (ie: executable files)Such modules are loaded in main memory with all memory references in relative formPhysical addresses are calculated “on the fly” as the instructions are executedFor adequate performance, the translation from relative to physical address must by done by hardware

Page 29: Memory Management Chapter 7. Memory Management It is the task carried out by the OS with support from hardware to accommodate multiple processes in main

Simple example of hardware translation of addressesWhen a process is assigned to the running state, a base register (in CPU) gets loaded with the starting physical address of the process A bounds register gets loaded with the process’s ending physical addressWhen a relative address is encountered, it is added with the content of the base register to obtain the physical address which is compared with the content of the bounds register This provides hardware protection: each process can only access memory within its process image

Page 30: Memory Management Chapter 7. Memory Management It is the task carried out by the OS with support from hardware to accommodate multiple processes in main

Hardware Support for Relocation

Page 31: Memory Management Chapter 7. Memory Management It is the task carried out by the OS with support from hardware to accommodate multiple processes in main

Paging

Memory partitioned into relatively small fixed size chunks called frames (page frames)Each process divided into chunks of same size called pages and can be assigned to framesNo external fragmentationMinimum internal fragmentation (last page)OS keeps track of frames in use and freeIf page size is power of two, address translation simplified

Page 32: Memory Management Chapter 7. Memory Management It is the task carried out by the OS with support from hardware to accommodate multiple processes in main

Paging Example

Page 33: Memory Management Chapter 7. Memory Management It is the task carried out by the OS with support from hardware to accommodate multiple processes in main

Page TableOS maintains a page-table for each process and a

list of free framesPage table is indexed by page number and the

entries are the corresponding frame numbers in main memory

Page 34: Memory Management Chapter 7. Memory Management It is the task carried out by the OS with support from hardware to accommodate multiple processes in main

Address Translation

Restrict page size to power of 2Logical address = (page #, offset)Physical address = (frame #, offset)16 bit addresses: 216 addressable bytes1K (210 = 1024) bytes page size: 10 bit offset, 6 bit page #Max number of pages = 26 = 64

Page 35: Memory Management Chapter 7. Memory Management It is the task carried out by the OS with support from hardware to accommodate multiple processes in main

Address Translation

Figure 7.11

Page 36: Memory Management Chapter 7. Memory Management It is the task carried out by the OS with support from hardware to accommodate multiple processes in main

Logical to Physical Address Translation with Paging

Page 37: Memory Management Chapter 7. Memory Management It is the task carried out by the OS with support from hardware to accommodate multiple processes in main

Address Translation

Advantages of power of 2 frame size: Logical address (page #, offset) is

same as relative address Easy to convert logical address to

physical address in hardware

Page 38: Memory Management Chapter 7. Memory Management It is the task carried out by the OS with support from hardware to accommodate multiple processes in main

Segmentation

Programs divided into variable length segments, usually along boundaries specific to the programExternal fragmentation is problemNo internal fragmentationProgrammer/Compiler must be aware of max segment size, etc. (paging is invisible to the programmer)

Page 39: Memory Management Chapter 7. Memory Management It is the task carried out by the OS with support from hardware to accommodate multiple processes in main

Address Translation

Logical address = (segment #, offset)Segment # indexes segment tableEach segment table entry contains starting physical address (base address) of segment and length of segmentPhysical address = base address + offsetIf offset > length of segment, address is invalid

Page 40: Memory Management Chapter 7. Memory Management It is the task carried out by the OS with support from hardware to accommodate multiple processes in main

Address Translation with Segmentation

Page 41: Memory Management Chapter 7. Memory Management It is the task carried out by the OS with support from hardware to accommodate multiple processes in main

Comparison of Memory Management Techniques

See Table 7.1, page 306.