@vtucode - In-2022-Scheme-Module-4-3rd semester-CSE
@vtucode - In-2022-Scheme-Module-4-3rd semester-CSE
MODULE: 4
NUMBER OF HOURS: 08
CONTENTS:
❖ Memory Management:
• Background
• Swapping
• Paging
• Segmentation
• Background
• Demand Paging
• Copy-on-write
• Page Replacement
• Allocation of Frames
• Trashing
❖ WEB RESOURCES:
• https://vtucode.in
Operating Systems BCS303
21CS44
MODULE 4
MEMORY MANAGEMENT
Main Memory Management Strategies
m
• In multi-tasking OS memory management is complex, because as processes are swapped
in and out of the CPU, their code and data must be swapped in and out of memory.
o
Basic Hardware
. c
• Main memory, cache and CPU registers in the processors are the only storage spaces
that CPU can access directly.
e
• The program and data must be bought into the memory from the disk, for the process to
run. Each process has a separate memory space and must access only this range of legal
d
addresses. Protection of memory is required to ensure correct operation. This prevention
is provided by hardware implementation.
• Two registers are used - a base register and a limit register. The base register holds the
o
smallest legal physical memory address; the limit register specifies the size of the range.
c
• For example, 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
u
300040 and limit register is 120900, then the program can legally access all addresses
t
from 300040 through 420940 (inclusive).
v
Figure: A base and a limit-register define a logical-address space
• The base and limit registers can be loaded only by the operating system, which uses a
special privileged instruction. Since privileged instructions can be executed only in
kernel mode only the operating system can load the base and limit registers.
1
20
BCS303
Operating Systems 21CS44
o m
c
Figure: Hardware address protection with base and limit-registers
.
Address Binding
e
• User programs typically refer to memory addresses with symbolic names. These symbolic
names must be mapped or bound to physical memory addresses.
d
• Address binding of instructions to memory-addresses can happen at 3 different stages.
o
1. Compile Time - If it is known at compile time where a program will reside in physical
memory, then absolute code can be generated by the compiler, containing actual
c
physical addresses. However, if the load address changes at some later time, then the
program will have to be recompiled.
u
2. Load Time - If the location at which a program will be loaded is not known at compile
t
time, then the compiler must generate relocatable code, which references addresses
relative to the start of the program. If that starting address changes, then the program
v
must be reloaded but not recompiled.
3. Execution Time - If a program can be moved around in memory during the course of its
execution, then binding must be delayed until execution time.
2
21
Operating Systems BCS303
21CS44
o m
. c
d e
Figure: Multistep processing of a user program
o
Logical Versus Physical Address Space
c
• The address generated by the CPU is a logical address, whereas the memory address
u
where programs are actually stored is a physical address.
• The set of all logical addresses used by a program composes the logical address space,
t
and the set of all corresponding physical addresses composes the physical address space.
• The run time mapping of logical to physical addresses is handled by the memory-
v
management unit (MMU).
• One of the simplest is a modification of the base-register scheme.
• The base register is termed a relocation register
• The value in the relocation-register is added to every address generated by a
user-process at the time it is sent to memory.
• The user-program deals with logical-addresses; it never sees the real physical-
addresses.
223
Operating Systems BCS303
21CS44
Dynamic Loading
• This can be used to obtain better memory-space utilization.
• A routine is not loaded until it is called.
m
2. Firstly, the main-program is loaded into memory and is executed.
3. When a main-program calls the routine, the main-program first checks to see whether the
routine has been loaded.
o
4. If routine has been not yet loaded, the loader is called to load desired routine into
memory.
c
5. Finally, control is passed to the newly loaded-routine.
.
Advantages:
1. An unused routine is never loaded.
e
2. Useful when large amounts of code are needed to handle infrequently occurring cases.
3. Although the total program-size may be large, the portion that is used (and hence loaded)
d
may be much smaller.
4. Does not require special support from the OS.
o
Dynamic Linking and Shared Libraries
c
• With static linking library modules get fully included in executable modules, wasting
u
both disk space and main memory usage, because every program that included a certain
t
routine from the library would have to have their own copy of that routine linked into
their executable code.
v
• With dynamic linking, however, only a stub is linked into the executable module,
containing references to the actual library module linked in at run time.
• The stub is a small piece of code used to locate the appropriate memory-resident
library-routine.
• This method saves disk space, because the library routines do not need to be fully
included in the executable modules, only the stubs.
• An added benefit of dynamically linked libraries (DLLs, also known as shared
libraries or shared objects on UNIX systems) involves easy upgrades and updates.
Shared libraries
• A library may be replaced by a new version, and all programs that reference the library
will automatically use the new one.
• Version info. is included in both program & library so that programs won't accidentally
execute incompatible versions.
234
Operating Systems BCS303
21CS44
Swapping
m
another process from backing store to memory. Swapping is a very slow process
compared to other operations.
o
• A variant of swapping policy is used for priority-based scheduling algorithms. If a
higher-priority process arrives and wants service, the memory manager can swap out the
c
lower-priority process and then load and execute the higher-priority process. When the
.
higher-priority process finishes, the lower-priority process can be swapped back in and
continued. This variant of swapping is called roll out, roll in.
e
Swapping depends upon address-binding:
d
• If binding is done at load-time, then process cannot be easily moved to a different
location.
• If binding is done at execution-time, then a process can be swapped into a different
o
memory-space, because the physical-addresses are computed during execution-time.
c
Major part of swap-time is transfer-time; i.e. total transfer-time is directly proportional to the
u
amount of memory swapped.
t
Disadvantages:
v
1. Context-switch time is fairly high.
2. If we want to swap a process, we must be sure that it is completely idle.
Two solutions:
i) Never swap a process with pending I/O.
ii) Execute I/O operations only into OS buffers.
245
Operating Systems BCS303
21CS44
Example:
Assume that the user process is 10 MB in size and the backing store is a standard hard disk with
a transfer rate of 40 MB per second.
The actual transfer of the 10-MB process to or from main memory takes
10000 KB/40000 KB per second = 1/4 second
= 250 milliseconds.
Assuming that no head seeks are necessary, and assuming an average latency of 8 milliseconds,
m
the swap time is 258 milliseconds. Since we must both swap out and swap in, the total swap
time is about 516 milliseconds.
c o
.
• The main memory must accommodate both the operating system and the various user
e
processes. Therefore we need to allocate the parts of the main memory in the most
efficient way possible.
d
• Memory is usually divided into 2 partitions: One for the resident OS. One for the user
processes.
o
• Each process is contained in a single contiguous section of memory.
c
1. Memory Mapping and Protection
u
• Memory-protection means protecting OS from user-process and protecting user-
t
processes from one another.
v
• Memory-protection is done using
o Relocation-register: contains the value of the smallest physical-address.
o Limit-register: contains the range of logical-addresses.
• 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 memory
• When the CPU scheduler selects a process for execution, the dispatcher loads the
relocation and limit-registers with the correct values.
• Because every address generated by the CPU is checked against these registers, we can
protect the OS from the running-process.
• The relocation-register scheme provides an effective way to allow the OS size to change
dynamically.
• Transient OS code: Code that comes & goes as needed to save memory-space and
overhead for unnecessary swapping.
256
BCS303
Operating Systems 21CS44
o m
c
Figure: Hardware support for relocation and limit-registers
2. Memory Allocation
e .
d
Two types of memory partitioning are:
1. Fixed-sized partitioning
o
2. Variable-sized partitioning
c
1. Fixed-sized Partitioning
u
The memory is divided into fixed-sized partitions.
•
t
Each partition may contain exactly one process.
• The degree of multiprogramming is bound by the number of partitions.
v
• When a partition is free, a process is selected from the input queue and loaded into the
free partition.
• When the process terminates, the partition becomes available for another process.
2. Variable-sized Partitioning
• The OS keeps a table indicating which parts of memory are available and which parts are
occupied.
• A hole is a block of available memory. Normally, memory contains a set of holes of
various sizes.
• Initially, all memory is available for user-processes and considered one large hole.
• When a process arrives, the process is allocated memory from a large hole.
• If we find the hole, we allocate only as much memory as is needed and keep the
remaining memory available to satisfy future requests.
267
Operating Systems BCS303
21CS44
Three strategies used to select a free hole from the set of available holes:
1. First Fit: Allocate the first hole that is big enough. Searching can start either at the
beginning of the set of holes or at the location where the previous first-fit search ended.
2. Best Fit: Allocate the smallest hole that is big enough. We must search the entire list,
unless the list is ordered by size. This strategy produces the smallest leftover hole.
m
3. Worst Fit: Allocate the largest hole. Again, we must search the entire list, unless it is
o
sorted by size. This strategy produces the largest leftover hole.
c
First-fit and best fit are better than worst fit in terms of decreasing time and storage utilization.
3. Fragmentation
e .
d
Two types of memory fragmentation:
1. Internal fragmentation
o
2. External fragmentation
c
1. Internal Fragmentation
• The general approach is to break the physical-memory into fixed-sized blocks and
u
allocate memory in units based on block size.
• The allocated-memory to a process may be slightly larger than the requested-memory.
t
• The difference between requested-memory and allocated-memory is called internal
v
fragmentation i.e. Unused memory that is internal to a partition.
2. External Fragmentation
• External fragmentation occurs when there is enough total memory-space to satisfy a
request but the available-spaces are not contiguous. (i.e. storage is fragmented into a
large number of small holes).
• Both the first-fit and best-fit strategies for memory-allocation suffer from external
fragmentation.
• Statistical analysis of first-fit reveals that given N allocated blocks, another 0.5 N blocks
will be lost to fragmentation. This property is known as the 50-percent rule.
278
Operating Systems 21CS44
BCS303
Paging
m
• Recent designs: The hardware & OS are closely integrated.
c o
• The basic method for implementing paging involves breaking physical memory into
.
fixed-sized blocks called frames and breaking logical memory into blocks of the same
size called pages.
• When a process is to be executed, its pages are loaded into any available memory frames
e
from the backing store.
• The backing store is divided into fixed-sized blocks that are of the same size as the
d
memory frames.
c o
t u
v Figure 1: Paging hardware
289
BCS303
Operating Systems 21CS44
o m
. c
Figure 2: Paging model of logical and physical memory.
e
• The page size (like the frame size) is defined by the hardware.
d
• The size of a page is typically a power of 2, varying between 512 bytes and 16 MB per
page, depending on the computer architecture.
o
• The selection of a power of 2 as a page size makes the translation of a logical address
into a page number and page offset.
c
• If the size of logical address space is 2m and a page size is 2n addressing units (bytes or
words), then the high-order m – n bits of a logical address designate the page number,
u
and the n low-order bits designate the page offset.
t
Thus, the logical address is as follows:
v
page number page offset
p d
m -n n
• When a process requests memory (e.g. when its code is loaded in from disk), free frames
are allocated from a free-frame list, and inserted into that process's page table.
• Processes are blocked from accessing anyone else's memory because all of their memory
requests are mapped through their page table. There is no way for them to generate an
address that maps into any other process's memory space.
• The operating system must keep track of each individual process's page table, updating it
whenever the process's pages get moved in and out of memory, and applying the correct
page table when processing system calls for a particular process. This all increases the
overhead involved when swapping processes in and out of the CPU.
10
29
Operating Systems 21CS44
BCS303
o m
. c
e
Figure: Free frames (a) before allocation and (b) after allocation.
Hardware Support
o d
c
Translation Look aside Buffer
u
• A special, small, fast lookup hardware cache, called a translation look-aside buffer
t
(TLB).
• Each entry in the TLB consists of two parts: a key (or tag) and a value.
v
• When the associative memory is presented with an item, the item is compared with all
keys simultaneously. If the item is found, the corresponding value field is returned. The
search is fast; the hardware, however, is expensive. Typically, the number of entries in a
TLB is small, often numbering between 64 and 1,024.
• The TLB contains only a few of the page-table entries.
Working:
• When a logical-address is generated by the CPU, its page-number is presented to the
TLB.
• If the page-number is found (TLB hit), its frame-number is immediately available and
used to access memory
• If page-number is not in TLB (TLB miss), a memory-reference to page table must be
made. The obtained frame-number can be used to access memory (Figure 1)
11
30
BCS303
Operating Systems 21CS44
o m
. c
e
Figure 1: Paging hardware with TLB
d
• In addition, we add the page-number and frame-number to the TLB, so that they will be
o
found quickly on the next reference.
• If the TLB is already full of entries, the OS must select one for replacement.
c
• Percentage of times that a particular page-number is found in the TLB is called hit ratio.
u
Advantage: Search operation is fast.
t
Disadvantage: Hardware is expensive.
v
• Some TLBs have wired down entries that can't be removed.
• Some TLBs store ASID (address-space identifier) in each entry of the TLB that uniquely
identify each process and provide address space protection for that process.
Protection
12
31
Operating Systems BCS303
21CS44
o m
. c
d e
c o
t u
Figure: Valid (v) or invalid (i) bit in a page-table
v
Shared Pages
Disadvantage:
Systems that use inverted page-tables have difficulty implementing shared-memory.
13
32
Operating Systems BCS303
21CS44
o m
. c
e
Figure: Sharing of code in a paging environment
o d
c
Structure of the Page Table
u
The most common techniques for structuring the page table:
1. Hierarchical Paging
t
2. Hashed Page-tables
v
3. Inverted Page-tables
1. Hierarchical Paging
• Problem: Most computers support a large logical-address space (232 to 264). In these
systems, the page-table itself becomes excessively large.
• Solution: Divide the page-table into smaller pieces.
14
33
Operating Systems BCS303
21CS44
o m
c
Figure: A two-level page-table scheme
.
For example:
Consider the system with a 32-bit logical-address space and a page-size of 4 KB.
e
A logical-address is divided into
→ 20-bit page-number and
d
→ 12-bit page-offset.
Since the page-table is paged, the page-number is further divided into
→ 10-bit page-number and
o
→ 10-bit page-offset.
c
Thus, a logical-address is as follows:
t u
v
• where p1 is an index into the outer page table, and p2 is the displacement within the
page of the inner 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.
15
34
Operating Systems 21CS44
BCS303
m
2. Value of the mapped page-frame and
3. Pointer to the next element in the linked-list.
o
The algorithm works as follows:
c
• The virtual page-number is hashed into the hash-table.
.
• The virtual page-number is compared with the first element in the linked-list.
• If there is a match, the corresponding page-frame (field 2) is used to form the desired
e
physical-address.
• If there is no match, subsequent entries in the linked-list are searched for a matching
d
virtual page-number.
c o
t u
v Figure: Hashed page-table
16
35
Operating Systems 21CS44
BCS303
o m
.
Figure: Inverted page-table
c
e
The algorithm works as follows:
d
1. When a memory-reference occurs, part of the virtual-address, consisting of <process-id,
page-number>, is presented to the memory subsystem.
2. The inverted page-table is then searched for a match.
o
3. If a match is found, at entry i-then the physical-address <i, offset> is generated.
c
4. If no match is found, then an illegal address access has been attempted.
Advantage:
u
1. Decreases memory needed to store each page-table
vt
Disadvantages:
1. Increases amount of time needed to search table when a page reference occurs.
2. Difficulty implementing shared-memory
Segmentation
17
36
BCS303
Operating Systems 21CS44
o m
. c
Figure: Programmer’s view of a program
e
Hardware support for Segmentation
• Segment-table maps 2 dimensional user-defined addresses into one-dimensional physical
d
addresses.
• In the segment-table, each entry has following 2 fields:
o
1. Segment-base contains starting physical-address where the segment resides in
memory.
c
2. Segment-limit specifies the length of the segment (Figure 2).
• A logical-address consists of 2 parts:
u
1. Segment-number(s) is used as an index to the segment-table
t
2. Offset(d) must be between 0 and the segment-limit.
• If offset is not between 0 & segment-limit, then we trap to the OS(logical-addressing
v
attempt beyond end of segment).
• If offset is legal, then it is added to the segment-base to produce the physical-memory
address.
MODULE 4
VIRTUAL MEMORYMANAGEMENT
• Virtual memory is a technique that allows for the execution of partially loaded process.
• Advantages:
▪ A program will not be limited by the amount of physical memory that is available
user can able to write in to large virtual space.
▪ Since each program takes less amount of physical memory, more than one
om
program could be run at the same time which can increase the throughput and
CPU utilization.
▪ Less i/o operation is needed to swap or load user program in to memory. So each
user program could run faster.
c
e.
od
uc
• Virtual memory is the separation of users logical memory from physical memory. This
separation allows an extremely large virtual memory to be provided when these is less
physical memory.
• Separating logical memory from physical memory also allows files and memory to be
shared by several different processes through page sharing.
219
BCS303
Operating Systems 21CS44
om
Fig: Shared Library using Virtual Memory
c
• Virtual address space: Every process has a virtual address space i.e used as the stack or
heap grows in size.
e.
od
uc
vt
DEMAND PAGING
• A demand paging is similar to paging system with swapping when we want to execute a
process we swap the process the in to memory otherwise it will not be loaded in to
memory.
• A swapper manipulates the entire processes, where as a pager manipulates individual
pages of the process.
▪ Bring a page into memory only when it is needed
▪ Less I/O needed
▪ Less memory needed
▪ Faster response
320
Operating Systems BCS303
21CS44
▪ More users
▪ Page is needed ⇒ reference to it
▪ invalid reference ⇒abort
▪ not-in-memory ⇒ bring to memory
▪ Lazy swapper– never swaps a page into memory unless page will be needed
▪ Swapper that deals with pages is a pager.
c om
e.
Fig: Transfer of a paged memory into continuous disk space
od
• Basic concept: Instead of swapping the whole process the pager swaps only the necessary
pages in to memory. Thus it avoids reading unused pages and decreases the swap time and
amount of physical memory needed.
• The valid-invalid bit scheme can be used to distinguish between the pages that are on the disk
and that are in memory.
uc
• During address translation, if valid–invalid bit in page table entry is I ⇒ page fault.
• If the bit is valid then the page is both legal and is in memory.
• If the bit is invalid then either page is not valid or is valid but is currently on the disk. Marking
421
Operating Systems 21CS44
BCS303
a page as invalid will have no effect if the processes never access to that page. Suppose if it
access the page which is marked invalid, causes a page fault trap. This may result in failure of
OS to bring the desired page in to memory.
c om
e.
Fig: Page Table when some pages are not in main memory
Page Fault
od
If a page is needed that was not originally loaded up, then a page fault trap is generated.
Steps in Handling a Page Fault
1. The memory address requested is first checked, to make sure it was a valid memory
request.
uc
2. If the reference is to an invalid page, the process is terminated. Otherwise, if the page is
not present in memory, it must be paged in.
3. A free frame is located, possibly from a free-frame list.
4. A disk operation is scheduled to bring in the necessary page from disk.
vt
5. After the page is loaded to memory, the process's page table is updated with the new
frame number, and the invalid bit is changed to indicate that this is now a valid page
reference.
6. The instruction that caused the page fault must now be restarted from the beginning.
522
Operating Systems BCS303
21CS44
c om
Fig: steps in handling page fault
e.
Pure Demand Paging: Never bring a page into main memory until it is required.
• We can start executing a process without loading any of its pages into main memory.
• Page fault occurs for the non memory resident pages.
od
• After the page is brought into memory, process continues to execute.
• Again page fault occurs for the next page.
Hardware support: For demand paging the same hardware is required as paging and swapping.
uc
1. Page table:-Has the ability to mark an entry invalid through valid-invalid bit.
2. Secondary memory:-This holds the pages that are not present in main memory.
Performance of Demand Paging: Demand paging can have significant effect on the performance
vt
623
Operating Systems 21CS44
BCS303
om
COPY-ON-WRITE
• Technique initially allows the parent and the child 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.
c
• Eg:-If a child process try 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
e.
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.
od
• The previous contents of pages are erased before getting them into main memory. This
is called Zero – on fill demand.
724
Operating Systems BCS303
21CS44
PAGEREPLACEMENT
• Page replacement policy deals with the solution of pages in memory to be replaced by a
new page that must be brought in. When a user process is executing a page fault occurs.
• The hardware traps to the operating system, which checks the internal table to see that this
is a page fault and not an illegal memory access.
• The operating system determines where the derived page is residing on the disk, and this
finds that there are no free frames on the list of free frames.
• When all the frames are in main memory, it is necessary to bring a new page to satisfy the
page fault, replacement policy is concerned with selecting a page currently in memory to be
om
replaced.
• The page i,e to be removed should be the page i,e least likely to be referenced in future.
c
e.
od
Victim Page
• The page that is supported out of physical memory is called victim page.
• If no frames are free, the two page transforms come (out and one in) are read. This will see
the effective access time.
• Each page or frame may have a dirty (modify) bit associated with the hardware. The
modify bit for a page is set by the hardware whenever any word or byte in the page is
825
Operating Systems BCS303
21CS44
om
• If the page is not modified (read-only) then one can discard such page without writing it
onto the disk. Modify bit of such page is set to0.
• Modify bit is set to 1, if the page has been modified. Such pages must be written to the
disk.
• Modify bit is used to reduce overhead of page transfers – only modified pages are written
c
to disk e.
PAGE REPLACEMENT ALGORITHMS
• Want lowest page-fault rate
• Evaluate algorithm by running it on a particular string of memory references (reference
od
string) and computing the number of page faults on that string
• In all our examples, the reference string is
1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
uc
FIFO Algorithm:
• This is the simplest page replacement algorithm. A FIFO replacement algorithm associates
each page the time when that page was brought into memory.
• When a Page is to be replaced the oldest one is selected.
• We replace the queue at the head of the queue. When a page is brought into memory, we
vt
26
9
Operating Systems 21CS44
BCS303
Belady’s Anomaly
• For some page replacement algorithm, the page fault may increase as the number of
allocated frames increases. FIFO replacement algorithm may face this problem.
more frames ⇒ more page faults
Example: Consider the following references string with frames initially empty.
▪ The first three references (7,0,1) cases page faults and are brought into the empty frames.
▪ The next references 2 replaces page 7 because the page 7 was brought in first. x Since 0 is
the next references and 0 is already in memory e has no page faults.
om
▪ The next references 3 results in page 0 being replaced so that the next references to 0
causer page fault. This will continue till the end of string. There are 15 faults all together.
c
e.
od
27
10
Operating Systems 21CS44
BCS303
Optimal Algorithm
▪ Optimal page replacement algorithm is mainly to solve the problem of Belady’s Anomaly.
▪ Optimal page replacement algorithm has the lowest page fault rate of all algorithms.
▪ An optimal page replacement algorithm exists and has been called OPT.
The working is simple “Replace the page that will not be used for the longest period of time”
Example: consider the following reference string
▪ The first three references cause faults that fill the three empty frames.
om
▪ The references to page 2 replaces page 7, because 7 will not be used until reference 18. x
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.
▪ Replace page that will not be used for longest period of time
c
Optimal Page Replacement
e.
od
uc
forwards.
The main problem to how to implement LRU is the LRU requires additional h/w assistance.
28
11
Operating Systems BCS303
21CS44
om
numbers when a page is referenced it is removed from the stack and put on to the top
of stack. In this way the top of stack is always the most recently used page and the
bottom in least recently used page. Since the entries are removed from the stack it is
best implement by a doubly linked list. With a head and tail pointer.
Note: Neither optimal replacement nor LRU replacement suffers from Belady’s Anamoly. These
c
are called stack algorithms.
e.
LRU-Approximation Page Replacement
• Many systems offer some degree of hardware support, enough to approximate LRU.
• In particular, many systems provide a reference bit for every entry in a page table, which
od
is set anytime that page is accessed. Initially all bits are set to zero, and they can also all be
cleared at any time. One bit distinguishes pages that have been accessed since the last clear
from those that have not been accessed.
Additional-Reference-Bits Algorithm
uc
• An 8-bit byte (reference bit) is stored for each page in a table in memory.
• At regular intervals (say, every 100 milliseconds), a timer interrupt transfers control to the
operating system. The operating system shifts the reference bit for each page into the
high-order bit of its 8-bit byte, shifting the other bits right by 1 bit and discarding the low-
vt
order bit.
• These 8-bit shift registers contain the history of page use for the last eight time periods.
• If the shift register contains 00000000, then the page has not been used for eight time
periods.
• A page with a history register value of 11000100 has been used more recently than one
with a value of 01110111.
Second- chance (clock) page replacement algorithm
• The second chance algorithm is a FIFO replacement algorithm, except the reference bit
is used to give pages a second chance at staying in the page table.
• When a page must be replaced, the page table is scanned in a FIFO (circular queue)
manner.
• If a page is found with its reference bit as ‘0’, then that page is selected as the next
victim.
29
12
Operating Systems BCS303
21CS44
• If the reference bitvalueis‘1’, then the page is given a second chance and its reference bit
value is cleared (assigned as‘0’).
• Thus, a page that is given a second chance will not be replaced until all other pages have
been replaced (or given second chances). In addition, if a page is used often, then it sets
its reference bit again.
• This algorithm is also known as the clock algorithm.
c om
e.
od
• This algorithm searches the page table in a circular fashion, looking for the first page it can
find in the lowest numbered category. i.e. it first makes a pass looking for a ( 0, 0 ), and then
if it can't find one, it makes another pass looking for a(0,1),etc.
• The main difference between this algorithm and the previous one is the preference for
replacing clean pages if possible.
30
13
Operating Systems BCS303
17CS64
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.
b)MFU Algorithm:
based on the argument that the page with the smallest count was probably just brought in
and has yet to be used
ALLOCATION OF FRAMES
• The absolute minimum number of frames that a process must be allocated is dependent
om
on system architecture.
• The maximum number is defined by the amount of available physical memory.
Allocation Algorithms
After loading of OS, there are two ways in which the allocation of frames can be done to
the processes.
c
Equal Allocation- If there are m frames available and n processes to share them, each
process gets m / n frames, and the left over’s are kept in a free-frame buffer pool.
e.
Proportional Allocation - Allocate the frames proportionally depending on the size
of the process. If the size of process i is Si, and S is the sum of size of all processes in
the system, then the allocation for process Pi is ai= m * Si/ S. where m is the free
od
frames available in the system.
• Consider a system with a 1KB frame size. If a small student process of 10 KB and an
interactive database of 127 KB are the only two processes running in a system with 62
free frames.
• with proportional allocation, we would split 62 frames between two processes, as
uc
follows
m=62, S = (10+127)=137
Allocation for process 1 = 62 X 10/137 ~ 4 Allocation for process 2 = 62 X
127/137 ~57
vt
• Global page replacement is over all more efficient, and is the more commonly used
approach.
om
• In such systems, CPU s can access memory that is physically located on the same board
much faster than the memory on the other boards.
• The basic solution is akin to processor affinity - At the same time that we try to
schedule processes on the same CPU to minimize cache misses, we also try to allocate
memory for those processes on the same boards, to minimize access times.
c
THRASHING
• If the number of frames allocated to a low-priority process falls below the minimum
e.
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.
od
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.
uc
Cause of Thrashing
• Thrashing results in severe performance problem.
• The operating system monitors the cpu utilization is low. We increase the degree of
vt
reached. If the degree of multi programming is increased further thrashing sets in and
the cpu utilization drops sharply.
• At this point, to increases CPU utilization and stop thrashing, we must increase degree
of multiprogramming.
• we can limit the effect of thrashing by using a local replacement algorithm. To prevent
thrashing, we must provide a process as many frames as it needs.
Locality of Reference:
• As the process executes it moves from locality to locality.
om
• 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 :
c
1. spatial locality 2. temporal locality
• Working sets are not enough and we must also introduce balance set.
▪ 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.
▪ 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.
▪ 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.
Page-Fault Frequency
• When page- fault rate is too high, the process needs more frames and when it is too low,
the process may have too many frames.
• The upper and lower bounds can be established on the page-fault rate. If the actual
page- fault rate exceeds the upper limit, allocate the process another frame or suspend
the process.
• If the page-fault rate falls below the lower limit, remove a frame from the process.
Thus, we can directly measure and control the page-fault rate to prevent thrashing.
c om
e.
od
uc
vt