operating system concepts

35
Operating System Operating System Concepts Concepts Ku-Yaw Chang Ku-Yaw Chang [email protected] [email protected] Assistant Professor, Department of Assistant Professor, Department of Computer Science and Information Engineering Computer Science and Information Engineering Da-Yeh University Da-Yeh University

Upload: penha

Post on 29-Jan-2016

41 views

Category:

Documents


0 download

DESCRIPTION

Operating System Concepts. Ku-Yaw Chang [email protected] Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University. Chapter 9 Memory Management. Keep several processes in memory to increase CPU utilization Memory management algorithms - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Operating System Concepts

Operating System Operating System ConceptsConcepts

Ku-Yaw ChangKu-Yaw [email protected]@mail.dyu.edu.tw

Assistant Professor, Department of Assistant Professor, Department of Computer Science and Information EngineeringComputer Science and Information Engineering

Da-Yeh UniversityDa-Yeh University

Page 2: Operating System Concepts

22Ku-Yaw ChangKu-Yaw Chang Chapter 9 Memory ManagementChapter 9 Memory Management

Chapter 9 Memory ManagementChapter 9 Memory Management

Keep several processes in memory to increase Keep several processes in memory to increase CPU utilizationCPU utilization

Memory management algorithmsMemory management algorithms Require hardware supportRequire hardware support Common strategiesCommon strategies

PagingPaging

SegmentationSegmentation

Page 3: Operating System Concepts

33Ku-Yaw ChangKu-Yaw Chang Chapter 9 Memory ManagementChapter 9 Memory Management

Chapter 9 Memory ManagementChapter 9 Memory Management

1.1. BackgroundBackground

2.2. SwappingSwapping

3.3. Contiguous Memory Contiguous Memory AllocationAllocation

4.4. PagingPaging

5.5. SegmentationSegmentation

6.6. Segmentation with Segmentation with PagingPaging

7.7. SummarySummary

8.8. ExercisesExercises

Page 4: Operating System Concepts

44Ku-Yaw ChangKu-Yaw Chang Chapter 9 Memory ManagementChapter 9 Memory Management

9.1 Background9.1 Background

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

Address bindingAddress binding A mapping from one address space to anotherA mapping from one address space to another

A typical instruction-execution cycleA typical instruction-execution cycle FetchFetch an instruction from memory an instruction from memory DecodeDecode the instruction the instruction

May cause operands to be fetched from memoryMay cause operands to be fetched from memory ExecuteExecute the instruction the instruction StoreStore results back into memory results back into memory

Page 5: Operating System Concepts

55Ku-Yaw ChangKu-Yaw Chang Chapter 9 Memory ManagementChapter 9 Memory Management

9.1.1 Address Binding9.1.1 Address Binding

Input queueInput queue A collection of processes on the disk that are waiting A collection of processes on the disk that are waiting

to be brought into memory to run the program.to be brought into memory to run the program.

A user program will go through several steps A user program will go through several steps before being executedbefore being executed Addresses in source program are Addresses in source program are symbolicsymbolic A compiler binds these symbolic addresses to A compiler binds these symbolic addresses to

relocatable addressesrelocatable addresses A loader binds these relocatable addresses to A loader binds these relocatable addresses to

absolute addressesabsolute addresses

Page 6: Operating System Concepts

66Ku-Yaw ChangKu-Yaw Chang Chapter 9 Memory ManagementChapter 9 Memory Management

9.1.1 Address Binding9.1.1 Address Binding

Compile timeCompile time Absolute code can Absolute code can

be generatedbe generatedKnow at compile Know at compile time where the time where the process will reside process will reside in memoryin memory

MS-DOS .COM-MS-DOS .COM-format programs format programs are absolute codeare absolute code

Page 7: Operating System Concepts

77Ku-Yaw ChangKu-Yaw Chang Chapter 9 Memory ManagementChapter 9 Memory Management

9.1.1 Address Binding9.1.1 Address Binding

Load timeLoad time Relocatable code Relocatable code

can be generatedcan be generatedNot known at Not known at compile time where compile time where the process will the process will reside in memoryreside in memory

Final binding is Final binding is delayed until load delayed until load timetime

Page 8: Operating System Concepts

88Ku-Yaw ChangKu-Yaw Chang Chapter 9 Memory ManagementChapter 9 Memory Management

9.1.1 Address Binding9.1.1 Address Binding

Execution timeExecution time The process can be The process can be

moved from one moved from one memory segment memory segment to anotherto another

