Bab 4

42

description

TTM309

Transcript of Bab 4

Page 1: Bab 4
Page 2: Bab 4

BackgroundBackground Swapping Swapping Contiguous AllocationContiguous Allocation PagingPaging SegmentationSegmentation

Page 3: Bab 4

Program must be brought into Program must be brought into memory and placed within a process memory and placed within a process for it to be run.for it to be run.

Input queueInput queue – collection of processes – collection of processes on the disk that are waiting to be on the disk that are waiting to be brought into memory to run the brought into memory to run the program.program.

User programs go through several User programs go through several steps before being run. steps before being run.

Page 4: Bab 4

Compile timeCompile time: If memory location known a : If memory location known a priori, absolute code can be generated; must priori, absolute code can be generated; must recompile code if starting location changes.recompile code if starting location changes.

Load timeLoad time: Must generate : Must generate relocatablerelocatable code code if memory location is not known at compile if memory location is not known at compile time.time.

Execution timeExecution time: Binding delayed until run : Binding delayed until run time if the process can be moved during its time if the process can be moved during its execution from one memory segment to execution from one memory segment to another. Need hardware support for address another. Need hardware support for address maps (e.g., maps (e.g., basebase and and limit registerslimit registers). ).

Address binding of instructions and data to memory addresses can happen at three different stages.

Page 5: Bab 4
Page 6: Bab 4

The concept of a logical The concept of a logical address spaceaddress space that is that is bound to a separate bound to a separate physicalphysical address spaceaddress space is is central to proper memory management.central to proper memory management. Logical addressLogical address – generated by the CPU; also – generated by the CPU; also

referred to as referred to as virtual addressvirtual address.. Physical addressPhysical address – address seen by the – address seen by the

memory unit.memory unit. Logical and physical addresses Logical and physical addresses are the sameare the same in in

compile-time and load-time address-binding compile-time and load-time address-binding schemes. schemes.

Logical (virtual) and physical addresses Logical (virtual) and physical addresses differdiffer in execution-time address-binding scheme.in execution-time address-binding scheme.

Page 7: Bab 4

Hardware device that maps virtual Hardware device that maps virtual to physical address.to physical address.

In MMU scheme, the value in the In MMU scheme, the value in the relocation registerrelocation register is added to is added to every address generated by a user every address generated by a user process at the time it is sent to process at the time it is sent to memory.memory.

The The user programuser program deals with deals with logicallogical addresses; it never sees the addresses; it never sees the realreal physical addresses.physical addresses.

Page 8: Bab 4
Page 9: Bab 4

Routine is not loaded until it is called.Routine is not loaded until it is called. Better memory-space utilization; Better memory-space utilization;

unused routine is never loaded.unused routine is never loaded. Useful when large amounts of code Useful when large amounts of code

are needed to handle infrequently are needed to handle infrequently occurring cases.occurring cases.

No special support from the operating No special support from the operating system is required implemented system is required implemented through program design.through program design.

Page 10: Bab 4

Linking postponed until execution time.Linking postponed until execution time. Small piece of code, Small piece of code, stubstub, used to , used to

locate the appropriate memory-resident locate the appropriate memory-resident library routine.library routine.

Stub replaces itself with the address of Stub replaces itself with the address of the routine, and executes the routine.the routine, and executes the routine.

Operating system needed to check if Operating system needed to check if routine is in processes’ memory routine is in processes’ memory address.address.

Dynamic linking is particularly useful Dynamic linking is particularly useful for libraries.for libraries.

Page 11: Bab 4

Keep in memory only those instructions Keep in memory only those instructions and data that are needed at any given and data that are needed at any given time.time.

Needed when process is larger than Needed when process is larger than amount of memory allocated to it.amount of memory allocated to it.

Implemented by user, no special support Implemented by user, no special support needed from operating system, needed from operating system, programming design of overlay structure is programming design of overlay structure is complexcomplex

Page 12: Bab 4
Page 13: Bab 4

A process can be A process can be swappedswapped temporarily out of temporarily out of memory to a memory to a backing storebacking store, and then brought back , and then brought back into memory for continued execution.into memory for continued execution.

Backing storeBacking store – fast disk large enough to – fast disk large enough to accommodate copies of all memory images for all accommodate copies of all memory images for all users; must provide direct access to these memory users; must provide direct access to these memory images.images.

