M4
M4
CHAPTER 8
MEMORY MANAGEMENT STRATEGIES
➢ Background
• Basic Hardware
✓ Main memory and the registers in the processor are the only storage that the
CPU can access directly. Hence the program and data must be brought from disk
into main memory for CPU to access.
✓ Registers can be accessed in one CPU clock cycle. But main memory access can
take many CPU clock cycles.
✓ A fast memory called cache is placed between main memory and CPU registers.
✓ We must ensure correct operation to protect the operating system from access
by user processes and also to protect user processes from one another. This
protection must be provided by the hardware. It can be implemented in several
ways and one such possible implementation is,
o We first need to make sure that each process has a separate memory space.
o To do this, we need the ability to determine the range of legal addresses that
the process may access and to ensure that the process can access only these
legal addresses.
o We can provide this protection by using two registers, a base and a limit, as
illustrated in below figure.
o The base register holds the smallest legal physical memory address; the
limit register specifies the size of the range. For example, if the base
register holds 300040 and the limit register is 120900, then the program can
legally access all addresses from 300040 through 420940.
✓ Protection of memory space is accomplished by having the CPU hardware
compare every address generated in user mode with the registers.
✓ Any attempt by a program executing in user mode to access operating-system
memory or other users' memory results in a trap to the operating system, which
treats the attempt as a fatal error as shown in below figure.
✓ This prevents a user program from (accidentally or deliberately) modifying the
code or data structures of either the operating system or other users.
✓ The base and limit registers uses a special privileged instructions which can be
executed only in kernel mode, and since only the operating system executes in
kernel mode, only the operating system can load the base and limit registers.
• Address Binding
✓ Programs are stored on the secondary storage disks as binary executable files.
✓ When the programs are to be executed they are brought in to the main memory
and placed within a process.
✓ The collection of processes on the disk waiting to enter the main memory forms
the input queue.
✓ One of the processes which are to be executed is fetched from the queue and is
loaded into main memory.
✓ During the execution it fetches instruction and data from main memory. After the
process terminates it returns back the memory space.
✓ During execution the process will go through several steps as shown in below
figure. and in each step the address is represented in different ways.
✓ In source program the address is symbolic. The compiler binds the symbolic
address to re-locatable address. The loader will in turn bind this re-locatable
address to absolute address.
✓ Binding of instructions and data to memory addresses can be done at any step
along the way:
o Compile time: If we know at compile time where the process resides in
memory, then absolute code can be generated. For example, if we know that
a user process will reside starting at location R, then the generated compiler
code will start at that location and extend up from there. If, at some later time,
the starting location changes, then it will be necessary to recompile this code.
o Load time: If it is not known at compile time where the process will reside in
memory, then the compiler must generate relocatable code. In this case, final
binding is delayed until load time.
o Execution time: If the process is moved during its execution from one
memory segment to another then the binding is delayed until run time. Special
hardware is used for this. Most of the general purpose operating system uses
this method.
✓ The address generated by the CPU is called logical address or virtual address.
✓ The address seen by the memory unit i.e., the one loaded in to the memory
register is called the physical address.
✓ Compile time and load time address binding methods generate same logical and
physical address. The execution time addressing binding generate different
logical and physical address.
✓ Set of logical address space generated by the programs is the logical address
space. Set of physical address corresponding to these logical addresses is the
physical address space.
✓ The mapping of virtual address to physical address during run time is done by the
hardware device called Memory Management Unit (MMU).
✓ The base register is now called re-location register.
✓ Value in the re-location register is added to every address generated by the user
process at the time it is sent to memory as shown in below figure.
✓ For example, if the base is at 14000, then an attempt by the user to address
location 0 is dynamically relocated to location 14000; an access to location 346 is
mapped to location 14346. The user program never sees the real physical
addresses.
• Dynamic Loading
✓ For a process to be executed it should be loaded in to the physical memory. The
size of the process is limited to the size of the physical memory. Dynamic loading
is used to obtain better memory utilization.
✓ In dynamic loading the routine or procedure will not be loaded until it is called.
✓ Whenever a routine is called, the calling routine first checks whether the called
routine is already loaded or not. If it is not loaded it calls the loader to load the
desired program in to the memory and updates the programs address table to
indicate the change and control is passed to newly invoked or called routine.
✓ The advantages are ,
o Gives better memory utilization.
o Unused routine is never loaded.
o Do not need special operating system support.
o Useful when large amount of codes are needed to handle infrequently
occurring cases, such as error routines.
✓ In dynamic linking only the main program is loaded in to the memory. If the main
program requests a procedure, the procedure is loaded and the link is established
at the time of references. This linking is postponed until the execution time.
✓ With dynamic linking a “stub” is used in the image of each library referenced
routine. A “stub” is a piece of code which is used to indicate how to locate the
appropriate memory resident library routine or how to load library if the
routine is not already present.
✓ When “stub” is executed it checks whether the routine is present is memory or
not. If not it loads the routine in to the memory.
✓ This feature can be used to update libraries i.e., library is replaced by a new
version and all the programs can make use of this library.
✓ More than one version of the library can be loaded in memory at a time and each
program uses its version of the library. Only the program that is compiled with the
new version is affected by the changes incorporated in it. Other programs linked
before new version was installed will continue using older library. This system is
called “shared libraries”.
➢ Swapping
✓ A process can be swapped temporarily out of the memory to a backing store and
then brought back in to the memory for continuing the execution. This process is
called swapping. Ex. In a multi-programming environment with a round robin CPU
scheduling whenever the time quantum expires then the process that has just finished
is swapped out and a new process swaps in to the memory for execution as shown in
below figure.
✓ A variant of this swapping policy is priority based scheduling. When a low priority is
executing and if a high priority process arrives then a low priority will be swapped
out and high priority is allowed for execution. This process is also called as Roll out
and Roll in.
✓ Normally the process which is swapped out will be swapped back to the same
memory space that is occupied previously and this depends upon address binding.
✓ The system maintains a ready queue consisting of all the processes whose memory
images are on the backing store or in memory and are ready to run.
✓ The context-switch time in a swapping system is high. For ex, assume that the user
process is 10 MB in size and the backing store is a standard hard disk with a transfer
rate of 40MB per second. The actual transfer of the 40MB process to or from main
memory takes,
10MB (10000KB)/40MB (40000KB) per second
=1/4 second
=250 milliseconds
✓ Assuming an average latency of 8 milliseconds, the swap time is 258 milliseconds.
Since we must both swap out and swap in, the total swap time is about 516
milliseconds.
✓ Swapping is constrained by other factors,
o To swap a process, it should be completely idle.
o If a process is waiting for an I/O operation, then the process cannot be swapped.
✓ Relocation registers are used to protect user processes from each other, and to
protect from changing OS code and data.
✓ The relocation register contains the value of the smallest physical address and the
limit register contains the range of logical addresses.
✓ With relocation and limit registers, each logical address must be less than the limit
register.
✓ The MMU maps the logical address dynamically by adding the value in the
relocation register. This mapped address is sent to main memory as shown in
below figure.
✓ The relocation-register scheme provides an effective way to allow the operating
system's size to change dynamically.
Limit
Register
Relocation
Register
Logical
< Memory
Dr.HN ,CPU
Dr.B Mrs. AMR Dept of ISE, Page 6
Operating Systems (BCS303) III-ISE, MODULE-IV
Address Physical
Yes Address
No
Trap: Addressing error
• Memory Allocation
• Fragmentation
process requests 18,462 bytes. If we allocate exactly the requested block, we are
left with a hole of 2 bytes.
✓ The overhead to keep track of this hole will be substantially larger than the hole
itself.
✓ The general approach to avoid this problem is to break the physical memory into
fixed-sized blocks and allocate memory in units based on block size. With this
approach, the memory allocated to a process may be slightly larger than the
requested memory.
✓ The difference between these two numbers is internal fragmentation that is
internal to a partition.
✓ One solution to over-come external fragmentation is compaction. The goal is to
move all the free memory together to form a large block. Compaction is possible
only if the re-location is dynamic and done at execution time.
✓ Another solution to the external fragmentation problem is to permit the logical
address space of a process to be non-contiguous.
➢ Paging
✓ Paging is a memory management scheme that permits the physical address space of
a process to be non-contiguous. Support for paging is handled by hardware.
✓ Paging avoids the considerable problem of fitting the varying sized memory chunks
on to the backing store.
• Basic Method
✓ Physical memory is broken in to fixed sized blocks called frames (f) and Logical
memory is broken in to blocks of same size called pages (p).
✓ When a process is to be executed its pages are loaded in to available frames from
the backing store. The backing store is also divided in to fixed-sized blocks of
same size as memory frames.
✓ The below figure. Shows paging hardware.
✓ Logical address generated by the CPU is divided in to two parts: page number
(p) and page offset (d).
✓ The page number (p) is used as index to the page table. The page table contains
base address of each page in physical memory. This base address is combined
with the page offset to define the physical memory i.e., sent to the memory unit.
The paging model memory is shown in below figure.
✓ The page size is defined by the hardware. The size is the power of 2, varying
between 512 bytes and 16Mb per page.
✓ If the size of logical address space is 2m address unit and page size is 2n, then high
order m-n designates the page number and n low order bits represents page
offset. Thus logic address is as follows.
page number page offset
p d
m-n n
✓ Ex: To show how to map logical memory in to physical memory, consider a page
size of 4 bytes and physical memory of 32 bytes (8 pages) as shown in below
figure.
a. Logical address 0 is page 0 and offset 0 and Page 0 is in frame 5. The logical
address 0 maps to physical address [(5*4) + 0]=20.
b. Logical address 3 is page 0 and offset 3 and Page 0 is in frame 5. The logical
address 3 maps to physical address [(5*4) + 3]= 23.
c. Logical address 4 is page 1 and offset 0 and page 1 is mapped to frame 6. So
logical address 4 maps to physical address [(6*4) + 0]=24.
d. Logical address 13 is page 3 and offset 1 and page 3 is mapped to frame 2.
So logical address 13 maps to physical address [(2*4) + 1]=9.
✓ For example, if page size is 2,048 bytes, a process of 72,766 bytes will need 35
pages plus 1,086 bytes. It will be allocated 36 frames, resulting in internal
fragmentation of 2,048 - 1,086= 962 bytes.
✓ When a process arrives in the system to be executed, its size expressed in pages is
examined. Each page of the process needs one frame. Thus, if the process requires
n pages, at least n frames must be available in memory. If n frames are available,
they are allocated to this arriving process.
✓ The first page of the process is loaded in to one of the allocated frames, and the
frame number is put in the page table for this process. The next page is loaded
into another frame and its frame number is put into the page table and so on, as
shown in below figure. (a) before allocation, (b) after allocation.
• Hardware Support
✓ The hardware implementation of the page table can be done in several ways. The
simplest method is that the page table is implemented as a set of dedicated registers.
✓ The use of registers for the page table is satisfactory if the page table is reasonably
small (for example, 256 entries). But most computers, allow the page table to be very
large (for example, 1 million entries) and for these machines, the use of fast registers
to implement the page table is not feasible.
✓ So the page table is kept in the main memory and a page table base register (PTBR)
points to the page table and page table length register (PTLR) indicates size of page
table. Here two memory accesses are needed to access a byte and thus memory access
is slowed by a factor of 2.
✓ The only solution is to use a special, fast lookup hardware cache called Translation
look aside buffer (TLB). TLB is associative, with high speed memory. Each entry in
TLB contains two parts, a key and a value. When an associative register is presented
with an item, it is compared with all the key values, if found the corresponding value
field is returned. Searching is fast but hardware is expensive.
✓ Some TLBs store Address Space Identifiers (ASIDs) in each TLB entry. An ASID
uniquely identifies each process and is used to provide address-space protection for
that process.
✓ The percentage of time that a page number is found in the TLB is called hit ratio.
✓ For example, an 80-percent hit ratio means that we find the desired page number in
the TLB 80 percent of the time. If it takes 20 nanoseconds to search the TLB and 100
nanoseconds to access memory, then a mapped-memory access takes 120
nanoseconds when the page number is in the TLB. If we fail to find the page
number in the TLB (20 nanoseconds), then we must first access memory for the page
table and frame number (100 nanoseconds) and then access the desired byte in
memory (100 nanoseconds), for a total of 220 nanoseconds. Thus the effective access
time is,
Effective Access Time (EAT) = 0.80 x 120 + 0.20 x 220
= 140 nanoseconds.
• Protection
• Shared Pages
• Hierarchical paging
✓ Recent computer system support a large logical address apace from 232 to 264
and thus page table becomes large. So it is very difficult to allocate contiguous
main memory for page table. One simple solution to this problem is to divide
page table in to smaller pieces.
✓ One way is to use two-level paging algorithm in which the page table itself is
also paged as shown in below figure.
✓ Ex. In a 32- bit machine with page size of 4kb, a logical address is divided in
to a page number consisting of 20 bits and a page offset of 12 bit. The page
table is further divided since the page table is paged, the page number is
further divided in to 10 bit page number and a 10 bit offset. So the logical
address is,
Page number page offset
P1 P2 d
10 10 12
✓ 1
P is an index into the outer page table and P2 is the displacement within the
page of the outer page table. The address-translation method for this
architecture is shown in below figure. Because address translation works from
the outer page table inward, this scheme is also known as a forward-mapped
page table.
✓ For a system with a 64-bit logical address space, a two-level paging scheme is
no longer appropriate. Suppose the page size in such a system is 4 KB the
page table consists of up to 252 entries. If we use a two-level paging scheme,
then the inner page tables can be one page long, or contain 210 4-byte entries.
The addresses look like this,
Outer page inner page offset
P1 P2 d
42 10 12
✓ The outer page table consists of 242 entries, or 244 bytes. The one way to avoid
such a large table is to divide the outer page table into smaller pieces.
✓ We can avoid such a large table using three-level paging scheme.
✓ The outer page table is still 234 bytes in size. The next step would be a four-
level paging scheme.
✓ Hashed page table handles the address space larger than 32 bit. The virtual
page number is used as hash value. Linked list is used in the hash table which
contains a list of elements that hash to the same location.
✓ Each element in the hash table contains the following three fields,
o Virtual page number
o Mapped page frame value
o Pointer to the next element in the linked list
✓ The algorithm works as follows,
o Virtual page number is taken from virtual address and is hashed in to the
hash table.
o Virtual page number is compared with field 1 in the first element in the
linked list.
o If there is a match, the corresponding page frame (field 2) is used to form
the desired physical address. If there is no match, subsequent entries in the
linked list are searched for a matching virtual page number. This scheme
is shown in below figure.
o Clustered pages are similar to hash table but one difference is that each
entity in the hash table refer to several pages.
✓ Page tables may consume large amount of physical memory just to keep track
of how other physical memory is being used.
✓ To solve this problem, we can use an inverted page table has one entry for
each real page (or frame) of memory. Each entry consists of the virtual
address of the page stored in that real memory location with information about
the process that owns the page.
✓ Thus, only one page table is in the system, and it has only one entry for each
page of physical memory.
✓ The inverted page table entry is a pair <process-id, page number>. Where
process-id assumes the role of the address-space identifier. When a
memory reference is made, the part of virtual address consisting of <process-
id, page number> is presented to memory sub-system.
✓ The inverted page table is searched for a match. If a match is found at entry i,
then the physical address <i, offset> is generated. If no match is found then an
illegal address access has been attempted.
✓ This scheme decreases the amount of memory needed to store each page
table, but increases the amount of time needed to search the table when a page
reference occurs.
➢ Segmentation
• Basic method
• Hardware
✓ For example, consider the below figure. We have five segments numbered from
0 through 4. Segment 2 is 400 bytes long and begins at location 4300. Thus, a
reference to byte 53 of segment 2 is mapped onto location 4300 +53= 4353. A
reference byte 852 of segment 3, is mapped to 3200 (the base of segment 3) + 852
= 4052. A reference to byte 1222 of segment 0 would result in a trap to the
operating system, as this segment is only 1,000 bytes long.
➢ Background
✓ Anexamination of real programs shows us that, in many cases, the entire program is
not needed to be in physical memory to get executed.
✓ Even in those cases where the entire program is needed, it may not need all to be at
the same time.
✓ The ability to execute a program that is only partially in memory wouldconfer many
benefits,
o A program will not be limited by the amount of physical memory that is
available.
o More than one program can run at the same time which can increase the
throughput and CPU utilization.
o Less I/O operation is needed to swap or load user program in to memory. So
each user program could run faster.
✓ Virtual memory involves the separation of user’s logical memory from physical
memory. This separation allows an extremely large virtual memory to be provided
for programmers when there is small physical memory as shown in below figure.
✓ The virtual address space of a process refers to the logical (or virtual) viewof how a
process is stored in memory.
✓ In below figure.We allow heap to grow upward in memoryas it is used for dynamic
memory allocation andstack togrow downward in memory through successive
function calls.
✓ The large blankspace (or hole) between the heap and the stack is part of the virtual
addressspace but will require actual physical pages only if the heap or stack grows.
✓ Virtual address spaces that include holes are known as sparse address spaces.
➢ Demand Paging
• Basic concepts
✓ We need some form of hardware support to distinguish between the pages that are
in memory and the pages that are on the disk.
✓ The valid-invalid bit scheme can provide this .If the bit is valid then the page is both
legal and is in memory.If the bit is invalid then either the page is not valid or is valid
but is currently on the disk.
✓ The page-table entry for a page that is brought into memory is set as valid but the
page-table entry for a page that is not currently in memory is either simply marked
invalid or contains the address of the page on diskas shown in below figure.
✓ Access to the page which is marked as invalid causes a page fault trap.
✓ The step for handling page fault is straight forward and is shown in below figure.
1. We check the internal table of the process to determine whether the reference
made is valid or invalid.
2. If invalid, terminate the process. If valid, then the page is not yet loaded and we
now page it in.
3. We find a free frame.
4. We schedule disk operation to read the desired page in to newly allocated frame.
5. When disk read is complete, we modify the internal table kept with the process to
indicate that the page is now in memory.
6. We restart the instruction which was interrupted by the trap. The process can now
access the page.
✓ In extreme cases, we can start executing the process without pages in memory. When
the OS sets the instruction pointer of process which is not in memory, it generates a
page fault. After this page is brought in to memory the process continues to execute,
faulting every time until every page that it needs is in memory. This scheme is known
as pure demand paging. That is, it never brings the page in to memory until it is
required.
✓ The hardware support for demand paging is same as paging and swapping.
o Page table: It has the ability to mark an entry invalid through valid-invalid bit.
o Secondary memory: This holds the pages that are not present in main memory. It
is a high speed disk.It is known as the swap device, and the section of disk used
for this purpose isknown as swap space.
✓ Demand paging can have significant effect on the performance of the computer
system.
✓ Let us compute the effective access time for a demand-paged memory.
✓ The memory-access time, denoted ma, ranges from 10 to 200 nanoseconds. As long
as we have no page faults, the effective access time is equal to the memory access
time.
✓ If a page fault occurs, we must first read the relevant page from disk and then access
the desired word.
✓ Letpbe the probability of a page fault (0<=p<=1).The effective access timeis then,
✓ To compute the effective access time, we must know how much time isneeded to
service a page fault. A page fault causes the following sequence tooccur,
➢ Copy-on-write
✓ Copy-on-write technique allows both the parent and the child processes to share the
same pages. These pages are marked as copy-on-write pages i.e., if either process
writes to a shared page, a copy of shared page is created.
✓ Copy-on-write is illustrated in below figure 1 and figure 2, which shows the contents
of the physical memory before and after process 1 modifies page C.
figure-1
figure-2
✓ For Ex: If a child process tries to modify a page containing portions of the stack, the
OS recognizes them as a copy-on-write page and create a copy of this page and maps
it on to the address space of the child process. So the child process will modify its
copied page and not the page belonging to parent.
✓ The new pages are obtained from the pool of free pages.Operating systems allocate
these pages using a technique known aszero-fill-on-demand. Zero-fill-on-demand
pages have been zeroed-out beforebeing allocated, thus erasing the previous
contents.
➢ Page Replacement
✓ The OS has several optionslike, it could terminatethe user processor instead swap
out a process, freeing all its frames and thus reduce the level of multiprogramming.
✓ If frame is not free, we find one that is not currently being used and free it. We
can free a frame by writing its contents to swap space and changing the page table
to indicate that the page is no longer in memory as shown in below figure. We
can now usethe freed frame to hold the page for which the process faulted.
Thepage-fault service routineis modifiedto include page replacement,
1. Find the location of derived page on the disk.
2. Find a free frame
a. If there is a free frame, use it.
b. Otherwise, use a replacement algorithm to select a victim.
c. Write the victim page to the disk; change the page and frame tables
accordingly.
3. Read the desired page into the free frame and change the page and frame
tables.
4. Restart the user process.
✓ The page that is supported out of physical memory is called victim page.
✓ If no frames are free, the two page transfers (one out and one in) are required.
This will doubles the page-fault service time and increases the effective access
time.
✓ This overhead can be reduced by usingmodify (dirty) bit. Each page or frame
may have modify (dirty) bit associated with it. The modify bit for a page is set by
the hardware whenever any word or byte in the page is written into, indicating
that the page has been modified.
✓ When we select the page for replacement, we check the modify bit. If the bit is
set, then the page is modified since it was read from the disk.
✓ If the bit was not set, the page has not been modified since it was read into
memory. Therefore, we can avoid writing the memory page to the disk as it is
already there.
✓ We must solve two major problems to implement demand paging i.e., we must
develop a frame allocation algorithm and a page replacement algorithm. If we
have multiple processes in memory, we must decide how many frames to allocate
to each processand when page replacement is needed. We must select the frames
that are to be replaced.
✓ There are manydifferent page-replacement algorithms.We want the one with the
lowestpage-fault rate.
✓ An algorithm is evaluatedby running it on a particular string of
memoryreferences called a reference stringand computing the number of page
faults.
✓ We replace the queue at the head of the queue. When a page is brought into
memory, we insert it at the tail of the queue.
✓ For example,consider the following reference string with 3 frames initially
empty.
✓ The first three references (7, 0, 1) causes page faults and are brought into the
empty frames.
✓ The next reference 2 replaces page 7 because the page 7 was brought in first.
✓ Since 0 is the next reference and 0 is already in memory we have no page faultfor
this reference.
✓ The next reference 3 replaces page 0 so but the next reference to 0 causer page
fault. Page 1 is then replaced by page 0.
✓ This will continue till the end of stringas shown in below figure. and there are 15
faults all together.
✓ For some page replacement algorithm, the page fault may increase as the
number of allocated frames increases. This is called as Belady’s Anamoly. FIFO
replacement algorithm may face this problem.
✓ The first three references cause faults that fill the three empty frames.
✓ The references to page 2 replaces page 7, because 7 will not be used until
reference 18.
✓ The page 0 will be used at 5 and page 1 at 14.
✓ With only 9 page faults, optimal replacement is much better than a FIFO, which
had 15 faults.
✓ This algorithm is difficult t implement because it requires future knowledge of
reference strings.
✓ When reference to page 4 occurs, LRU sees that of the three frames, page 2 as
used least recently. The most recently used page is page 0 and just before page 3
was used.
✓ The LRU policy is often used as a page replacement algorithm and considered to
be good. The main problem to how to implement LRU is the LRU requires
additional hardware assistance.
✓ Two implementation are possible:
o Counters: In this we associate each page table entry a time -of -use field,
and add to the cpu a logical clock or counter. The clock is incremented for
each memory reference.When a reference to a page is made, the contents of
the clock register are copied to the time-of-use field in the page table entry for
that page.In this way we have the time of last reference to each page we
replace the page with smallest time value. The time must also be maintained
when page tables are changed.
✓ An LRU page replacement algorithm should update the page removal status
information after every page reference updating is done by software, cost
increases.
✓ But hardware LRU mechanism tend to degrade execution performance at the
same time, then substantially increases the cost. For this reason, simple and
efficient algorithm that approximation the LRU have been developed. With
hardware support the reference bit was used. A reference bit associate with each
memory block and this bit automatically set to 1 by the hardware whenever the
page is referenced. The single reference bit per clock can be used to approximate
LRU removal.
✓ The page removal software periodically resets the reference bit to 0, write the
execution of the users job causes some reference bit to be set to 1.
✓ If the reference bit is 0 then the page has not been referenced since the last time
the reference bit was set to 0.
▪ Additional-Reference-Bits Algorithm
▪ Second-Chance Algorithm
✓ There is many other algorithms that can be used for page replacement, we can
keep a counter of the number of references that has made to a page.
✓ This causes the page with the smallest count to be replaced. The reason for
this selection is that actively used page should have a large reference
count.
✓ This algorithm suffers from the situation in which a page is used heavily
during the initial phase of a process but never used again. Since it was
used heavily, it has a large count and remains in memory even though it is
no longer needed.
o Most Frequently Used(MFU)
✓ This is based on the principle that the page with the smallest count was
probably just brought in and has yet to be used.
• Allocation of Frames
✓ The allocation policy in a virtual memory controls the operating system decision
regarding the amount of real memory to be allocated to each active process.
✓ In a paging system if more real pages are allocated, it reduces the page fault
frequency and improved turnaround throughput.
✓ If too few pages are allocated to a process its page fault frequency and turnaround
times may deteriorate to unacceptable levels.
✓ The minimum number of frames per process is defined by the architecture, and
the maximum number of frames. This scheme is called equal allocation.
✓ With multiple processes competing for frames, we can classify page replacement
into two broad categories
o Local Replacement: requires that each process selects frames from only its
own sets of allocated frame.
o Global Replacement: allows a process to select frame from the set of all
frames. Even if the frame is currently allocated to some other process, one
process can take a frame from another.
✓ In local replacement the number of frames allocated to a process do not change
but with global replacement number of frames allocated to a process do not
change global replacement results in greater system throughput.
Other consideration
There is much other consideration for the selection of a replacement algorithm and allocation
policy.
1) Preparing: This is an attempt to present high level of initial paging. This strategy is to
bring into memory all the pages at one time.
2) TLB Reach: The TLB reach refers to the amount of memory accessible from the TLB
and is simply the no of entries multiplied by page size.
3) Page Size: following parameters are considered
a) page size us always power of 2 (from 512 to 16k)
b) Internal fragmentation is reduced by a small page size.
c) A large page size reduces the number of pages needed.
4) Invented Page table: This will reduces the amount of primary memory i,e. needed to
track virtual to physical address translations.
5) Program Structure: Careful selection of data structure can increases the locality and
hence lowers the page fault rate and the number of pages in working state.
6) Real time Processing: Real time system almost never has virtual memory. Virtual
memory is the antithesis of real time computing, because it can introduce unexpected
long term delay in the execution of a process.
➢ Thrashing
➔ If the number of frames allocated to a low-priority process falls below the minimum
number required by the computer architecture then we suspend the process execution.
➔ A process is thrashing if it is spending more time in paging than executing.
➔ If the processes do not have enough number of frames, it will quickly page fault.
During this it must replace some page that is not currently in use. Consequently it
quickly faults again and again. The process continues to fault, replacing pages for
which it then faults and brings back. This high paging activity is called thrashing. The
phenomenon of excessively moving pages back and forth b/w memory and secondary
has been called thrashing.
Cause of Thrashing
• Thrashing results in severe performance problem.
• The operating system monitors the cpu utilization is low. We increase the degree
of multi programming by introducing new process to the system.
• A global page replacement algorithm replaces pages with no regards to the
process to which they belong.
The figure shows the thrashing
➔ As the degree of multi programming increases, more slowly until a
maximum is reached. If the degree of multi programming is increased
further thrashing sets in and the cpu utilization drops sharply.
Locality of Reference:
• As the process executes it moves from locality to locality.
• A locality is a set of pages that are actively used.
• A program may consist of several different localities, which may overlap.
• Locality is caused by loops in code that find to reference arrays and other data structures
by indices.
The ordered list of page number accessed by a program is called reference string.
Locality is of two types
1) spatial locality
2) temporal locality
“the collection of pages that a process is working with and which must be resident if the process
to avoid thrashing”.
The idea is to use the recent needs of a process to predict its future reader.
The working set is an approximation of programs locality.
Ex: given a sequence of memory reference, if the working set window size to memory
references, then working set at time t1 is {1,2,5,6,7} and at t2 is changed to {3,4}
• At any given time, all pages referenced by a process in its last 4 seconds of execution are
considered to compromise its working set.
• A process will never execute until its working set is resident in main memory.
• Pages outside the working set can be discarded at any movement.
Working sets are not enough and we must also introduce balance set.
a) If the sum of the working sets of all the run able process is greater than the size of
memory the refuse some process for a while.
b) Divide the run able process into two groups, active and inactive. The collection of active
set is called the balance set. When a process is made active its working set is loaded.
c) Some algorithm must be provided for moving process into and out of the balance set.
As a working set is changed, corresponding change is made to the balance set. Working set
presents thrashing by keeping the degree of multi programming as high as possible. Thus if
optimizes the CPU utilization. The main disadvantage of this is keeping track of the working
set.
1a. Solution:
2. For the following page reference calculate the page faults that occur using FIFO and LRU for
3 and 4 page frames respectively, 5,4,2,1,4,3,5,4,3,2,1,5. (10 marks) Jan 2015
Solution:
3 Frames (FIFO) ---------10 pagefaults
3 3 3 4 4 4 2 2
4 4 4 1 1 1 5 5 5
5 5 5 2 2 2 3 3 3 1
2 2 2 2 3 3 3
3 3 3 3 4 4 4 4
4 4 4 4 5 5 5 5 1
5 5 5 5 1 1 1 1 2 2
3
5
1
2
3 3 3 4 4 4 4 1
4 4 4 1 1 1 5 2 2
5 5 5 2 2 2 3 3 3 3
1
2
5
2 2 5 5 1 1
3 3 3 3 3 3 3
4 4 4 4 4 4 4 5
5 5 5 5 1 1 2 2 2
Question Bank
1. Write a note on virtual memory.
12. Explain bit vector, linked list, grouping and counting methods.
2. For page reference string : 1,2,3,4,2,1,5,6,2,1,2,3,7,6,3,2,1,2,3,6, how many page faults would
occur for LRU and optimal alg. Assuming 2 and 6 frames. (10) Dec 07/Jan 08
3. What is the cause of thrashing? How does system detect thrashing? (4) Dec 07/Jan 08, Dec
08/Jan 09, Dec 09/ Jan 10, June 2011.
4. Differentiate between internal and external fragmentation. How are they overcome? (4)
5. With diagram, discuss steps involved in handling a page fault. (6) Dec 09/ Jan 10
6. Mention the problem with simple paging scheme. How TLB is used to solve this problem?
Explain with supporting h/w dig with example. (Dec 2012)
7. Prove that Belady’s anomaly exists for the following reference string 1,2,3,4,1,2,5,1,2,3,4,5
using FIFO algorithm when numbers of frames used are 3 and 4. (8 marks) Jan 2015
8. Draw and explain the multistep processing of a user program. (8 marks)Jan 2015
10. What is a file? Describe different access methods on files. (4) Dec 08/Jan 09/Dec 2012
12. Draw neat diagram and explain fixed file allocation. Is FAT linked allocation? (9) Dec08/Jan
09
13. Explain following: file types, file operations, file attributes (12) Dec 09/ Jan 10
14. Explain methods to implement directories (8) Dec 09/ Jan 10/Jun 11
15. What is free space list? With example, explain any two methods to implement free space list.
(8) Dec2010
16. What are major methods to allocate disk space? Explain each with examples. (12) Dec 2010
19. Explain different disk space allocation methods with example. (8) June 2011, 2014
20. Explain the various storage mechanisms available to store files, with neat diagram.(8)
Jan 2015.
21. What is file? Explain in detail different allocation methods.(8) June/July 2016
22. What are directories? List different type’s directory structures with example. Mention their
advantages and disadvantages.(8) June/July 2016
27. List any five typical file attributes and any five file operations indicating their purpose in one
line each. (10)Dec2016/Jan2017
28. Briefly explain the methods of keeping track of free space on disks.(10)Dec2016/Jan2017
29. What is the principle behind paging? Explain its operation, clearly indicating how the
30. A hypothetical main memory can store only 3 frames simultaneously. The sequence which
the pages will be required is given below:
7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1 (Twenty operations).
Indicate the sequence in which the three frames will be filled in i) FIFO II) Optimal page
replacement and iii) Least recently used methods of page replacement. Indicate number of
page faults in each case. (10)Dec2016/Jan2017