125
Main Memory (Part I) Amir H. Payberah [email protected] Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 1 / 47

Main Memory - Part1

Embed Size (px)

Citation preview

Page 1: Main Memory - Part1

Main Memory (Part I)

Amir H. [email protected]

Amirkabir University of Technology(Tehran Polytechnic)

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 1 / 47

Page 2: Main Memory - Part1

Motivation and Background

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 2 / 47

Page 3: Main Memory - Part1

Motivation

I Main memory is a large array of bytes, each with its own address.

I Program must be brought (from disk) into memory and placedwithin a process for it to be run.

• Machine instructions may take memory addresses as arguments, butnot disk addresses.

I The CPU fetches instructions from memory according to the valueof the program counter.

I These instructions may cause additional loading from and storing tospecific memory addresses.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 3 / 47

Page 4: Main Memory - Part1

Motivation

I Main memory is a large array of bytes, each with its own address.

I Program must be brought (from disk) into memory and placedwithin a process for it to be run.

• Machine instructions may take memory addresses as arguments, butnot disk addresses.

I The CPU fetches instructions from memory according to the valueof the program counter.

I These instructions may cause additional loading from and storing tospecific memory addresses.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 3 / 47

Page 5: Main Memory - Part1

Motivation

I Main memory is a large array of bytes, each with its own address.

I Program must be brought (from disk) into memory and placedwithin a process for it to be run.

• Machine instructions may take memory addresses as arguments, butnot disk addresses.

I The CPU fetches instructions from memory according to the valueof the program counter.

I These instructions may cause additional loading from and storing tospecific memory addresses.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 3 / 47

Page 6: Main Memory - Part1

Motivation

I Main memory is a large array of bytes, each with its own address.

I Program must be brought (from disk) into memory and placedwithin a process for it to be run.

• Machine instructions may take memory addresses as arguments, butnot disk addresses.

I The CPU fetches instructions from memory according to the valueof the program counter.

I These instructions may cause additional loading from and storing tospecific memory addresses.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 3 / 47

Page 7: Main Memory - Part1

Basic Hardware

I Main memory and registers are the only storage that the CPU canaccess directly.

I Register access in one CPU clock (or less)

I Main memory can take many cycles, causing a stall.

I Cache sits between main memory and registers.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 4 / 47

Page 8: Main Memory - Part1

Basic Hardware

I Main memory and registers are the only storage that the CPU canaccess directly.

I Register access in one CPU clock (or less)

I Main memory can take many cycles, causing a stall.

I Cache sits between main memory and registers.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 4 / 47

Page 9: Main Memory - Part1

Basic Hardware

I Main memory and registers are the only storage that the CPU canaccess directly.

I Register access in one CPU clock (or less)

I Main memory can take many cycles, causing a stall.

I Cache sits between main memory and registers.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 4 / 47

Page 10: Main Memory - Part1

Basic Hardware

I Main memory and registers are the only storage that the CPU canaccess directly.

I Register access in one CPU clock (or less)

I Main memory can take many cycles, causing a stall.

I Cache sits between main memory and registers.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 4 / 47

Page 11: Main Memory - Part1

Address Protection

I We must protect the OS from access by user processes.

I We must protect user processes from one another.

I This protection is provided by the hardware.

I A separate memory space for each process.• Determining the range of legal addresses that the process may

access.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 5 / 47

Page 12: Main Memory - Part1

Address Protection

I We must protect the OS from access by user processes.

I We must protect user processes from one another.

I This protection is provided by the hardware.

I A separate memory space for each process.• Determining the range of legal addresses that the process may

access.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 5 / 47

Page 13: Main Memory - Part1

Address Protection

I We must protect the OS from access by user processes.

I We must protect user processes from one another.

I This protection is provided by the hardware.

I A separate memory space for each process.• Determining the range of legal addresses that the process may

access.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 5 / 47

Page 14: Main Memory - Part1

Address Protection

I We must protect the OS from access by user processes.

I We must protect user processes from one another.

I This protection is provided by the hardware.

I A separate memory space for each process.• Determining the range of legal addresses that the process may

access.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 5 / 47

Page 15: Main Memory - Part1

Base and Limit Registers

I A pair of base and limit registersdefine the logical address space.

I CPU must check every memoryaccess generated in user mode tobe sure it is between base and limitfor that user.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 6 / 47

Page 16: Main Memory - Part1

Hardware Address Protection

I Any attempt by a user program to access OS memory or otherusers’ memory results in a trap to the OS, which treats theattempt as a fatal error.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 7 / 47

Page 17: Main Memory - Part1

Address Binding

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 8 / 47

Page 18: Main Memory - Part1

Address Binding (1/2)

I Programs on disk, ready to be brought into memory to executeform an input queue.

I A user process can reside in any part of the physical memory.• Without support, must be loaded into address 00000.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 9 / 47

Page 19: Main Memory - Part1

Address Binding (1/2)

I Programs on disk, ready to be brought into memory to executeform an input queue.