Binding must be Binding must be delayed until run delayed until run timetime

Special hardware Special hardware must be availablemust be available

Page 9: Operating System Concepts

99Ku-Yaw ChangKu-Yaw Chang Chapter 9 Memory ManagementChapter 9 Memory Management

9.1.2 Logical- Versus Physical-9.1.2 Logical- Versus Physical-Address SpaceAddress Space

LogicalLogical address address An address generated by the CPUAn address generated by the CPU Compile-time and load-timeCompile-time and load-time Also called Also called virtual addressvirtual address Logical-address spaceLogical-address space

The set of all logical addressesThe set of all logical addresses

PhysicalPhysical address address An address seen by the memory unitAn address seen by the memory unit

The one loaded into the memory-address unitThe one loaded into the memory-address unit Execution timeExecution time

Logical and physical address spaces differLogical and physical address spaces differ Physical-address spacePhysical-address space

Page 10: Operating System Concepts

1010Ku-Yaw ChangKu-Yaw Chang Chapter 9 Memory ManagementChapter 9 Memory Management

9.1.2 Logical- Versus Physical-9.1.2 Logical- Versus Physical-Address SpaceAddress Space

MMemory-emory-MManagement anagement UUnit (nit (MMUMMU)) A hardware deviceA hardware device

Run-time mapping from virtual to physical addressesRun-time mapping from virtual to physical addresses Different methods to accomplish such a mappingDifferent methods to accomplish such a mapping

Logical addressesLogical addresses 00 to to maxmax

Physical addressesPhysical addresses RR + + 00 to to RR + + maxmax

Page 11: Operating System Concepts

1111Ku-Yaw ChangKu-Yaw Chang Chapter 9 Memory ManagementChapter 9 Memory Management

Dynamic RelocationDynamic RelocationUsing a Relocation RegisterUsing a Relocation Register

Page 12: Operating System Concepts

1212Ku-Yaw ChangKu-Yaw Chang Chapter 9 Memory ManagementChapter 9 Memory Management

9.1.3 Dynamic Loading9.1.3 Dynamic Loading

The entire program and data must be in memory The entire program and data must be in memory for its executionfor its execution The size of a process is limited to the size pf physical The size of a process is limited to the size pf physical

memory.memory.

Dynamic LoadingDynamic Loading All routines are kept on disk in a relocatable load All routines are kept on disk in a relocatable load

formatformatThe main program is loaded into memory and is executedThe main program is loaded into memory and is executed

A routine is not loaded until it is calledA routine is not loaded until it is called

AdvantageAdvantage An unused routine is never loadedAn unused routine is never loaded

Page 13: Operating System Concepts

1313Ku-Yaw ChangKu-Yaw Chang Chapter 9 Memory ManagementChapter 9 Memory Management

9.1.4 Dynamic Linking and9.1.4 Dynamic Linking andShared LibrariesShared Libraries

Dynamic LinkingDynamic Linking Linking is postponed until execution timeLinking is postponed until execution time

Small piece of code, called Small piece of code, called stubstub, used to locate the , used to locate the appropriate memory-resident library routineappropriate memory-resident library routine

OS checks if routine is in processes’ memory addressOS checks if routine is in processes’ memory addressIf yes, load the routine into memoryIf yes, load the routine into memory

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

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

Page 14: Operating System Concepts

1414Ku-Yaw ChangKu-Yaw Chang Chapter 9 Memory ManagementChapter 9 Memory Management

9.1.5 Overlays9.1.5 Overlays

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

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

FeaturesFeatures Implemented by userImplemented by user No special support needed from operating systemNo special support needed from operating system Programming design is complexProgramming design is complex

Page 15: Operating System Concepts

1515Ku-Yaw ChangKu-Yaw Chang Chapter 9 Memory ManagementChapter 9 Memory Management

9.1.5 Overlays9.1.5 Overlays

Page 16: Operating System Concepts

1616Ku-Yaw ChangKu-Yaw Chang Chapter 9 Memory ManagementChapter 9 Memory Management

Chapter 9 Memory ManagementChapter 9 Memory Management

1.1. BackgroundBackground

2.2. SwappingSwapping

3.3. Contiguous Memory Contiguous Memory AllocationAllocation

4.4. PagingPaging

5.5. SegmentationSegmentation

6.6. Segmentation with Segmentation with PagingPaging

7.7. SummarySummary

8.8. ExercisesExercises

Page 17: Operating System Concepts