Roll out, roll inRoll out, roll in – swapping variant used for priority- – swapping variant used for priority-based scheduling algorithms; lower-priority process based scheduling algorithms; lower-priority process is swapped out so higher-priority process can be is swapped out so higher-priority process can be loaded and executed.loaded and executed.

Major part of swap time is transfer time; total Major part of swap time is transfer time; total transfer time is directly proportional to the transfer time is directly proportional to the amountamount of memory swapped.of memory swapped.

Modified versions of swapping are found on many Modified versions of swapping are found on many systems, i.e., UNIX, Linux, and Windows.systems, i.e., UNIX, Linux, and Windows.

Page 14: Bab 4
Page 15: Bab 4

Main memory usually divided into two partitions:Main memory usually divided into two partitions: Resident operating system. Resident operating system.

usually held in low memory with interrupt usually held in low memory with interrupt vector.vector.

User processes User processes held in high memory.held in high memory.

Single-partition allocationSingle-partition allocation Relocation-register scheme: used to protect: Relocation-register scheme: used to protect:

user processes from each other, and user processes from each other, and from changing operating-system code and data.from changing operating-system code and data.

Relocation register contains value of Relocation register contains value of smallest smallest physical address.physical address.

limit register contains range of logical addresses – limit register contains range of logical addresses – each logical address must be less than the limit each logical address must be less than the limit register. register.

Page 16: Bab 4
Page 17: Bab 4

Multiple-partition allocationMultiple-partition allocation HoleHole – block of available memory; holes of various – block of available memory; holes of various

size are scattered throughout memory.size are scattered throughout memory. When a process arrives, it is allocated memory When a process arrives, it is allocated memory

from a hole large enough to accommodate it.from a hole large enough to accommodate it. Operating system maintains information about:Operating system maintains information about:

a) allocated partitions a) allocated partitions b) free partitions (hole)b) free partitions (hole)

OSOS

process 5process 5

process 8process 8

process 2process 2

OSOS

process 5process 5

process 2process 2

OSOS

process 5process 5

process 2process 2

OSOS

process 5process 5

process 9process 9

process 2process 2

process 9process 9

process process 1010P8 P8

terminatesterminatesAllocate Allocate

P9P9Allocate Allocate

P10P10

Page 18: Bab 4

2560K2560K

Operating Operating SystemSystem

00

400K400K

2160K2160K

Job QueueJob Queue

ProcessProcess

P1P1 600K600KP2P2 1000K1000KP3P3 300K300KP4P4 700K700KP5P5 500K500K

sizesize

Page 19: Bab 4
Page 20: Bab 4

First-fit: Allocate the First-fit: Allocate the firstfirst hole that is big enough. hole that is big enough. Best-fit: Allocate the Best-fit: Allocate the smallestsmallest hole that is big hole that is big

enough; must search entire list, unless ordered by enough; must search entire list, unless ordered by size. Produces the smallest leftover hole.size. Produces the smallest leftover hole.

Worst-fit: Allocate the Worst-fit: Allocate the largestlargest hole; must also hole; must also search entire list. Produces the largest leftover search entire list. Produces the largest leftover hole.hole.

How to satisfy a request of size How to satisfy a request of size nn from a list of free holes. from a list of free holes.

First-fit and best-fit better than worst-fit in terms of First-fit and best-fit better than worst-fit in terms of speed and storage utilization.speed and storage utilization.

Page 21: Bab 4

External Fragmentation – total memory space External Fragmentation – total memory space exists to satisfy a request, but it is not contiguous.exists to satisfy a request, but it is not contiguous.

Internal Fragmentation – allocated memory may be Internal Fragmentation – allocated memory may be slightly larger than requested memory; this size slightly larger than requested memory; this size difference is memory internal to a partition, but difference is memory internal to a partition, but not being used.not being used.

Reduce external fragmentation by Reduce external fragmentation by compactioncompaction Shuffle memory contents to place all free Shuffle memory contents to place all free

memory together in one large block.memory together in one large block. Compaction is possible Compaction is possible onlyonly if relocation is if relocation is

dynamic, and is done at execution time.dynamic, and is done at execution time. I/O problemI/O problem

Latch job in memory while it is involved in I/O.Latch job in memory while it is involved in I/O. Do I/O only into OS buffers.Do I/O only into OS buffers.

Page 22: Bab 4
Page 23: Bab 4
Page 24: Bab 4

Logical address space of a process can be Logical address space of a process can be noncontiguous; process is allocated noncontiguous; process is allocated physical memory whenever the latter is physical memory whenever the latter is available.available.