I A user process can reside in any part of the physical memory.• Without support, must be loaded into address 00000.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 9 / 47

Page 20: Main Memory - Part1

Address Binding (2/2)

I A user program goes through several steps before being executed.

I Addresses may be represented in different ways during these steps.

• Source code addresses usually symbolic.

• A compiler typically binds these symbolic addresses to relocatableaddresses, e.g., 14 bytes from beginning of this module.

• Linker or loader will bind relocatable addresses to absoluteaddresses, e.g., 74014.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 10 / 47

Page 21: Main Memory - Part1

Address Binding (2/2)

I A user program goes through several steps before being executed.

I Addresses may be represented in different ways during these steps.

• Source code addresses usually symbolic.

• A compiler typically binds these symbolic addresses to relocatableaddresses, e.g., 14 bytes from beginning of this module.

• Linker or loader will bind relocatable addresses to absoluteaddresses, e.g., 74014.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 10 / 47

Page 22: Main Memory - Part1

Address Binding (2/2)

I A user program goes through several steps before being executed.

I Addresses may be represented in different ways during these steps.

• Source code addresses usually symbolic.

• A compiler typically binds these symbolic addresses to relocatableaddresses, e.g., 14 bytes from beginning of this module.

• Linker or loader will bind relocatable addresses to absoluteaddresses, e.g., 74014.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 10 / 47

Page 23: Main Memory - Part1

Address Binding (2/2)

I A user program goes through several steps before being executed.

I Addresses may be represented in different ways during these steps.

• Source code addresses usually symbolic.

• A compiler typically binds these symbolic addresses to relocatableaddresses, e.g., 14 bytes from beginning of this module.

• Linker or loader will bind relocatable addresses to absoluteaddresses, e.g., 74014.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 10 / 47

Page 24: Main Memory - Part1

Address Binding (2/2)

I A user program goes through several steps before being executed.

I Addresses may be represented in different ways during these steps.

• Source code addresses usually symbolic.

• A compiler typically binds these symbolic addresses to relocatableaddresses, e.g., 14 bytes from beginning of this module.

• Linker or loader will bind relocatable addresses to absoluteaddresses, e.g., 74014.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 10 / 47

Page 25: Main Memory - Part1

Binding of Instructions and Data to Memory (1/3)

I Address binding of instructions and datato memory addresses can happen at threedifferent stages.

I Compile time: if memory location knowna priori, absolute code can be generated.

• Must recompile code if starting locationchanges.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 11 / 47

Page 26: Main Memory - Part1

Binding of Instructions and Data to Memory (1/3)

I Address binding of instructions and datato memory addresses can happen at threedifferent stages.

I Compile time: if memory location knowna priori, absolute code can be generated.

• Must recompile code if starting locationchanges.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 11 / 47

Page 27: Main Memory - Part1

Binding of Instructions and Data to Memory (2/3)

I Load time: must generate relocatable codeif memory location is not known at compiletime.

• Final binding is delayed until load time.• If the starting address changes, we need

only reload the user code to incorporatethis changed value.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 12 / 47

Page 28: Main Memory - Part1

Binding of Instructions and Data to Memory (3/3)

I Execution time: binding delayed untilrun time if the process can be movedduring its execution from one memorysegment to another.

• Need hardware support

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 13 / 47

Page 29: Main Memory - Part1

Logical vs. Physical Address Space (1/2)

I Logical address (virtual address): address generated by the CPU.• Logical address space is the set of all logical addresses generated by

a program.

I Physical address: address seen by the memory unit.• Physical address space is the set of all physical addresses generated

by a program.

I The concept of a logical address space that is bound to a separatephysical address space is central to proper memory management.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 14 / 47

Page 30: Main Memory - Part1

Logical vs. Physical Address Space (1/2)

I Logical address (virtual address): address generated by the CPU.• Logical address space is the set of all logical addresses generated by

a program.

I Physical address: address seen by the memory unit.• Physical address space is the set of all physical addresses generated

by a program.

I The concept of a logical address space that is bound to a separatephysical address space is central to proper memory management.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 14 / 47

Page 31: Main Memory - Part1

Logical vs. Physical Address Space (1/2)

I Logical address (virtual address): address generated by the CPU.• Logical address space is the set of all logical addresses generated by

a program.

I Physical address: address seen by the memory unit.• Physical address space is the set of all physical addresses generated

by a program.

I The concept of a logical address space that is bound to a separatephysical address space is central to proper memory management.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 14 / 47

Page 32: Main Memory - Part1

Logical vs. Physical Address Space (2/2)

I Logical and physical addresses are the same in compile-time andload-time address-binding schemes.

I Logical and physical addresses differ in execution-timeaddress-binding scheme.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 15 / 47

Page 33: Main Memory - Part1

Memory-Management Unit (MMU) (1/2)

I Hardware device that at run time maps virtual to physical address.

I As a simple scheme, the value in the relocation register is added toevery address generated by a user process at the time it is sent tomemory.

• Base register now called relocation register.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 16 / 47

Page 34: Main Memory - Part1