1717Ku-Yaw ChangKu-Yaw Chang Chapter 9 Memory ManagementChapter 9 Memory Management

SwappingSwapping

A process can beA process can be Swapped temporarily out of memory to a backing Swapped temporarily out of memory to a backing

storestoreCommonly a fast diskCommonly a fast disk

Brought back into memory for continued executionBrought back into memory for continued execution

A process swapped back intoA process swapped back into The same memory spaceThe same memory space

Binding is done at assembly or load timeBinding is done at assembly or load time A different memory spaceA different memory space

Execution-time bindingExecution-time binding

Page 18: Operating System Concepts

1818Ku-Yaw ChangKu-Yaw Chang Chapter 9 Memory ManagementChapter 9 Memory Management

Swapping of two processesSwapping of two processes

Page 19: Operating System Concepts

1919Ku-Yaw ChangKu-Yaw Chang Chapter 9 Memory ManagementChapter 9 Memory Management

SwappingSwapping

Context-switch time is fairly highContext-switch time is fairly high User process size: 1MBUser process size: 1MB Transfer rate: 5MB per secondTransfer rate: 5MB per second Actual transfer:Actual transfer:

1000KB / 5000 KB per second = 200 milliseconds1000KB / 5000 KB per second = 200 milliseconds An average latency: 8 msAn average latency: 8 ms Total swap timeTotal swap time

208 + 208 = 416 ms208 + 208 = 416 ms Time quantum should be substantially larger than Time quantum should be substantially larger than

0.416 seconds.0.416 seconds.

Page 20: Operating System Concepts

2020Ku-Yaw ChangKu-Yaw Chang Chapter 9 Memory ManagementChapter 9 Memory Management

SwappingSwapping

Major part of the swap time is transfer timeMajor part of the swap time is transfer time Directly proportional to the amount of memory Directly proportional to the amount of memory

swappedswapped

FactorsFactors How much memory is actually usedHow much memory is actually used

To reduce swap timeTo reduce swap time Be sure the process is completely idleBe sure the process is completely idle

Pending I/OPending I/O

Page 21: Operating System Concepts

2121Ku-Yaw ChangKu-Yaw Chang Chapter 9 Memory ManagementChapter 9 Memory Management

Chapter 9 Memory ManagementChapter 9 Memory Management

1.1. BackgroundBackground

2.2. SwappingSwapping

3.3. Contiguous Memory Contiguous Memory AllocationAllocation

4.4. PagingPaging

5.5. SegmentationSegmentation

6.6. Segmentation with Segmentation with PagingPaging

7.7. SummarySummary

8.8. ExercisesExercises

Page 22: Operating System Concepts

2222Ku-Yaw ChangKu-Yaw Chang Chapter 9 Memory ManagementChapter 9 Memory Management

Contiguous Memory AllocationContiguous Memory Allocation

MemoryMemory One for the resident operating systemOne for the resident operating system

In either low or high memoryIn either low or high memory Location of the interrupt vectorLocation of the interrupt vector

One for the user processesOne for the user processes

Contiguous memory allocationContiguous memory allocation Each process is contained in a single contiguous Each process is contained in a single contiguous

section of memorysection of memory

Page 23: Operating System Concepts

2323Ku-Yaw ChangKu-Yaw Chang Chapter 9 Memory ManagementChapter 9 Memory Management

Memory ProtectionMemory Protection

A relocation register with a limit registerA relocation register with a limit register

Page 24: Operating System Concepts

2424Ku-Yaw ChangKu-Yaw Chang Chapter 9 Memory ManagementChapter 9 Memory Management

Memory AllocationMemory Allocation

Fixed-sized partitions Fixed-sized partitions SimplestSimplest Each partition contain exactly one processEach partition contain exactly one process

Degree of multiprogramming is boundedDegree of multiprogramming is bounded StrategiesStrategies

First fitFirst fitBest fitBest fitWorst fitWorst fit

ProblemProblem External fragmentationExternal fragmentation

Internal fragmentationInternal fragmentation

50-percent rule50-percent rule Given N allocated blocksGiven N allocated blocks

Another 0.5 N blocks will be lostAnother 0.5 N blocks will be lost

Page 25: Operating System Concepts

2525Ku-Yaw ChangKu-Yaw Chang Chapter 9 Memory ManagementChapter 9 Memory Management

Memory AllocationMemory Allocation

Possible solutions to the external fragmentationPossible solutions to the external fragmentation CompactionCompaction Permit the logical address space of a process to be Permit the logical address space of a process to be

