Os 4 1727677618970
Os 4 1727677618970
• In this variable partition scheme, the operating system keeps a table indicating
which parts of memory are available and which are occupied.
• Initially, all memory is available for user processes and is considered one large
block of available memory, a hole.
• Eventually, as you will see, memory contains a set of holes of various sizes
Partitioned allocation:
• In Partition Allocation, when there is more than one partition freely
available to accommodate a process’s request, a partition must be
selected.
• To choose a particular partition, a partition allocation method is
needed.
• A partition allocation method is considered better if it avoids internal
fragmentation.
• When it is time to load a process into the main memory and if there is
more than one free block of memory of sufficient size then the OS
decides which free block to allocate.
Contiguous Allocation
• Main memory must support both OS and user processes
• Limited resource, must allocate efficiently
• Contiguous allocation is one early method
• Main memory usually into two partitions:
• Resident operating system, usually held in low memory with interrupt vector
• User processes then held in high memory
• Each process contained in single contiguous section of memory
Multiple-partition allocation
• Multiple-partition allocation
• Degree of multiprogramming limited by number of partitions
• Variable-partition sizes for efficiency (sized to a given process’ needs)
• Hole – block of available memory; holes of various size are scattered throughout memory
• When a process arrives, it is allocated memory from a hole large enough to accommodate it
• Process exiting frees its partition, adjacent free partitions combined
• Operating system maintains information about:
a) allocated partitions b) free partitions (hole)
Dynamic Storage-Allocation Problem
How to satisfy a request of size n from a list of free holes?
• Best-fit: Allocate the smallest hole that is big enough; must search entire
list, unless ordered by size
• Produces the smallest leftover hole
• Worst-fit: Allocate the largest hole; must also search entire list
• Produces the largest leftover hole
First-fit and best-fit better than worst-fit in terms of speed and storage
utilization
Placement Algorithms
There are different Placement Algorithm:
• A. First Fit
• B. Best Fit
• C. Worst Fit
First Fit
Hole 10k Hole 10k Hole 10k Hole 10k Hole 10k
40k is not allocated as free blocks are not Available
Best Fit
• The best fit deals with allocating the smallest free partition which meets the
requirement of the requesting process. This algorithm first searches the
entire list of free partitions and considers the smallest hole that is adequate.
It then tries to find a hole which is close to actual process size needed.
• Advantage
• Memory utilization is much better than first fit as it searches the smallest
free partition first available.
• Disadvantage
• It is slower and may even tend to fill up memory with tiny useless holes.
Fragmentation
• External Fragmentation – total memory space exists to satisfy a
request, but it is not contiguous
• Internal Fragmentation – allocated memory may be slightly larger than
requested memory, unused memory that is internal to a partition
100kb 180 kb
Process: p3
150 kb
100kb 180 kb
Process: p3
150 kb
Contiguous Allocation
50kb
× 30kb
× 30kb 30kb
100kb
50kb 50kb 50kb
180kb
100kb 100kb 100kb
Compaction
× 150kb × 150kb 150kb
Frame a 1 kb
Frames 1 kb Process: p1
of 1kb Frame b Size: 3 kB
each 1 kb
Frame c
Frame d 1 kb
Frame e 1 kb
1 kb Process: p2
Frame f Size: 5 kB
Frame g 1 kb
Frame h 1 kb
1 kb
Process: p3
1 kb Size: 2kB
Physical Memory is divided into fixed-sized blocks Logical memory is divided into blocks of the same
called frames size called pages.
Frame a 1 kb
Frames 1 kb
of 1kb Frame b
each 1 kb
Frame c
Frame d 1 kb
Process: p1
Frame e 1 kb
1 kb Process: p2
Frame f Size: 5 kB
Frame g 1 kb
Frame h 1 kb
1 kb
Process: p3
1 kb Size: 2kB
Physical Memory is divided into fixed-sized blocks Logical memory is divided into blocks of the same
called frames size called pages.
Frame a 1 kb
Frames Process: p2 1 kb
of 1kb Frame b
each 1 kb
Frame c
Frame d 1 kb
Process: p1
Frame e 1 kb
Frame f 1 kb
Frame g 1 kb
Process: p2
Frame h 1 kb
1 kb
Process: p3
1 kb Size: 2kB
Logical Versus Physical Address Space
The MMU translates a logical address generated by the CPU to a physical address:
Page Size
• The page size (like the frame size) is defined by the hardware.
• The size of a page is a power of 2, typically varying between 4 KB and 1 GB per
page, depending on the computer architecture.
• 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 particularly easy.
• 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. Thus, the logical address is as follows:
Paging model of logical and physical
memory.
Structure of the Page Table
• Most modern computer systems support a large logical address space
(232 to 264). In such an environment, the page table itself becomes
excessively large.
• For example, consider a system with a 32-bit logical address space.
• If the page size in such a system is 4 KB (212),
• then a page table may consist of over 1 million entries (220 = 232/212).
Assuming that each entry consists of 4 bytes,
• Each process may need up to 4 MB of physical address space for the page
table alone
two-level paging algorithm
• One way is to use a two-level paging algorithm, in which the page
table itself is also paged
• For example, consider again the system with a 32-bit logical address
space and a page size of 4 KB.
• A logical address is divided into a page number consisting of 20 bits and a
page offset consisting of 12 bits. Because we page the page table, the page
number is further divided into a 10-bit page number and a 10-bit page offset.
Thus, a logical address is as follows:
Segmentation Basic Method
• A process is divided into Segments.
• The chunks that a program is divided into which are not necessarily all
of the exact sizes are called segments.
• Segmentation gives the user’s view of the process which paging does
not provide.
• Here the user’s view is mapped to physical memory.
What is Segment Table?
• It maps a two-dimensional Logical address into a one-dimensional
Physical address. It’s each table entry has:
• Base Address: It contains the starting physical address where the
segments reside in memory.
• Segment Limit: Also known as segment offset. It specifies the length
of the segment.
Logical View of Segmentation
Segmentation
• The process is divided into large chunks called segments or modules.
• The CPU generates this logical address.
• This logical address is to access secondary memory. But CPU has to access the
main memory. In this case, address translation is required to convert a logical
address into a physical one.
• A physical address is an address for finding data in the main memory. So now the
CPU will take the help of the segment table.
• A segment table is a data structure for storing the information of all process
segments.
Segmentation
• CPU uses a segment table to map the logical address to a physical
address. In the segment table, there are two types of information
• Limit: Actual size of a segment.
• Base Address: the address of the segment in the main memory.
• Then if the value of offset(d)<=Limit.
• Then only the CPU can read that segment;
• else, the error will be there.
• Offset(d) depicts the size of that segment CPU wants to read.
SEGMENTATION PROBLEMS
Problem-
• Consider the following segment table-
• Option-C: 2, 100
• Segment Number = 2
• Segment Offset = 100
• In the segment table,
• limit of segment-2 is 100.
• Thus, segment offset must always lie in the range = [2, 100-1] =[0, 99]
• In the segment table, limit of segment-2 is 100.
• Thus, segment offset must always lie in the range = [0, 100-1] = [0, 99] , Since generated segment offset does
not lie in the above range, so request generated is invalid. Therefore, trap will be produced.
• Option-D: 3, 425-
• Here,
• Segment Number = 3
• Segment Offset = 425
• In the segment table, limit of segment-3 is 580.
• Thus, segment offset must always lie in the range = [0, 580-1] = [0,
579]
• Now,
• Since generated segment offset lies in the above range, so request
generated is valid.
• Therefore, no trap will be produced.
• Physical Address = 1327 + 425 = 1752
• Option-E: 4, 95-
• Here,
• Segment Number = 4
• Segment Offset = 95
• In the segment table, limit of segment-4 is 96.
• Thus, segment offset must always lie in the range = [0, 96-1] = [0, 95]
Now,Since generated segment offset lies in the above range, so request
generated is valid.
• Therefore, no trap will be produced. Physical Address = 1952 + 95 =
2047
Virtual Memory
• Virtual Memory is a storage allocation scheme in which secondary memory can be
addressed as though it were part of the main memory. It maps memory addresses
used by a program, called virtual addresses, into physical addresses in computer
memory.
1.All memory references within a process are logical addresses that are dynamically
translated into physical addresses at run time. This means that a process can be
swapped in and out of the main memory such that it occupies different places in
the main memory at different times during the course of execution.
2.A process may be broken into a number of pieces and these pieces need not be
continuously located in the main memory during execution. The combination of
dynamic run-time address translation and the use of a page or segment table
permits this.
Demand paging
• Demand paging in os is a memory management technique used by modern operating systems to efficiently
manage memory usage.
• The basic idea behind demand paging is to allow the operating system to load only the parts of a program that
are currently needed into memory, rather than loading the entire program all at once.
• When a program is launched, the operating system does not load the entire program into memory at once.
Instead, it loads only the parts of the program that are immediately required to start executing. These parts are
typically the program’s code and any data that is immediately needed. As the program runs and requires more
memory, the operating system loads additional parts of the program into memory as needed.
Demand paging
• The operating system also keeps track of which parts of the program are currently being used and which parts
are not. If a part of the program has not been used for a long time, the operating system can remove it from
memory to free up space for other programs. This process is called “swapping.”
• Demand paging in os is an effective technique because it allows the operating system to use memory more
efficiently. Rather than allocating a large block of memory for a program, the operating system can allocate
only the memory that is actually needed. This can help reduce the amount of memory that is wasted, which
can be especially important in systems with limited memory resources.
• Overall, demand paging is a key technique used by modern operating systems to manage memory usage and
improve system performance.
Thrashing
• Thrashing is a condition or a situation when the system is spending a
major portion of its time servicing the page faults, but the actual
processing done is very negligible.
• Causes of thrashing:
• High degree of multiprogramming.
• Lack of frames.
• Page replacement policy.
Causes of Thrashing
3. Bring the desired page into the (newly) free frame; update the page
and frame tables
4. Continue the process by restarting the instruction that caused the trap
Note now potentially 2 page transfers for page fault – increasing EAT
Page Replacement
Basic Page Replacement
1. Find the location of the desired page on disk
3. Bring the desired page into the (newly) free frame; update the page
and frame tables
4. Continue the process by restarting the instruction that caused the trap
Note now potentially 2 page transfers for page fault – increasing EAT
Page Replacement
Page and Frame Replacement Algorithms
15 page faults
• Can vary by reference string: consider
1,2,3,4,1,2,5,1,2,3,4,5
• Adding more frames can cause more page faults!
• Belady’s Anomaly
• How to track ages of pages?
• Just use a FIFO queue
Optimal Algorithm
• Replace page that will not be used for longest period
of time
• 9 is optimal for the example
• How do you know this?
• Can’t read the future
• Used for measuring how well your algorithm performs
Least Recently Used (LRU) Algorithm
• Use past knowledge rather than future
• Replace page that has not been used in the most amount of time
• Associate time of last use with each page