Memory-Management Unit (MMU) (1/2)

I Hardware device that at run time maps virtual to physical address.

I As a simple scheme, the value in the relocation register is added toevery address generated by a user process at the time it is sent tomemory.

• Base register now called relocation register.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 16 / 47

Page 35: Main Memory - Part1

Memory-Management Unit (MMU) (2/2)

I Two different types of addresses:• Logical addresses: range 0 to max• Physical addresses: range R + 0 to R + max for a base value R

I The user program generates only logical addresses and thinks thatthe process runs in locations 0 to max.

I These logical addresses must be mapped to physical addressesbefore they are used.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 17 / 47

Page 36: Main Memory - Part1

Memory-Management Unit (MMU) (2/2)

I Two different types of addresses:• Logical addresses: range 0 to max• Physical addresses: range R + 0 to R + max for a base value R

I The user program generates only logical addresses and thinks thatthe process runs in locations 0 to max.

I These logical addresses must be mapped to physical addressesbefore they are used.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 17 / 47

Page 37: Main Memory - Part1

Memory-Management Unit (MMU) (2/2)

I Two different types of addresses:• Logical addresses: range 0 to max• Physical addresses: range R + 0 to R + max for a base value R

I The user program generates only logical addresses and thinks thatthe process runs in locations 0 to max.

I These logical addresses must be mapped to physical addressesbefore they are used.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 17 / 47

Page 38: Main Memory - Part1

Dynamic Linking and Loading

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 18 / 47

Page 39: Main Memory - Part1

Dynamic Loading (1/2)

I Routine/library is not loaded until it is called.

I The main program is loaded into memory and is executed.

I When a routine is needed, if it has not been loaded, the loaderloads the desired routine into memory and to update the program’saddress tables to reflect this change.

I Then control is passed to the newly loaded routine.

I All routines kept on disk in relocatable load format.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 19 / 47

Page 40: Main Memory - Part1

Dynamic Loading (1/2)

I Routine/library is not loaded until it is called.

I The main program is loaded into memory and is executed.

I When a routine is needed, if it has not been loaded, the loaderloads the desired routine into memory and to update the program’saddress tables to reflect this change.

I Then control is passed to the newly loaded routine.

I All routines kept on disk in relocatable load format.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 19 / 47

Page 41: Main Memory - Part1

Dynamic Loading (1/2)

I Routine/library is not loaded until it is called.

I The main program is loaded into memory and is executed.

I When a routine is needed, if it has not been loaded, the loaderloads the desired routine into memory and to update the program’saddress tables to reflect this change.

I Then control is passed to the newly loaded routine.

I All routines kept on disk in relocatable load format.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 19 / 47

Page 42: Main Memory - Part1

Dynamic Loading (1/2)

I Routine/library is not loaded until it is called.

I The main program is loaded into memory and is executed.

I When a routine is needed, if it has not been loaded, the loaderloads the desired routine into memory and to update the program’saddress tables to reflect this change.

I Then control is passed to the newly loaded routine.

I All routines kept on disk in relocatable load format.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 19 / 47

Page 43: Main Memory - Part1

Dynamic Loading (1/2)

I Routine/library is not loaded until it is called.

I The main program is loaded into memory and is executed.

I When a routine is needed, if it has not been loaded, the loaderloads the desired routine into memory and to update the program’saddress tables to reflect this change.

I Then control is passed to the newly loaded routine.

I All routines kept on disk in relocatable load format.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 19 / 47

Page 44: Main Memory - Part1

Dynamic Loading (2/2)

I Better memory-space utilization; unused routine is never loaded.

I Useful when large amounts of code are needed to handleinfrequently occurring cases.

I No special support from the OS is required.

I Implemented through program design.

I OS can help by providing libraries to implement dynamic loading.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 20 / 47

Page 45: Main Memory - Part1

Dynamic Loading (2/2)

I Better memory-space utilization; unused routine is never loaded.

I Useful when large amounts of code are needed to handleinfrequently occurring cases.

I No special support from the OS is required.

I Implemented through program design.

I OS can help by providing libraries to implement dynamic loading.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 20 / 47

Page 46: Main Memory - Part1

Dynamic Loading (2/2)

I Better memory-space utilization; unused routine is never loaded.

I Useful when large amounts of code are needed to handleinfrequently occurring cases.

I No special support from the OS is required.

I Implemented through program design.

I OS can help by providing libraries to implement dynamic loading.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 20 / 47

Page 47: Main Memory - Part1

Dynamic Loading (2/2)

I Better memory-space utilization; unused routine is never loaded.

I Useful when large amounts of code are needed to handleinfrequently occurring cases.

I No special support from the OS is required.

I Implemented through program design.

I OS can help by providing libraries to implement dynamic loading.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 20 / 47

Page 48: Main Memory - Part1

Dynamic Loading (2/2)

I Better memory-space utilization; unused routine is never loaded.

I Useful when large amounts of code are needed to handleinfrequently occurring cases.

I No special support from the OS is required.

