Osy Meena Chapter-5 College
Osy Meena Chapter-5 College
Osy Meena Chapter-5 College
Memory Management
Memory is central to the operation of a modern computer system. Memory is a large array of
words or bytes, each with its own address.
A program resides on a disk as a binary executable file. The program must be brought into
memory and placed within a process for it to be executed. Depending on the memory
management in use the process may be moved between disk and memory during its execution.
The collection of processes on the disk that are waiting to be brought into memory for execution
forms the input queue. i.e. selected one of the process in the input queue and to load that process
into memory. We can provide protection by using two registers, usually a base and a limit, as
shown in fig.5.1 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
420939(inclusive).
Fig. 5.1
Partitioning
The memory is usually divided into two partitions, one for the resident operating system,
and one for the user processes.
If the operating system is residing in low memory then user processes are executed in high
memory.
The code and data of operating system must be protected from changes by user processes.
The user processes are also protected from one another.
Operating 0
system Job Queue
2160 400k Process Memor Time
y
P1 600K 10
Operating System chapter -5 notes Meena talele P2 1000K 5 2
P3 300K 20
P4 700K 8
P5 500K 15
2560K
Operating 0
System
400K
P1
1000K
P2
2000K
P3
2300K
2560K
Memory Allocation for processes
Fig 5.2
Disadvantages
1. The main drawback of fixed partition is memory wastage.
2. In a fixed partition, if the memory required for process is less than size of partition, then
remaining part of memory can not be allocated to any other process causing internal
fragmentation.
1. First fit:-
Allocates first hole that is big enough.
This algorithm scans memory from the beginning and selects the first available
block that is large enough to hold the process.
Fig (a) part of memory with five processes and three holes. The shaded regions (0 in the bitmap)
are free. (b) The corresponding bitmap
Fig 5.3
Swapping
A process, can be swapped temporarily out of memory to a backing store, and then
brought back into memory for continued execution.
Assume a multiprogramming environment with a round robin CPU-scheduling algorithm.
When a quantum expires, the memory manager will start to swap out the process that just
finished, and to swap in another process to the memory space that has been freed ( Fig ).
When each process finishes its quantum, it will be swapped with another process.
Fig 5.4
Fragmentation:
Memory fragmentation can be of two types:
1. Internal Fragmentation.
2. External Fragmentation
In Internal Fragmentation there is wasted space internal to a portion due to the fact that block
of data loaded is smaller than the partition.
Eg:-If there is a block of 50kb and if the process requests 40kb and if the block is
allocated to
the process then there will be 10kb of memory left.
External Fragmentation exists when there is enough memory space exists to satisfy the request,
but it is not contiguous i.e., storage is fragmented into large number of small holes.
External Fragmentation may be either minor or a major problem.
One solution for over-coming external fragmentation is compaction. The goal is to move
all the free memory together to form a large block.
Compaction is not possible always. If the relocation is static and is done at load time then
compaction is not possible. Compaction is possible if the re-location is dynamic and done
at execution time.
Another possible solution to the external fragmentation problem is to permit the logical
address space of a process to be non-contiguous, thus allowing the process to be allocated
physical memory whenever the latter is available.
Segmentation
A user program can be subdivided using segmentation, in which the program and its
associated data are divided into a number of segments.
It is not required that all segments of all programs be of the same length, although there is
a maximum segment length.
A logical address using segmentation consists of two parts, a segment number and an
offset.
Because of the use of unequal-size segments, segmentation is similar to dynamic
partitioning.
In segmentation, a program may occupy more than one partition, and these partitions need
not be contiguous.
Segmentation eliminates internal fragmentation but, like dynamic partitioning, it suffers
from external fragmentation.
Fig. 5.5
For Example,
Using a page size of 4 bytes and a physical memory of 32 bytes (8 pages) , the logical memory
can be mapped to physical memory as shown in below fig. 5.6
(Note To calculate page number and offset from the given logical address the formula is :
Logical address/page size. The quotient gives the page number and remainder gives the offset )
Physical
Logical Frame Physical
Info Page Page size Offset Address
Address no. Address
=
1 B 0 5 4 1 5*4+1 21
3 D 0 5 4 3 5*4+3 23
8 I 2 1 4 0 1*4+0 4
14 O 3 2 4 2 2*4+2 10
10 K 2 1 4 2 1*4+2 6
Reference String
The string of memory references is called reference string. Reference strings are generated
artificially or by tracing a given system and recording the address of each memory reference. The
latter choice produces a large number of data, where we note two things.
For a given page size we need to consider only the page number, not the entire address.
If we have a reference to a page p, then any immediately following references to page p
will never cause a page fault. Page p will be in memory after the first reference. The
immediately following references will not cause page fault.
For example, consider the following sequence of addresses - 123,215,600,1234,76,96
If page size is 100 then the reference string is 1,2,6,12,0,0
FIFO Algorithm:
1. This is the simplest page replacement algorithm. A FIFO replacement algorithm
associates each page the time when that page was brought into memory.
2. When a Page is to be replaced the oldest one is selected.
3. 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.
Example: Consider the following references string with frames initially empty.
Example 2
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.
Example 2