cs3451 Ios Unit III Notes
cs3451 Ios Unit III Notes
MEMORY PROTECTION:
For proper system operation we must protect the operating system from access by user processes.
Each process has a separate memory space. Separate per-process memory space protects the processes from
each other.
The hardware protection of memory is provided by two registers
o Base Register
o Limit Register
The base register holds the smallest legal physical memory address, called the starting address of the
process.
The Limit register specifies the size of range of the process.
If the base register holds300040 and the limit register is 120900, then the program can legally access all
addresses from 300040 through 420939
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, resulting in addressing error.
This scheme prevents a user program from modifying the code or data structures of either the operating system
or other users.
The address generated by the CPU for a process should lie between the Base address of the process and base +
Limit of the process, Else the hardware sends an interrupt to the OS.
ADDRESS BINDING:
Address binding is the process of mapping the program's logical or virtual addresses to corresponding physical
or main memory addresses.
Addresses in the source program are generally symbolic.
A compiler typically binds these symbolic addresses to relocatable addresses.
The linkage editor or loader in turn binds the relocatable addresses to absolute addresses
Each binding is a mapping from one address space to another .
The binding of instructions and data to memory addresses can be done in three ways.
1) Compile time. If you know at compile time where the process will reside in memory, then absolute code can be
generated.
2) Load time. If it is not known at compile time where the process will reside in memory, then the compiler must
generate relocatable code.
3) Execution time. If the process can be moved during its execution from one memory segment to another, then
binding must be delayed until run time.
DYNAMIC LOADING:
Dynamic Loading is the process of loading a routine only when it is called or needed during runtime.
Initially all routines are kept on disk in a relocatable load format.
The main program is loaded into memory and is executed. When a routine needs to call another routine, the
calling routine first checks to see whether the other routine has been loaded. If it has not, the relocatable
linking loader is called to load the desired routine into memory.
The advantage of dynamic loading is that a routine is loaded only when it is needed.
This method is particularly useful when large amounts of code are needed to handle infrequently occurring
cases, such as error routines.
SWAPPING:
A process must be in memory to be executed.
A process can be swapped temporarily out of memory to a backing store and then brought back into
memory for continued execution. This process is called as Swapping.
Swapping allows the total physical address space of all processes to exceed the real physical memory of the
system, and increases the degree of multiprogramming in a system.
Swapping involves moving processes between main memory and a backing store. The backing store is
commonly a fast disk.
EXAMPLE: Consider a multiprogramming environment with Round Robin Scheduling. When the quantum time of a
process expires the memory manager will swap out the process just finished and swap another process into the memory
space.
The system maintains a ready queue consisting of all processes whose memory images are on the backing store or in
memory and are ready to run.
Whenever the CPU scheduler decides to execute a process, it calls the dispatcher. The dispatcher checks to see whether
the next process in the queue is in memory.
If it is not, and if there is no free memory region, the dispatcher swaps out a process currently in memory and swaps in
the desired process.
MEMORY ALLOCATION:
In Contiguous memory allocation the memory can be allocated in two ways
1) Fixed partition scheme
2) Variable partition scheme
Fixed partition scheme:
One of the simplest methods for allocating memory is to divide memory into several fixed-sized
Partitions. Each partition may contain exactly one process.
Thus, the degree of multiprogramming is bound by the number of partitions.
In this multiple partition method, when a partition is free, a process is selected from the input
queue and is loaded into the free partition.
INTERNAL FRAGMENTATION:
In fixed size partitions, each process is allocated with a partition, irrespective of its size. The allocated
memory for a process may be slightly larger than requested memory; this memory that is wasted
internal to a partition, is called as internal fragmentation.
EXTERNAL FRAGMENTATION: As processes are loaded and removed from memory, the free memory space is
broken into little pieces. External fragmentation exists when there is enough total memory space to satisfy a
request but the available spaces are not contiguous, so that the memory cannot be allocated to the process.
COMPACTION: One solution to the problem of external fragmentation is compaction. The goal is to shuffle the
memory contents so as to place all free memory together in one large block.
50 PERCENT RULE: The analysis of first fit, reveals that given N allocated blocks, another 0.5 N blocks will be lost
to fragmentation. That is, one-third of memory may be unusable! This property is known as the 50-percent rule.
1. SEGMENTATION:
Prepared by Department of CSE,PSNACET Page 6
SEGMENTATION HARDWARE:
The programmer can refer to objects in the program by a two-dimensional address (segment number and
offset); the actual physical memory a one dimensional sequence of bytes.
The two-dimensional user-defined addresses should be mapped into one-dimensional physical addresses.
The mapping of logical address to physical address is done by a table called segment table.
Each entry in the segment table has a segment base and a segment limit.
The segment base contains the starting physical address where the segment resides in memory
The segment limit specifies the length of the segment.
A logical address consists of two parts: a segment number, s, and an offset into that segment, d.
The segment number is used as an index to the segment table. The offset d of the logical address must be
between 0 and the segment limit.
If it is not between 0 and limit then hardware trap to the operating system (logical addressing attempt beyond
end of segment).
When an offset is legal, it is added to the segment base to produce the address in physical memory of the
desired byte.
The segment table is an array of base–limit register pairs.
Segmentation can be combined with paging.
Example: Consider five segments numbered from 0 through 4. The segment table has a separate entry for each
segment, giving the beginning address of the segment in physical memory (or base) and the length of that segment (or
limit).
The 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 to segment 3, byte 852, 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.
2. PAGING:
Paging involves breaking physical memory into fixed-sized blocks called frames and breaking logical memory
PAGING HARDWARE:
Every address generated by the CPU is divided into two parts: a page number (p) and a page offset (d).
The page number is used as an index into a page table.
The page table contains the base address of each page in physical memory.
This base address is combined with the page offset to define the physical memory address that is sent to the
memory unit.
The page size is defined by the hardware. The size of a page is a power of 2, varying between 512 bytes and 1 GB per
page.
If the size of the logical address space is 2m, and a page size is 2n bytes, then the high-order m− n bits of a logical
address designate the page number, and the n low-order bits designate the page offset.
The logical address is given by
Here p is an index into the page table and d is the displacement within the page.
PAGING MODEL:
PAGING EXAMPLE:
Consider the memory with the logical address, n= 2 and m = 4. Using a page size of 4 bytes and a physical
memory of 32 bytes
Logical address 0 is page 0, offset 0. Indexing into the page table, we find that page 0 is in frame 5. Thus,
logical address 0 maps to physical address 20 [= (5 × 4) + 0].
Logical address 3 (page 0, offset 3) maps to physical address 23 [= (5 × 4) + 3].
Logical address 4 is page 1, offset 0; according to the page table, page 1 is mapped to frame 6.
Thus, logical address 4 maps to physical address 24 [= (6 × 4) + 0].
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 operating system is managing physical memory and knows the allocation details of physical memory—
which frames are allocated, which frames are available, how many total frames there are, and so on.
This information is generally kept in a data structure called a frame table.
HARDWARE SUPPORT:
The hardware implementation of the page table can be done in several ways. The page table is implemented as
a set of dedicated registers if the size of the page table is too small.
If the size of the page table is too large then the page table is kept in main memory and a page table base
register is used to point to the page table.
When the page table is kept in main memory then two memory accesses are required to access a byte.
One for accessing the page table entry, another one for accessing the byte.
Thus the overhead of accessing the main memory increases.
The standard solution to this problem is to use a special, small, fast lookup hardware cache called a translation
look-aside buffer (TLB).
Each entry in the TLB consists of two parts: a key (or tag) and a value.
The TLB contains only a few of the page-table entries.
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 is used to access memory.
If the page number is not in the TLB (TLB miss), a memory reference to the page table must be made. When
the frame number is obtained, we can use it to access memory.
The percentage of times that the page number of interest is found in the TLB is called the hit ratio.
Memory protection in a paged environment is accomplished by protection bits associated with each frame.
One bit can define a page to be read–write or read-only. When the physical address is being computed, the
protection bits can be checked to verify that no writes are being made to a read-only page
One additional bit is generally attached to each entry in the page table: a valid–invalid bit. When this bit is set
to valid, the associated page is in the process’s logical address space and is thus a legal.
When the bit is set to invalid, the page is not in the process’s logical address space.
Page-table length register (PTLR),is used to indicate the size of the page table. This value is checked against
every logical address to verify that the address is in the valid range for the process
SHARED PAGES:
EXAMPLE: Consider three processes that share a page editor which is of three pages. Each process has its own data
page.
HIERARCHIAL PAGING:
In Computer a system that has a 32 bit logical address space the page table becomes too large. Each process
may need upto 4MB of Physical address space for the page table alone.
One simple solution to this problem is to divide the page table into smaller pieces. One way is to use a two-
level paging algorithm, in which the page table itself is also paged.
Consider the system with a 32-bit logical address space and a page size of (2 12)4 KB.
A logical address is divided into a page number consisting of 20 bits and a page offset consisting of 12 bits.
The page number is further divided into a 10-bit page number and a 10-bit page offset.
Here p1 is an index into the outer page table and p2 is the displacement within the page of the inner page table.
Address translation works from the outer page table inward; this scheme is also known as a forward mapped
page table.
A variation to hashed page table is clustered page tables, which are similar to hashed page tables except that
each entry in the hash table refers to several pages (such as 16) rather than a single page.
IA-32 Segmentation:
The IA-32 architecture allows a segment to be as large as 4 GB, and the maximum number of segments per
process is 16 K.
The logical address space of a process is divided into two partitions. Information about the process that are
private to the process is kept in the local descriptor table (LDT);
The information that is shared among other processes is kept in the global descriptor table (GDT).
Each entry in the LDT and GDT consists of an 8-byte segment descriptor with detailed information about a
particular segment, including the base location and limit of that segment.
The logical address is a pair (selector, offset), where the selector is a 16-bit number:
s designates the segment number, g indicates whether the segment is in the GDT or LDT, and p deals
with protection.
The offset is a 32-bit number specifying the location of the byte.
The segment registers points to the appropriate entry in the LDT or GDT.
The base and limit information about the segment is used to generate a linear address.
IA -32 PAGING:
The IA-32 architecture allows a page size of either 4 KB or 4 MB.
For 4-KB pages, IA-32 uses a two-level paging scheme in which the division of the 32-bit linear address is as
follows.
The 10 high-order bits reference an entry in the outermost page table, which IA-32 terms the page directory.
VIRTUAL MEMORY:
Virtual memory is a memory management technique that allows the execution of processes that are not
completely in memory.
In some cases during the execution of the program the entire program may not be needed, such as error
conditions, menu selection options etc.
The virtual address space of a process refers to the logical view of how a process is stored in memory.
The heap will grow upward in memory as it is used for dynamic memory allocation.
The stack will grow downward in memory through successive function calls .
The large blank space (or hole) between the heap and the stack is part of the virtual address space 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.
Prepared by Department of CSE,PSNACET Page 17
ADVANTAGES:
One major advantage of this scheme is that programs can be larger than physical memory
Virtual memory also allows processes to share files easily and to implement shared memory.
Increase in CPU utilization and throughput.
Less I/O would be needed to load or swap user programs into memory
DEMAND PAGING:
Demand paging is the process of loading the pages only when they are demanded by the process during
execution. Pages that are never accessed are thus never loaded into physical memory.
A demand-paging system is similar to a paging system with swapping where processes reside in secondary
memory
When we want to execute a process, we swap it into memory. Rather than swapping the entire process into
memory we use a lazy swapper that never swaps a page into memory unless that page will be needed.
PAGE FAULT: If the process tries to access a page that was not brought into memory, then it is called as a page fault.
Access to a page marked invalid causes a page fault.
The paging hardware, will notice that the invalid bit is set, causing a trap to the operating system.
PURE DEMAND PAGING: The process of executing a program with no pages in main memory is called as pure
demand paging. This never brings a page into memory until it is required.
The hardware to support demand paging is the same as the hardware for paging and swapping:
Page table. This table has the ability to mark an entry invalid through a valid–invalid bit or a special value of
protection bits.
Secondary memory. This memory holds those pages that are not present in main memory. The secondary
memory is usually a high-speed disk. It is known as the swap device, and the section of disk used for this
purpose is known as swap space.
Page 0 is the next reference and 0 is already in memory, we have no fault for this reference.
The first reference to 3 results in replacement of page 0, since it is now first in line.
Because of this replacement, the next reference, to 0, will fault. Page 1 is then replaced by page 0. The process
continues until all the pages are referenced.
Advantages:
The FIFO page-replacement algorithm is easy to understand and program
Disadvantages:
The Performance is not always good.
It Suffers from Belady’s Anomaly.
BELADY’S ANOMALY: The page fault increases as the number of allocated memory frame increases. This
unexpected result is called as Belady’s Anomaly.
Advantages:
The LRU policy is often used as a page-replacement algorithm and is considered to be good.
LRU replacement does not suffer from Belady’s anomaly.
Disadvantage:
The problem is to determine an order for the frames defined by the time of last use.
Two implementations are feasible:
• Counters. We associate with each page-table a time-of-use field and add to the CPU a logical clock or counter. The
clock is incremented for every memory reference. Whenever 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. So we can find the ―time‖ of the last
reference to each page.
• Stack. Another approach to implementing LRU replacement is to keep a stack of page numbers. Whenever a page is
referenced, it is removed from the stack and put on the top. In this way, the most recently used page is always at the top
of the stack and the least recently used page is always at the bottom
STACK ALGORITHM:
A stack algorithm is an algorithm for which it can be shown that the set of pages in memory for n frames is
always a subset of the set of pages that would be in memory with n + 1 frames.
a) Additional-reference-bits algorithm
The additional ordering information can be gained by recording the reference bits at regular intervals. We can
keep an 8-bit byte for each page in a table in memory.
At regular intervals (say, every 100 milliseconds), a timer interrupt transfers control to the operating system.
These 8-bit shift registers contain the history of page use for the last eight time periods.
If the shift register contains 00000000, for example, then the page has not been used for eight time periods.
A page that is used at least once in each period has a shift register value of 11111111.
A page with a history register value of 11000100 has been used more recently than one with a value of
01110111.
Thus the page with the lowest number is the LRU page, and it can be replaced.
b) Second-Chance Algorithm:
The basic algorithm of second-chance replacement is a FIFO replacement algorithm.
When a page has been selected, however, we inspect its reference bit.
If the value is 0, we proceed to replace this page; but if the reference bit is set to 1, we give the page a second
chance and move on to select the next FIFO page.
When a page gets a second chance, its reference bit is cleared, and its arrival time is reset to the current time.
Thus, a page that is given a second chance will not be replaced until all other pages have been replaced
One way to implement the second-chance algorithm is as a circular queue.
A pointer indicates which page is to be replaced next. When a frame is needed, the pointer advances until it
finds a page with a 0 reference bit.
Once a victim page is found, the page is replaced, and the new page is inserted in the circular queue in that
position
Most frequently used (MFU) page-replacement algorithm: The most frequently used (MFU) page-
replacement algorithm is 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:
Allocation of frames deals with how the operating system allocates the fixed amount of free memory among
the various processes.
Consider a single-user system with 128 KB of memory composed of pages 1 KB in size.
This system has 128 frames. The operating system may take 35 KB, leaving 93 frames for the user process.
Under pure demand paging, all 93 frames would initially be put on the free-frame list. When a user process
started execution, it would generate a sequence of page faults.
The first 93 page faults would all get free frames from the free-frame list. When the free-frame list was
exhausted, a page-replacement algorithm would be used to select one of the 93 in-memory pages to be replaced
with the 94th, and so on.
When the process terminated, the 93 frames would once again be placed on the free-frame list.
Operating system allocates all its buffer and table space from the free-frame list.
When this space is not in use by the operating system, it can be used to support user paging.
a) Minimum Number of Frames
OS cannot allocate more than the total number of available frames (unless there is page sharing). It must also allocate
at least a minimum number of frames as required by the process.
The reason for allocating at least a minimum number of frames involves performance. As the number of
Prepared by Department of CSE,PSNACET Page 26
EXAMPLE: consider an allocation scheme where in we allow high-priority processes to select frames from low-
priority processes for replacement.
A process can select a replacement from among its own frames or the frames of any lower-priority process.
This approach allows a high-priority process to increase its frame allocation
One problem with a global replacement algorithm is that a process cannot control its own page-fault rate. The
set of pages in memory for a process depends not only on the paging behavior of that process but also on the
paging behavior of other processes.
In local replacement, the set of pages in memory for a process is affected by the paging behavior of only that
process.
The goal is to have memory frames allocated ―as close as possible‖ to the CPU on which the process is
running so that the memory access can be faster.
In NUMA systems the scheduler tracks the last CPU on which each process ran. If the scheduler tries to
schedule each process onto its previous CPU, and the memory-management system tries to allocate frames for
the process close to the CPU on which it is being scheduled, then improved cache hits and decreased memory
access times will result.
THRASHING:
If the process does not have the number of frames it needs to support pages in active use, it will quickly
page-fault. At this point, it must replace some page. If all its pages are in active use, it must replace a
page that will be needed again right away. So it quickly faults again, and again, and again, replacing
pages that it must bring back in immediately. This high paging activity is called thrashing.
Prepared by Department of CSE,PSNACET Page 28
Causes of Thrashing:
The operating system monitors CPU utilization If CPU utilization is too low; we increase the degree of
multiprogramming by introducing a new Process to the system.
Now suppose that a process enters a new phase in its execution and needs more frames. It starts faulting and
taking frames away from other processes.
A global page-replacement algorithm is used; it replaces pages without regard to the process to which they
belong.
These processes need those pages, however, and so they also fault, taking frames from other processes. These
faulting processes must use the paging device to swap pages in and out. As processes wait for the paging
device, CPU utilization decreases.
The CPU scheduler sees the decreasing CPU utilization and increases the degree of multiprogramming as a
result. The new process tries to get started by taking frames from running processes, causing more page faults
and a longer queue for the paging device.
As a result, CPU utilization drops even further, and the CPU scheduler tries to increase the degree of
multiprogramming even more. Thrashing has occurred, and system throughput plunges.
At this point, to increase CPU utilization and stop thrashing, we must decrease the degree of Multi
programming.
We can limit the effects of thrashing by using a local replacement algorithm. With local replacement, if one
process starts thrashing, it cannot steal frames from another process, so the page fault of one process does not
affect the other process.
To prevent thrashing, we must provide a process with as many frames as it needs. The Os need to know how
many frames are required by the process.
The working-set strategy starts by looking at how many frames a process is actually using. This approach
defines the locality model of process execution.
A locality is a set of pages that are actively used together. A program is generally composed of several
different localities, which may overlap.
Suppose we allocate enough frames to a process to accommodate its current locality. It will fault for the pages
in its locality until all these pages are in memory; then, it will not fault again until it changes localities.
If we do not allocate enough frames to accommodate the size of the current locality, the process will thrash,
since it cannot keep in memory all the pages that it is actively using.
Working-Set Model
The working-set model is based on the assumption of locality.
This model uses a parameter Δ to define the working-set window.
The idea is to examine the most recent Δ page references.
The set of pages in the most recent Δ page references is the working set.
If the page-fault rate falls below the lower limit, we remove a frame from the process.
Thus, we can directly measure and control the page-fault rate to prevent thrashing.
If the page-fault rate increases and no free frames are available, we must select some process and swap it out to
backing store.
The freed frames are then distributed to processes with high page-fault rates.