I Implemented through program design.

I OS can help by providing libraries to implement dynamic loading.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 20 / 47

Page 49: Main Memory - Part1

Dynamic Liking (1/2)

I Static linking: system libraries and program code combined by theloader into the binary program image.

I Dynamic linking: linking postponed until execution time.

I Stub: small piece of code, used to locate the appropriatememory-resident library routine.

I Stub replaces itself with the address of the routine, and executesthe routine.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 21 / 47

Page 50: Main Memory - Part1

Dynamic Liking (1/2)

I Static linking: system libraries and program code combined by theloader into the binary program image.

I Dynamic linking: linking postponed until execution time.

I Stub: small piece of code, used to locate the appropriatememory-resident library routine.

I Stub replaces itself with the address of the routine, and executesthe routine.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 21 / 47

Page 51: Main Memory - Part1

Dynamic Liking (1/2)

I Static linking: system libraries and program code combined by theloader into the binary program image.

I Dynamic linking: linking postponed until execution time.

I Stub: small piece of code, used to locate the appropriatememory-resident library routine.

I Stub replaces itself with the address of the routine, and executesthe routine.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 21 / 47

Page 52: Main Memory - Part1

Dynamic Liking (1/2)

I Static linking: system libraries and program code combined by theloader into the binary program image.

I Dynamic linking: linking postponed until execution time.

I Stub: small piece of code, used to locate the appropriatememory-resident library routine.

I Stub replaces itself with the address of the routine, and executesthe routine.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 21 / 47

Page 53: Main Memory - Part1

Dynamic Liking (2/2)

I OS checks if routine is in processes memory address.

I If not in address space, add to address space.

I Dynamic linking is particularly useful for shared libraries.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 22 / 47

Page 54: Main Memory - Part1

Dynamic Liking (2/2)

I OS checks if routine is in processes memory address.

I If not in address space, add to address space.

I Dynamic linking is particularly useful for shared libraries.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 22 / 47

Page 55: Main Memory - Part1

Dynamic Liking (2/2)

I OS checks if routine is in processes memory address.

I If not in address space, add to address space.

I Dynamic linking is particularly useful for shared libraries.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 22 / 47

Page 56: Main Memory - Part1

Swapping

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 23 / 47

Page 57: Main Memory - Part1

Swapping (1/3)

I A process can be swapped temporarily out of memory to a backingstore, and then brought back into memory for continued execution.

I Backing store: fast disk large enough to accommodate copies of allmemory images for all users; must provide direct access to thesememory images.

I Total memory space of processes can exceed physical memory.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 24 / 47

Page 58: Main Memory - Part1

Swapping (1/3)

I A process can be swapped temporarily out of memory to a backingstore, and then brought back into memory for continued execution.

I Backing store: fast disk large enough to accommodate copies of allmemory images for all users; must provide direct access to thesememory images.

I Total memory space of processes can exceed physical memory.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 24 / 47

Page 59: Main Memory - Part1

Swapping (1/3)

I A process can be swapped temporarily out of memory to a backingstore, and then brought back into memory for continued execution.

I Backing store: fast disk large enough to accommodate copies of allmemory images for all users; must provide direct access to thesememory images.

I Total memory space of processes can exceed physical memory.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 24 / 47

Page 60: Main Memory - Part1

Swapping (2/3)

I Does the swapped out process need to swap back in to samephysical addresses?

• Depends on address binding method.

I Pending I/O: cannot swap out as I/O would occur to wrongprocess.

• Or always transfer I/O to kernel space, then to I/O device.• Known as double buffering, adds overhead.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 25 / 47

Page 61: Main Memory - Part1

Swapping (2/3)

I Does the swapped out process need to swap back in to samephysical addresses?

• Depends on address binding method.

I Pending I/O: cannot swap out as I/O would occur to wrongprocess.

• Or always transfer I/O to kernel space, then to I/O device.• Known as double buffering, adds overhead.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 25 / 47

Page 62: Main Memory - Part1

Swapping (2/3)

I Does the swapped out process need to swap back in to samephysical addresses?

• Depends on address binding method.

I Pending I/O: cannot swap out as I/O would occur to wrongprocess.

• Or always transfer I/O to kernel space, then to I/O device.• Known as double buffering, adds overhead.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 25 / 47

Page 63: Main Memory - Part1

Swapping (2/3)

I Does the swapped out process need to swap back in to samephysical addresses?

• Depends on address binding method.

I Pending I/O: cannot swap out as I/O would occur to wrongprocess.

• Or always transfer I/O to kernel space, then to I/O device.• Known as double buffering, adds overhead.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 25 / 47

Page 64: Main Memory - Part1

Swapping (3/3)

I Standard swapping not used in modern OSs, but modified versionis common.

I Swap only when free memory extremely low.

I Disabled again once memory demand reduced below threshold.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 26 / 47

Page 65: Main Memory - Part1

Swapping Cost

I Major part of swap time is transfer time; which is proportional tothe amount of memory swapped.