noncontiguousnoncontiguous

OS

process 5

process 8

process 2

OS

process 5

process 2

OS

process 5

process 2

OS

process 5

process 9

process 2

process 9

process 10

Page 26: Operating System Concepts

2626Ku-Yaw ChangKu-Yaw Chang Chapter 9 Memory ManagementChapter 9 Memory Management

Chapter 9 Memory ManagementChapter 9 Memory Management

1.1. BackgroundBackground

2.2. SwappingSwapping

3.3. Contiguous Memory Contiguous Memory AllocationAllocation

4.4. PagingPaging

5.5. SegmentationSegmentation

6.6. Segmentation with Segmentation with PagingPaging

7.7. SummarySummary

8.8. ExercisesExercises

Page 27: Operating System Concepts

2727Ku-Yaw ChangKu-Yaw Chang Chapter 9 Memory ManagementChapter 9 Memory Management

PagingPaging

A memory-management scheme that permits A memory-management scheme that permits the physical-address space of a process to be the physical-address space of a process to be noncontiguousnoncontiguous FramesFrames (physical memory) (physical memory)

Fixed-sized blocks Fixed-sized blocks PagesPages (logical memory) (logical memory)

Blocks of the same sizeBlocks of the same size

Every address is divided intoEvery address is divided into A page number (p)A page number (p)

An index to a page tableAn index to a page table A page offset (d)A page offset (d)

p d

page number page offset

m-n n

Page 28: Operating System Concepts

2828Ku-Yaw ChangKu-Yaw Chang Chapter 9 Memory ManagementChapter 9 Memory Management

Paging hardwarePaging hardware

Page 29: Operating System Concepts

2929Ku-Yaw ChangKu-Yaw Chang Chapter 9 Memory ManagementChapter 9 Memory Management

Paging ModelPaging Model

Page 30: Operating System Concepts

3030Ku-Yaw ChangKu-Yaw Chang Chapter 9 Memory ManagementChapter 9 Memory Management

Paging ExamplePaging Example

Page 31: Operating System Concepts

3131Ku-Yaw ChangKu-Yaw Chang Chapter 9 Memory ManagementChapter 9 Memory Management

Free FramesFree Frames

Before Allocation After Allocation

Page 32: Operating System Concepts

3232Ku-Yaw ChangKu-Yaw Chang Chapter 9 Memory ManagementChapter 9 Memory Management

Chapter 9 Memory ManagementChapter 9 Memory Management

1.1. BackgroundBackground

2.2. SwappingSwapping

3.3. Contiguous Memory Contiguous Memory AllocationAllocation

4.4. PagingPaging

5.5. SegmentationSegmentation

6.6. Segmentation with Segmentation with PagingPaging

7.7. SummarySummary

8.8. ExercisesExercises

Page 33: Operating System Concepts

3333Ku-Yaw ChangKu-Yaw Chang Chapter 9 Memory ManagementChapter 9 Memory Management

Chapter 9 Memory ManagementChapter 9 Memory Management

1.1. BackgroundBackground

2.2. SwappingSwapping

3.3. Contiguous Memory Contiguous Memory AllocationAllocation

4.4. PagingPaging

5.5. SegmentationSegmentation

6.6. Segmentation with Segmentation with PagingPaging

7.7. SummarySummary

8.8. ExercisesExercises

Page 34: Operating System Concepts

3434Ku-Yaw ChangKu-Yaw Chang Chapter 9 Memory ManagementChapter 9 Memory Management

Chapter 9 Memory ManagementChapter 9 Memory Management

1.1. BackgroundBackground

2.2. SwappingSwapping

3.3. Contiguous Memory Contiguous Memory AllocationAllocation

4.4. PagingPaging

5.5. SegmentationSegmentation

6.6. Segmentation with Segmentation with PagingPaging

7.7. SummarySummary

8.8. ExercisesExercises

Page 35: Operating System Concepts

3535Ku-Yaw ChangKu-Yaw Chang Chapter 9 Memory ManagementChapter 9 Memory Management

Chapter 9 Memory ManagementChapter 9 Memory Management

1.1. BackgroundBackground

2.2. SwappingSwapping

3.3. Contiguous Memory Contiguous Memory AllocationAllocation

4.4. PagingPaging

5.5. SegmentationSegmentation

6.6. Segmentation with Segmentation with PagingPaging

7.7. SummarySummary

8.8. ExercisesExercises