Divide physical memory into fixed-sized Divide physical memory into fixed-sized blocks called frames (size is power of 2, blocks called frames (size is power of 2, between 512 bytes and 8192 bytes).between 512 bytes and 8192 bytes).

Divide logical memory into blocks of same Divide logical memory into blocks of same size called pages.size called pages.

Keep track of all free frames.Keep track of all free frames. To run a program of size To run a program of size nn pages, need to pages, need to

find find nn free frames and load program. free frames and load program. Set up a page table to translate logical to Set up a page table to translate logical to

physical addresses. physical addresses. Internal fragmentation.Internal fragmentation.

Page 25: Bab 4

Address generated by CPU is Address generated by CPU is divided into:divided into: Page numberPage number (p)(p) – used as an – used as an

index into a index into a pagepage tabletable which which contains base address of each contains base address of each page in physical memory.page in physical memory.

Page offsetPage offset (d)(d) – combined with – combined with base address to define the base address to define the physical memory address that is physical memory address that is sent to the memory unit.sent to the memory unit.

Page 26: Bab 4
Page 27: Bab 4
Page 28: Bab 4
Page 29: Bab 4

Before allocationBefore allocation After allocationAfter allocation

Page 30: Bab 4

Memory protection implemented by Memory protection implemented by associating protection bit with each frame.associating protection bit with each frame.

Valid-invalidValid-invalid bit attached to each entry in bit attached to each entry in the page table:the page table: ““valid” indicates that the associated valid” indicates that the associated

page is in the process’ logical address page is in the process’ logical address space, and is thus a legal page.space, and is thus a legal page.

““invalid” indicates that the page is not in invalid” indicates that the page is not in the process’ logical address space.the process’ logical address space.

Page 31: Bab 4
Page 32: Bab 4

Shared codeShared code One copy of read-only (reentrant) code shared One copy of read-only (reentrant) code shared

among processes (i.e., text editors, compilers, among processes (i.e., text editors, compilers, window systems). window systems).

Shared code must appear in same location in Shared code must appear in same location in the logical address space of all processes.the logical address space of all processes.

Private code and data Private code and data Each process keeps a separate copy of the Each process keeps a separate copy of the

code and data.code and data. The pages for the private code and data can The pages for the private code and data can

appear anywhere in the logical address space.appear anywhere in the logical address space.

Page 33: Bab 4
Page 34: Bab 4

Memory-management scheme that Memory-management scheme that supports user view of memory. supports user view of memory.

A program is a collection of segments. A A program is a collection of segments. A segment is a logical unit such as:segment is a logical unit such as:

main program,main program,procedure, procedure, function,function,method,method,object,object,local variables, global local variables, global

variables,variables,common block,common block,stack,stack,symbol table, arrayssymbol table, arrays

Page 35: Bab 4
Page 36: Bab 4

1

3

2

4

1

4

2

3

user space user space physical memory spacephysical memory space

Page 37: Bab 4

Logical address consists of a two tuple:Logical address consists of a two tuple:

<segment-number, offset>,<segment-number, offset>, Segment tableSegment table – maps two-dimensional physical – maps two-dimensional physical

addresses; each table entry has:addresses; each table entry has: base – contains the starting physical address base – contains the starting physical address

where the segments reside in memory.where the segments reside in memory. limitlimit – specifies the length of the segment. – specifies the length of the segment.

Segment-table base register (STBR)Segment-table base register (STBR) points to the points to the segment table’s location in memory.segment table’s location in memory.

Segment-table length register (STLR)Segment-table length register (STLR) indicates indicates number of segments used by a program;number of segments used by a program; segment number segment number ss is legal if is legal if ss < STLR. < STLR.

Page 38: Bab 4

Relocation. dynamic by segment table

Sharing. shared segments same segment number

Allocation. first fit/best fit external fragmentation

Page 39: Bab 4

Protection. With each entry in segment Protection. With each entry in segment table associate:table associate: validation bit = 0 validation bit = 0 illegal segment illegal segment read/write/execute privilegesread/write/execute privileges

Protection bits associated with segments; Protection bits associated with segments; code sharing occurs at segment level.code sharing occurs at segment level.

Since segments vary in length, memory Since segments vary in length, memory allocation is a dynamic storage-allocation allocation is a dynamic storage-allocation problem.problem.

A segmentation example is shown in the A segmentation example is shown in the following diagramfollowing diagram

Page 40: Bab 4
Page 41: Bab 4
Page 42: Bab 4