I If next processes to be put on CPU is not in memory, need to swapout a process and swap in target process.

I Example:• 100MB process swapping to hard disk with transfer rate of

50MB/sec.• Swap out time of 2s + swap in of same sized process.• Total context switch swapping component time of 4s.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 27 / 47

Page 66: Main Memory - Part1

Swapping Cost

I Major part of swap time is transfer time; which is proportional tothe amount of memory swapped.

I If next processes to be put on CPU is not in memory, need to swapout a process and swap in target process.

I Example:• 100MB process swapping to hard disk with transfer rate of

50MB/sec.• Swap out time of 2s + swap in of same sized process.• Total context switch swapping component time of 4s.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 27 / 47

Page 67: Main Memory - Part1

Swapping Cost

I Major part of swap time is transfer time; which is proportional tothe amount of memory swapped.

I If next processes to be put on CPU is not in memory, need to swapout a process and swap in target process.

I Example:• 100MB process swapping to hard disk with transfer rate of

50MB/sec.• Swap out time of 2s + swap in of same sized process.• Total context switch swapping component time of 4s.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 27 / 47

Page 68: Main Memory - Part1

Swapping on Mobile Systems (1/2)

I Not typically supported.

I Flash memory based• Small amount of space• Limited number of write cycles• Poor throughput between flash memory and CPU on mobile

platform

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 28 / 47

Page 69: Main Memory - Part1

Swapping on Mobile Systems (1/2)

I Not typically supported.

I Flash memory based• Small amount of space• Limited number of write cycles• Poor throughput between flash memory and CPU on mobile

platform

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 28 / 47

Page 70: Main Memory - Part1

Swapping on Mobile Systems (2/2)

I Instead use other methods to free memory if low.

I iOS asks apps to voluntarily relinquish allocated memory.

I Read-only data thrown out and reloaded from flash if needed.

I Failure to free can result in termination.

I Android terminates apps if low free memory, but first writesapplication state to flash for fast restart.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 29 / 47

Page 71: Main Memory - Part1

Swapping on Mobile Systems (2/2)

I Instead use other methods to free memory if low.

I iOS asks apps to voluntarily relinquish allocated memory.

I Read-only data thrown out and reloaded from flash if needed.

I Failure to free can result in termination.

I Android terminates apps if low free memory, but first writesapplication state to flash for fast restart.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 29 / 47

Page 72: Main Memory - Part1

Swapping on Mobile Systems (2/2)

I Instead use other methods to free memory if low.

I iOS asks apps to voluntarily relinquish allocated memory.

I Read-only data thrown out and reloaded from flash if needed.

I Failure to free can result in termination.

I Android terminates apps if low free memory, but first writesapplication state to flash for fast restart.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 29 / 47

Page 73: Main Memory - Part1

Swapping on Mobile Systems (2/2)

I Instead use other methods to free memory if low.

I iOS asks apps to voluntarily relinquish allocated memory.

I Read-only data thrown out and reloaded from flash if needed.

I Failure to free can result in termination.

I Android terminates apps if low free memory, but first writesapplication state to flash for fast restart.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 29 / 47

Page 74: Main Memory - Part1

Swapping on Mobile Systems (2/2)

I Instead use other methods to free memory if low.

I iOS asks apps to voluntarily relinquish allocated memory.

I Read-only data thrown out and reloaded from flash if needed.

I Failure to free can result in termination.

I Android terminates apps if low free memory, but first writesapplication state to flash for fast restart.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 29 / 47

Page 75: Main Memory - Part1

Contiguous Memory Allocation

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 30 / 47

Page 76: Main Memory - Part1

Contiguous Allocation (1/2)

I Main memory must support both OS and user processes.

I Limited resource, must allocate efficiently.

I Contiguous allocation is one early method.

I Main memory usually into two partitions:• Resident OS, usually held in low memory with interrupt vector.• User processes then held in high memory.• Each process contained in single contiguous section of memory.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 31 / 47

Page 77: Main Memory - Part1

Contiguous Allocation (1/2)

I Main memory must support both OS and user processes.

I Limited resource, must allocate efficiently.

I Contiguous allocation is one early method.

I Main memory usually into two partitions:• Resident OS, usually held in low memory with interrupt vector.• User processes then held in high memory.• Each process contained in single contiguous section of memory.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 31 / 47

Page 78: Main Memory - Part1

Contiguous Allocation (1/2)

I Main memory must support both OS and user processes.

I Limited resource, must allocate efficiently.

I Contiguous allocation is one early method.

I Main memory usually into two partitions:• Resident OS, usually held in low memory with interrupt vector.• User processes then held in high memory.• Each process contained in single contiguous section of memory.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 31 / 47

Page 79: Main Memory - Part1

Contiguous Allocation (1/2)

I Main memory must support both OS and user processes.

I Limited resource, must allocate efficiently.

I Contiguous allocation is one early method.

I Main memory usually into two partitions:• Resident OS, usually held in low memory with interrupt vector.• User processes then held in high memory.• Each process contained in single contiguous section of memory.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 31 / 47

