Upload
dsweetalker
View
218
Download
0
Embed Size (px)
Citation preview
7/29/2019 Chapter 3-Memory Management
1/51
CHAPTER 3
MEMORY MANAGEMENT
CLO1: Analyzed the step process performed by operating systembased on management of memory, resource and file to ensure thecomputer system operates at optimum performance. (C4)
CLO2: Install and configure correctly workstation and domainserver using MS Windows server or Open Sources server operatingsystem.(P4)
7/29/2019 Chapter 3-Memory Management
2/51
Memory Management
Memory management Resident and transient routines Related terminologies Dynamic address translation Virtual memory
Processor management Control blocks and interrupts Types of scheduling processes
Scheduling algorithms Queuing routine and scheduler Multiprogramming and time sharing Deadlock
7/29/2019 Chapter 3-Memory Management
3/51
Scenario that evokes memorymanagement
Picture a computer with a single program in memory. Theprogram cannot process data it does not yet have, andsuccess cannot be assumed until an output operation is
finished, so the program waits for input or output. Since theprogram controls the computer, the computer waits, too.
Typically, given the speed disparity between the processorand its peripheral devices, a program spends far more timewaiting for I/O than processing data.
Why not put two programs in memory? Then, when program Ais waiting for data, the processor can turn its attention toprogram B. And why stop at two programs? With three,
even more otherwise wasted time is utilized .Generally,the more programs in memory, the greater the utilization of the
processor.
7/29/2019 Chapter 3-Memory Management
4/51
Memory Management
Memory managementis the act ofmanaging computer memory.
In its simpler forms, this involvesproviding ways to allocate portionsof memory to programs at theirrequest, and freeing it for reuse when
no longer needed.
The management of main memory iscritical to the computer system.
7/29/2019 Chapter 3-Memory Management
5/51
Memory Management
Memory management is done by memorymanager.
Main tasks are to:
Keep track of which parts of memory are in useand which parts are not in use
Allocate memory to processes when theyneed it and deallocate it when they are done
Manage swapping between main memoryand disk when main memory is too small tohold all the processes
7/29/2019 Chapter 3-Memory Management
6/51
Memory Management
The allocation of computer storage in amultiprogramming system so as tomaximize processing efficiency.
The collection of routines for placing,fetching, and removing pages orsegments into or out of the main memoryof a computer system.
7/29/2019 Chapter 3-Memory Management
7/51
Memory Management
Memory management systems onmulti-tasking operating systemsusually deal with the following issues:
Relocation
Protection
Sharing
Logical organization
Physical Organization
7/29/2019 Chapter 3-Memory Management
8/51
Relocation
In systems with virtual memory, programs in memory must beable to reside in different parts of the memory at differenttimes.
The program is swapped back into memory after beingswapped out for a while it can not always be placed in the
same location. Memory management in the operating system should
therefore be able to relocate programs in memory andhandle memory references and addresses in the code of theprogram so that they always point to the right location in
memory.
7/29/2019 Chapter 3-Memory Management
9/51
Protection
Processes should not be able toreference the memory for anotherprocess without permission.
This is called memory protection, andprevents malicious or malfunctioningcode in one program from interfering
with the operation of other runningprograms
7/29/2019 Chapter 3-Memory Management
10/51
Sharing
Even though the memory for differentprocesses is normally protected fromeach other, different processes
sometimes need to be able to shareinformation and therefore access thesame part of memory.
Shared memory is one of the fastesttechniques for Inter-processcommunication.
7/29/2019 Chapter 3-Memory Management
11/51
Logical organization
Programs are often organized in modules.
Some of these modules could be shared
between different programs, some are
read only and some contain data thatcan be modified.
The memory management is responsible
for handling this logical organization that is
different from the physical linear addressspace. One way to arrange this
organization is segmentation.
7/29/2019 Chapter 3-Memory Management
12/51
Physical Organization
Memory is usually divided into fastprimary storage and slow secondarystorage.
Memory management in theoperating system handles movinginformation between these two levels
of memory.
7/29/2019 Chapter 3-Memory Management
13/51
Memory Allocation
in a PC
7/29/2019 Chapter 3-Memory Management
14/51
Resident Routines
OS is a collection of software routines Resident routines (Rutin Tetap)
The part of a program that must remain in memory at all times.Instructions and data that remain in memory can be accessedinstantly.
A routine that stays in the memory eg routines that controlphysical I/O and directly support application programs as theyrun
In computing, a resident module orresident program stays inmemory throughout the lifetime of a computing session.
One such program might be an anti-virus program. This hasgiven rise to the term resident protection.
7/29/2019 Chapter 3-Memory Management
15/51
Transient Routines
Transient routines (Rutin Sementara)
A routine that is stored on diskloaded as needed
routines that format disks
7/29/2019 Chapter 3-Memory Management
16/51
Resident and Transient routines
The operating system occupieslow memory.
Key control information comes
first followed by the various
resident operating system
routines.
The transient area, is where
application programs andtransient operating system
routines are loaded.
SystemControl
Information
ResidentOperating
System
TransientArea
7/29/2019 Chapter 3-Memory Management
17/51
Terminologies related to memorymanagement :
Fixed partition memorymanagement
Dynamic memory management Segmentation
Paging
Memory management
Real memory Virtual memory
Fixed
Partition
Dynamic Segmentat
ion
Paging
7/29/2019 Chapter 3-Memory Management
18/51
Fixed partition memorymanagement
Memory is divided into n (possiblyunequal and fixed length) partitioneach of which holds one program
Partition sizes are set when the systemis initially started
So the memory allocation decision ismade before the actual amount ofspace needed by a given program isknown
7/29/2019 Chapter 3-Memory Management
19/51
Fixed partition memorymanagement
When a job arrives, it can be put intothe input queue for the smallestpartition large enough to hold it.
Since a partition is fixed in thisscheme, any space in a partition notused by a job is lost (internal
fragmentation)
7/29/2019 Chapter 3-Memory Management
20/51
Fixed partition memorymanagement
Major advantage is simplicity
Disadvantage of sorting the incoming jobs intoseparate queues is when the queue for a largepartition is empty but the queue of a smallpartition is full(Refer figure a)
Small jobs have to wait to get into memory, eventhough plenty of memory is free
Alternative of separate queues is to maintain asingle queue(Refer figure b)
i i i
7/29/2019 Chapter 3-Memory Management
21/51
Fixed partition memorymanagement
Whenever a partition becomes free,the job closest to the front of thequeue that fits in could be loaded
into the empty partition and run
Small partition is usually used to allowsmall jobs to run without having to
allocate large partition for themwhich will cause waste of partitionspace
i d titi
7/29/2019 Chapter 3-Memory Management
22/51
Fixed partition memorymanagement: Figure a
Partition 4
Partition 3
Partition 2
Partition 1
Operating
System
Multiple input queues800K
700K
400K
200K
100K
0K
Fi d titi
7/29/2019 Chapter 3-Memory Management
23/51
Fixed partition memorymanagement: Figure b
Partition 4
Partition 3
Partition 2
Partition 1
Operating
System
Single input queue
800K
700K
400K
200K
100K
0K
7/29/2019 Chapter 3-Memory Management
24/51
Dynamic memory management
Transient area is treated as a pool ofunstructured free space.
When the system loads a particularprogram, a region of memory justsufficient to hold the program isallocated from the pool.
Because a program gets only thespace it needs, relatively little spaceis wasted
7/29/2019 Chapter 3-Memory Management
25/51
Dynamic memory management
Disadvantage :
does not completely solve the wasted
space problems. (Refer figure c)
D i t
7/29/2019 Chapter 3-Memory Management
26/51
Dynamic memory management:Figure c
Operating
System
Other Regions
250K Region
300K Region
Unused 90K
Fragment
Other Regions
Example, a 640K program hasjust finished executing. If thereare no 640K programs
available, the system mightload a 250K and a 300Kprograms, but note that 90Kremains unallocated.
If there are no 90K or smallerprograms available, the spacewill not be used. This will lead toexternalfragmentation .
7/29/2019 Chapter 3-Memory Management
27/51
Segmentation: Figure d
Programs aredivided into
independentlyaddressedsegments andstored in
noncontiguousmemory
(Refer Figure d)
Operating Systems
Other Programs
Program A, segment 0
Other Programs
Program A, segment 1
Other Programs
Program A, segment 2
Other Programs
7/29/2019 Chapter 3-Memory Management
28/51
Segmentation:
Different segments may and usually do
have different lengths
Segments length may changed during
execution
7/29/2019 Chapter 3-Memory Management
29/51
Segmentation:
requires adding a step to the addresstranslation process.
When a program is loaded intomemory, the operating system buildsa segment table listing the (absolute)entry point address of each of the
programs segments (Refer figure e)
7/29/2019 Chapter 3-Memory Management
30/51
Segmentation:
When the operating system starts a given
program, it loads the address of that
programs segment table into a special
register. As the program runs, addresses must be
translated from relative to absolute form
because programmers still write the same
code and compilers still generate base-plus-displacement addresses.
7/29/2019 Chapter 3-Memory Management
31/51
Segmentation
After fetching an instruction, theinstruction control unit expands eachoperand address by adding the base
register and the displacement.
Traditionally, the expanded addresswas an absolute address.
7/29/2019 Chapter 3-Memory Management
32/51
Segmentation
The expanded address consists oftwo parts:
a segment number
a displacement
Dynamic Address Translation willconvert the segment/displacement
address to an absolute address.
7/29/2019 Chapter 3-Memory Management
33/51
Absolute Address.
The term absolute distinguishes it from arelativeaddress, which indicates a location byspecifying a distance from another location.Absolute addresses are also calledreal
addresses and machine addresses.
7/29/2019 Chapter 3-Memory Management
34/51
Segmentation: Figure e
Segme
nt
Start
addre
ss0 Aaa
1 Bbb
2 Ccc
Segment 0
Segment 1
Segment 2
Segment Displaceme
nt
Base Displacement
Segment start address
plus displacement equals
absolute segment
address
Expand
7/29/2019 Chapter 3-Memory Management
35/51
Paging
a program is broken into fixed-lengthpages
Page size is generally small (perhaps2K to 4K)
Like segments, a programs pagesare loaded into noncontiguous
memory.
7/29/2019 Chapter 3-Memory Management
36/51
Paging
Addresses consist of two parts :
a page number in the high-order
positions
a displacement in the low-order bits.
Addresses are dynamically translatedas the program runs.
7/29/2019 Chapter 3-Memory Management
37/51
Paging
When an instruction is fetched, itsbase-plus displacement addressesare expanded to absolute addresses
by hardware.
Then the pages base address islooked up in a program page table
(like the segment table, maintainedby the operating system) and addedto the displacement.
7/29/2019 Chapter 3-Memory Management
38/51
Paging: Figure f
Page Startaddress
0 aaa
1 bbb
2 ccc
Page 0
Page 1
Page 2
Page Displaceme
nt
Base Displaceme
nt
Segment start address
plus displacement
equals absolute pageaddress
Expand
7/29/2019 Chapter 3-Memory Management
39/51
Dynamic Address Translation
If, while executing an instruction, a CPU fetches aninstruction located at a particular virtual address, or fetchesdata from a specific virtual address or stores data to aparticular virtual address, the virtual address must betranslated to the corresponding physical address. This isdone by a hardware component, sometimes called a
memory management unit, which looks up the realaddress (from the page table) corresponding to a virtualaddress and passes the real address to the parts of the CPUwhich execute instructions. If the page tables indicate thatthe virtual memory page is not currently in real memory,the hardware raises a page faultexception (specialinternal signal) which invokes the paging supervisor
component of the operating system.
http://en.wikipedia.org/wiki/CPUhttp://en.wikipedia.org/wiki/Memory_management_unithttp://en.wikipedia.org/wiki/Memory_management_unithttp://en.wikipedia.org/wiki/CPUhttp://en.wikipedia.org/wiki/CPUhttp://en.wikipedia.org/wiki/Page_faulthttp://en.wikipedia.org/wiki/Exceptionhttp://en.wikipedia.org/wiki/Page_faulthttp://en.wikipedia.org/wiki/Exceptionhttp://en.wikipedia.org/wiki/Operating_systemhttp://en.wikipedia.org/wiki/Operating_systemhttp://en.wikipedia.org/wiki/Operating_systemhttp://en.wikipedia.org/wiki/Exceptionhttp://en.wikipedia.org/wiki/Page_faulthttp://en.wikipedia.org/wiki/CPUhttp://en.wikipedia.org/wiki/Memory_management_unithttp://en.wikipedia.org/wiki/CPU7/29/2019 Chapter 3-Memory Management
40/51
Dynamic Address Translation
Steps to convert the
segment/displacement address to an
absolute address, hardware:
1. checks the special register to find the
programs segment table,
2. extracts the segment number from the
expanded address,3. uses the segment number to search the
programs segment table,
7/29/2019 Chapter 3-Memory Management
41/51
Dynamic Address Translation
4. finds the segments absolute entry point
address, and
5. adds the displacement to the entry point
address to get an absolute address
7/29/2019 Chapter 3-Memory Management
42/51
Virtual Memory
splits up a program into small modular
pieces
a technique that allows the execution of
processes that may not be completely in
memory.
ie, the process can start executing with only
part of the process image in the memory.
7/29/2019 Chapter 3-Memory Management
43/51
Virtual Memory
Advantage :
the program can be larger than the physical
memory.
You can run more applications at once.
You can run larger applications with less real
RAM.
Applications may launch faster because ofFile Mapping.
You don't have to buy more memory(RAM).
7/29/2019 Chapter 3-Memory Management
44/51
Virtual Memory
Applications run slower.
It takes more time to switch between
applications. Less hard drive space for your use.
Reduced system stability.
7/29/2019 Chapter 3-Memory Management
45/51
Virtual Memory
It shows three levels of storagevirtual
memory, the external paging device, and
real memory.
Real memory is good, old-fashioned
main memory, directly addressable by
the processor.
7/29/2019 Chapter 3-Memory Management
46/51
Virtual Memory
The external paging device is usually
disk, model that simplifies address
translation.
Virtual memory is a model that simplifies
address translation. It contains the
operating system and all the application
programs, but it does not physically existanywhere
7/29/2019 Chapter 3-Memory Management
47/51
Virtual Memory
A computer system
technique, which
gives an application
program theimpression that it
has contiguous
working memory(an address space),
while in fact it may
be physically
7/29/2019 Chapter 3-Memory Management
48/51
Virtual Memory
Consists of 3 main parts :
Main memory (RAM)
Secondary memory(disk)
Page Table
7/29/2019 Chapter 3-Memory Management
49/51
Virtual MemoryMain Memory(RAM)
Store the more recently used pages.
Each page is stored into subdivisions ofmemory called "frames." A frame size is
the same as page size. If we have a 4096 byte page size, then each
frame will hold 4096 bytes to accommodatethe pages.
The first frame will take up actualaddresses 0x00000000 to 0x00000FFF.The second frame will take up address
0x00001000 to 0x00001FFF. Etc. If
7/29/2019 Chapter 3-Memory Management
50/51
Virtual MemoryDisk
The disk memory is a repository for
pages not currently in use. When a page
needs to be brought to memory, the
appropriate page is found andtransferred to main memory. Whenever a
page that has been modified during its
time in main memory, it is written to disk.Think of the pages on the disk as being
in long term storage.
7/29/2019 Chapter 3-Memory Management
51/51
Virtual MemoryPage Table
keeps track of where pages are, and whattheir properties are. The system updatesthe page table as changes in the state ofthe system warrant. It makes sense that
there are as many entries in the page tableas there are pages in our virtual addressspace.
Therefore, in a virtual address space
addressed by 32 bits, at 4K per page that's220 pages, and hence 220 entries in thepage table. The first entry in the page tablecontains information about the first page.The second entry contains information