Page 80: Main Memory - Part1

Contiguous Allocation (2/2)

I Relocation registers used to protect user processes from eachother, and from changing OS code and data.

• Base register contains value of smallest physical address.• Limit register contains range of logical addresses.• MMU maps logical address dynamically.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 32 / 47

Page 81: Main Memory - Part1

Multiple-Partition Allocation (1/2)

I Memory is to divide memory into several fixed-sized partitions.

I Each partition may contain exactly one process.

I Degree of multiprogramming limited by number of partitions.

I When a partition is free, a process is selected from the input queueand is loaded into the free partition.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 33 / 47

Page 82: Main Memory - Part1

Multiple-Partition Allocation (1/2)

I Memory is to divide memory into several fixed-sized partitions.

I Each partition may contain exactly one process.

I Degree of multiprogramming limited by number of partitions.

I When a partition is free, a process is selected from the input queueand is loaded into the free partition.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 33 / 47

Page 83: Main Memory - Part1

Multiple-Partition Allocation (1/2)

I Memory is to divide memory into several fixed-sized partitions.

I Each partition may contain exactly one process.

I Degree of multiprogramming limited by number of partitions.

I When a partition is free, a process is selected from the input queueand is loaded into the free partition.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 33 / 47

Page 84: Main Memory - Part1

Multiple-Partition Allocation (1/2)

I Memory is to divide memory into several fixed-sized partitions.

I Each partition may contain exactly one process.

I Degree of multiprogramming limited by number of partitions.

I When a partition is free, a process is selected from the input queueand is loaded into the free partition.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 33 / 47

Page 85: Main Memory - Part1

Multiple-Partition Allocation (2/2)

I Variable-partition sizes for efficiency (sized to a given process’needs).

I Hole: block of available memory.

I When a process arrives, it is allocated memory from a hole largeenough to accommodate it.

I Process exiting frees its partition, adjacent free partitions combined

I OS maintains information about: allocated partitions and freepartitions (hole)

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 34 / 47

Page 86: Main Memory - Part1

Multiple-Partition Allocation (2/2)

I Variable-partition sizes for efficiency (sized to a given process’needs).

I Hole: block of available memory.

I When a process arrives, it is allocated memory from a hole largeenough to accommodate it.

I Process exiting frees its partition, adjacent free partitions combined

I OS maintains information about: allocated partitions and freepartitions (hole)

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 34 / 47

Page 87: Main Memory - Part1

Multiple-Partition Allocation (2/2)

I Variable-partition sizes for efficiency (sized to a given process’needs).

I Hole: block of available memory.

I When a process arrives, it is allocated memory from a hole largeenough to accommodate it.

I Process exiting frees its partition, adjacent free partitions combined

I OS maintains information about: allocated partitions and freepartitions (hole)

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 34 / 47

Page 88: Main Memory - Part1

Multiple-Partition Allocation (2/2)

I Variable-partition sizes for efficiency (sized to a given process’needs).

I Hole: block of available memory.

I When a process arrives, it is allocated memory from a hole largeenough to accommodate it.

I Process exiting frees its partition, adjacent free partitions combined

I OS maintains information about: allocated partitions and freepartitions (hole)

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 34 / 47

Page 89: Main Memory - Part1

Multiple-Partition Allocation (2/2)

I Variable-partition sizes for efficiency (sized to a given process’needs).

I Hole: block of available memory.

I When a process arrives, it is allocated memory from a hole largeenough to accommodate it.

I Process exiting frees its partition, adjacent free partitions combined

I OS maintains information about: allocated partitions and freepartitions (hole)

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 34 / 47

Page 90: Main Memory - Part1

Dynamic Storage-Allocation Problem

I How to satisfy a request of size n from a list of free holes?

I First-fit: allocate the first hole that is big enough

I Best-fit: allocate the smallest hole that is big enough• Must search entire list, unless ordered by size.• Produces the smallest leftover hole.

I Worst-fit: allocate the largest hole• Must also search entire list.• Produces the largest leftover hole.

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

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 35 / 47

Page 91: Main Memory - Part1

Dynamic Storage-Allocation Problem

I How to satisfy a request of size n from a list of free holes?

I First-fit: allocate the first hole that is big enough

I Best-fit: allocate the smallest hole that is big enough• Must search entire list, unless ordered by size.• Produces the smallest leftover hole.

I Worst-fit: allocate the largest hole• Must also search entire list.• Produces the largest leftover hole.

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

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 35 / 47

Page 92: Main Memory - Part1

Dynamic Storage-Allocation Problem

I How to satisfy a request of size n from a list of free holes?

I First-fit: allocate the first hole that is big enough

I Best-fit: allocate the smallest hole that is big enough• Must search entire list, unless ordered by size.• Produces the smallest leftover hole.

I Worst-fit: allocate the largest hole• Must also search entire list.• Produces the largest leftover hole.

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

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 35 / 47

Page 93: Main Memory - Part1

Dynamic Storage-Allocation Problem

I How to satisfy a request of size n from a list of free holes?

I First-fit: allocate the first hole that is big enough

I Best-fit: allocate the smallest hole that is big enough• Must search entire list, unless ordered by size.• Produces the smallest leftover hole.

I Worst-fit: allocate the largest hole• Must also search entire list.• Produces the largest leftover hole.

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

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 35 / 47

Page 94: Main Memory - Part1

Dynamic Storage-Allocation Problem

I How to satisfy a request of size n from a list of free holes?

I First-fit: allocate the first hole that is big enough

I Best-fit: allocate the smallest hole that is big enough• Must search entire list, unless ordered by size.• Produces the smallest leftover hole.

I Worst-fit: allocate the largest hole• Must also search entire list.• Produces the largest leftover hole.

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

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 35 / 47

Page 95: Main Memory - Part1

Fragmentation (1/3)

I External fragmentation: total memory space exists to satisfy arequest, but it is not contiguous.

I Internal fragmentation: allocated memory may be slightly largerthan requested memory; this size difference is memory internal to apartition, but not being used.

I First fit analysis reveals that given N blocks allocated, 0.5N blockslost to fragmentation: 50-percent rule

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 36 / 47

Page 96: Main Memory - Part1

Fragmentation (1/3)

I External fragmentation: total memory space exists to satisfy arequest, but it is not contiguous.

I Internal fragmentation: allocated memory may be slightly largerthan requested memory; this size difference is memory internal to apartition, but not being used.

I First fit analysis reveals that given N blocks allocated, 0.5N blockslost to fragmentation: 50-percent rule

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 36 / 47

Page 97: Main Memory - Part1

Fragmentation (1/3)

I External fragmentation: total memory space exists to satisfy arequest, but it is not contiguous.

I Internal fragmentation: allocated memory may be slightly largerthan requested memory; this size difference is memory internal to apartition, but not being used.

I First fit analysis reveals that given N blocks allocated, 0.5N blockslost to fragmentation: 50-percent rule

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 36 / 47

Page 98: Main Memory - Part1

Fragmentation (2/3)

I Compaction: a solution to the problem of external fragmentation.

I Shuffle memory contents to place all free memory together in onelarge block.

I Compaction is possible only if relocation is dynamic, and is done atexecution time.

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

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 37 / 47

Page 99: Main Memory - Part1

Fragmentation (2/3)

I Compaction: a solution to the problem of external fragmentation.

I Shuffle memory contents to place all free memory together in onelarge block.

I Compaction is possible only if relocation is dynamic, and is done atexecution time.

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

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 37 / 47

Page 100: Main Memory - Part1

Fragmentation (2/3)

I Compaction: a solution to the problem of external fragmentation.

I Shuffle memory contents to place all free memory together in onelarge block.

I Compaction is possible only if relocation is dynamic, and is done atexecution time.

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

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 37 / 47

Page 101: Main Memory - Part1

Fragmentation (2/3)

I Compaction: a solution to the problem of external fragmentation.

I Shuffle memory contents to place all free memory together in onelarge block.

I Compaction is possible only if relocation is dynamic, and is done atexecution time.

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

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 37 / 47

Page 102: Main Memory - Part1

Fragmentation (3/3)

I Another possible solution to the external fragmentation problem:permit the logical address space of the processes to benoncontiguous.

I Two techniques:• Segmentation• Paging

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 38 / 47

Page 103: Main Memory - Part1

Segmentation

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 39 / 47

Page 104: Main Memory - Part1

Fragmentation (2/2)

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

I A program is a collection of segments.

I A segment is a logical unit such as:• Main program• Procedure• Function• Object• ...

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 40 / 47

Page 105: Main Memory - Part1

Logical View of Segmentation

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 41 / 47

Page 106: Main Memory - Part1

Segmentation Architecture

I Logical address consists of a tuple: 〈segment number, offset〉

I Segment table: maps two-dimensional user-defined addresses intoone-dimensional physical address.

I Each table entry has:• Base: contains the starting physical address where the segments

reside in memory• Limit: specifies the length of the segment

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 42 / 47

Page 107: Main Memory - Part1

Segmentation Architecture

I Logical address consists of a tuple: 〈segment number, offset〉

I Segment table: maps two-dimensional user-defined addresses intoone-dimensional physical address.

I Each table entry has:• Base: contains the starting physical address where the segments

reside in memory• Limit: specifies the length of the segment

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 42 / 47

Page 108: Main Memory - Part1

Segmentation Architecture

I Logical address consists of a tuple: 〈segment number, offset〉

I Segment table: maps two-dimensional user-defined addresses intoone-dimensional physical address.

I Each table entry has:• Base: contains the starting physical address where the segments

reside in memory• Limit: specifies the length of the segment

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 42 / 47

Page 109: Main Memory - Part1

Segmentation Hardware

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 43 / 47

Page 110: Main Memory - Part1

Segmentation Example

I A reference to byte 53 of segment 2:

I A reference to byte 852 of segment 3:

I A reference to byte 1222 of segment 0:

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 44 / 47

Page 111: Main Memory - Part1

Segmentation Example

I A reference to byte 53 of segment 2: 4300 + 53 = 4353

I A reference to byte 852 of segment 3:

I A reference to byte 1222 of segment 0:

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 44 / 47

Page 112: Main Memory - Part1

Segmentation Example

I A reference to byte 53 of segment 2: 4300 + 53 = 4353

I A reference to byte 852 of segment 3:

I A reference to byte 1222 of segment 0:

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 44 / 47

Page 113: Main Memory - Part1

Segmentation Example

I A reference to byte 53 of segment 2: 4300 + 53 = 4353

I A reference to byte 852 of segment 3: 3200 + 852 = 4052

I A reference to byte 1222 of segment 0:

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 44 / 47

Page 114: Main Memory - Part1

Segmentation Example

I A reference to byte 53 of segment 2: 4300 + 53 = 4353

I A reference to byte 852 of segment 3: 3200 + 852 = 4052

I A reference to byte 1222 of segment 0:Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 44 / 47

Page 115: Main Memory - Part1

Segmentation Example

I A reference to byte 53 of segment 2: 4300 + 53 = 4353

I A reference to byte 852 of segment 3: 3200 + 852 = 4052

I A reference to byte 1222 of segment 0: trap to OSAmir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 44 / 47

Page 116: Main Memory - Part1

Summary

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 45 / 47

Page 117: Main Memory - Part1

Summary

I Main memory

I Address protection: base + limit

I Address binding: compile time, load time, execution time

I Logical and physical address, MMU

I Swapping: backing store, swapping cost

I Contiguous memory allocation: partitions, holes, first-fit, best-fit,worst-fit

I External and internal fragmentation: compaction, segmentation,paging

I Segmentation: noncontiguous address, user view of memory

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 46 / 47

Page 118: Main Memory - Part1

Summary

I Main memory

I Address protection: base + limit

I Address binding: compile time, load time, execution time

I Logical and physical address, MMU

I Swapping: backing store, swapping cost

I Contiguous memory allocation: partitions, holes, first-fit, best-fit,worst-fit

I External and internal fragmentation: compaction, segmentation,paging

I Segmentation: noncontiguous address, user view of memory

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 46 / 47

Page 119: Main Memory - Part1

Summary

I Main memory

I Address protection: base + limit

I Address binding: compile time, load time, execution time

I Logical and physical address, MMU

I Swapping: backing store, swapping cost

I Contiguous memory allocation: partitions, holes, first-fit, best-fit,worst-fit

I External and internal fragmentation: compaction, segmentation,paging

I Segmentation: noncontiguous address, user view of memory

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 46 / 47

Page 120: Main Memory - Part1

Summary

I Main memory

I Address protection: base + limit

I Address binding: compile time, load time, execution time

I Logical and physical address, MMU

I Swapping: backing store, swapping cost

I Contiguous memory allocation: partitions, holes, first-fit, best-fit,worst-fit

I External and internal fragmentation: compaction, segmentation,paging

I Segmentation: noncontiguous address, user view of memory

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 46 / 47

Page 121: Main Memory - Part1

Summary

I Main memory

I Address protection: base + limit

I Address binding: compile time, load time, execution time

I Logical and physical address, MMU

I Swapping: backing store, swapping cost

I Contiguous memory allocation: partitions, holes, first-fit, best-fit,worst-fit

I External and internal fragmentation: compaction, segmentation,paging

I Segmentation: noncontiguous address, user view of memory

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 46 / 47

Page 122: Main Memory - Part1

Summary

I Main memory

I Address protection: base + limit

I Address binding: compile time, load time, execution time

I Logical and physical address, MMU

I Swapping: backing store, swapping cost

I Contiguous memory allocation: partitions, holes, first-fit, best-fit,worst-fit

I External and internal fragmentation: compaction, segmentation,paging

I Segmentation: noncontiguous address, user view of memory

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 46 / 47

Page 123: Main Memory - Part1

Summary

I Main memory

I Address protection: base + limit

I Address binding: compile time, load time, execution time

I Logical and physical address, MMU

I Swapping: backing store, swapping cost

I Contiguous memory allocation: partitions, holes, first-fit, best-fit,worst-fit

I External and internal fragmentation: compaction, segmentation,paging

I Segmentation: noncontiguous address, user view of memory

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 46 / 47

Page 124: Main Memory - Part1

Summary

I Main memory

I Address protection: base + limit

I Address binding: compile time, load time, execution time

I Logical and physical address, MMU

I Swapping: backing store, swapping cost

I Contiguous memory allocation: partitions, holes, first-fit, best-fit,worst-fit

I External and internal fragmentation: compaction, segmentation,paging

I Segmentation: noncontiguous address, user view of memory

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 46 / 47

Page 125: Main Memory - Part1

Questions?

Acknowledgements

Some slides were derived from Avi Silberschatz slides.

Amir H. Payberah (Tehran Polytechnic) Main Memory 1393/8/5 47 / 47