OS Unit-4
OS Unit-4
In a multiprogramming computer, the operating system resides in a part of memory and the rest is used
by multiple processes.
The task of sub dividing the memory among the different processes is called memory management.
It is a method in the operating system to manage operations between main memory and disk during
process execution. The main aim of memory management is to achieve efficient utilization of memory.
Address Binding
Usually, a program resides on a disk as a binary executable file. To be executed, the program must be
brought into memory and placed within a process.
Depending on the memory management in use, the process may be moved between disk and memory
during its execution. The processes on the disk that are waiting to be brought into memory for execution
form the input queue.
The normal single-tasking procedure is to select one of the processes in the input queue and to load
that process into memory. As the process is executed, it accesses instructions and data from memory.
Eventually, the process terminates, and its memory space is declared available.
Most systems allow a user process to reside in any part of the physical memory. Thus, although the
address space of the computer may start at 00000, the first address of the user process need not be
00000.
Addresses in the source program are generally symbolic (such as the variable count). A compiler
typically binds these symbolic addresses to relocatable. The linkage editor or loader in turn binds the
relocatable addresses to absolute addresses (such as 74014). Each binding is a mapping from one
address space to another.
Classically, the binding of instructions and data to memory addresses can be done at any step along the
way:
• Compile time: During the compile time, if you know where the process will reside in memory, then
absolute code can be generated.
For example, if you 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.
• 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. If the
starting address changes, we need only reload the user code to incorporate this changed value. The
Loader translate the relocatable address to absolute address.
• 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. Special hardware must be available for this
scheme to work.
SSK 1
Unit-4
An address generated by the CPU is commonly referred to as a logical address, whereas an address
seen by the memory unit—that is, the one loaded into the memory-address register of the memory—
is commonly referred to as a physical address.
The set of all logical addresses generated by a program is a logical address space. The set of all physical
addresses corresponding to these logical addresses is a physical address space.
The compile-time and load-time address-binding methods generate identical logical and physical
addresses. However, the execution-time address- binding scheme results in differing logical and
physical addresses. We usually refer to the logical address as a virtual address.
The run-time mapping from virtual to physical addresses is done by a hardware device called the
memory-management unit (MMU).
MMU scheme that is a generalization of the base-register scheme uses the base register which is also
called a relocation register.
The value in the relocation register is added to every address generated by a user process at the time
the address is sent to memory (see Figure 8.4).
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.
We now have two different types of addresses: logical addresses (in the range 0 to max) and physical
addresses (in the range R + 0 to R + max for a base value R). The user program generates only logical
addresses and thinks that the process runs in locations 0 to max. However, these logical addresses must
SSK 2
Unit-4
be mapped to physical addresses before they are used. The concept of a logical address space that is
bound to a separate physical address space is central to proper memory management.
Swapping:
A process must be in memory to be executed. A process, however, can be swapped temporarily out of
memory to a backing store and then brought back into memory for continued execution (Figure 8.5).
Swapping makes it possible for the total physical address space of all processes to exceed the real
physical memory of the system, thus increasing the degree of multiprogramming in a system.
Swapping requires a backing store. Standard swapping involves moving processes between main
memory and a backing store. The backing store is commonly a fast disk. It must be large enough to
accommodate copies of all memory images for all users, and it must provide direct access to these
memory images. 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. It then reloads registers and transfers
control to the selected process.
The context-switch time in such a swapping system is fairly high. To get an idea of the context-switch
time, let’s assume that the user process is 100 MB in size and the backing store is a standard hard disk
with a transfer rate of 50 MB per second. The actual transfer of the 100-MB process to or from main
memory takes
The swap time is 200 milliseconds. Since we must swap both out and in, the total swap time is about
4,000 milliseconds.
SSK 3
Unit-4
The memory is usually divided into two partitions: one for the resident operating system and one for
the user processes. We can place the operating system in either low memory or high memory. The
major factor affecting this decision is the location of the interrupt vector. Since the interrupt vector is
often in low memory, programmers usually place the operating system in low memory as well.
In contiguous memory allocation, each process is contained in a single section of memory that is
contiguous to the section containing the next process.
Fixed Partition
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. When the process terminates, the partition becomes available for
another process. This method was originally used by the IBM OS/360 operating system (called MFT) but
is no longer in use.
Variable Partition
In the 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.
As processes enter the system, they are put into an input queue. The operating system takes into
account the memory requirements of each process and the amount of available memory space in
determining which processes are allocated memory. When a process is allocated space, it is loaded into
memory, and it can then compete for CPU time. When a process terminates, it releases its memory,
which the operating system may then fill with another process from the input queue.
Memory is allocated to processes until, finally, the memory requirements of the next process cannot
be satisfied—that is, no available block of memory (or hole) is large enough to hold that process. The
operating system can then wait until a large enough block is available, or it can skip down the input
queue to see whether the smaller memory requirements of some other process can be met.
When a process arrives and needs memory, the system searches the set for a hole that is large enough
for this process. If the hole is too large, it is split into two parts. One part is allocated to the arriving
process; the other is returned to the set of holes. When a process terminates, it releases its block of
memory, which is then placed back in the set of holes.
SSK 4
Unit-4
This procedure is a particular instance of the general dynamic storage- allocation problem
There are many solutions to this problem. The first-fit, best-fit, and worst-fit strategies are the ones
most commonly used to select a free hole from the set of available holes.
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. We can stop searching as soon as
we find a free hole that is large enough.
• 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.
• Worst fit: Allocate the largest hole. Again, we must search the entire list, unless it is sorted by size.
This strategy produces the largest leftover hole, which may be more useful than the smaller leftover
hole from a best-fit approach.
Simulations have shown that both first fit and best fit are better than worst fit in terms of decreasing
time and storage utilization.
SSK 5
Unit-4
Segmentation
Each segment has a name and a length. The addresses specify both the segment name and the offset
within the segment. The programmer therefore specifies each address by two quantities: a segment
name and an offset.
For simplicity of implementation, segments are numbered and are referred to by a segment number,
rather than by a segment name. Thus, a logical address consists of a two tuple:
<segment-number, offset>.
Normally, when a program is compiled, the compiler automatically constructs segments reflecting the
input program.
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, and 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.
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 thus essentially an array of base–limit register pairs.
SSK 6
Unit-4
Paging
Paging is a memory-management scheme that permits the physical address space of a process to be
non-contiguous. Paging avoids the considerable problem of fitting memory chunks of varying sizes Into
the backing store.
Traditionally, support for paging has been handled by hardware. However, recent designs have
Implemented paging by closely integrating the hardware and operating system, especially on 64-bit
microprocessors.
The basic method for implementing paging involves breaking physical mem- ory 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 from their source (a file system
or the backing store). The backing store is divided into fixed-sized blocks that are the same size as the
memory frames or clusters of multiple frames. This rather simple idea has great functionality and wide
ramifications. For example, the logical address space is now totally separate from the physical address
space, so a process can have a logical 64-bit address space even though the system has less than 264
bytes of physical memory.
The hardware support for paging is illustrated in Figure 8.10. 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 paging model of memory is shown in Figure 8.11.
The size of a page is a power of 2, varying between 512 bytes and 1 GB per page, depending on the
computer architecture.
SSK 7
Unit-4
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:
where p is an index into the page table and d is the displacement within the page.
Virtual Memory
Virtual memory is a technique that allows the execution of processes that are not completely in
memory. One major advantage of this scheme is that programs can be larger than physical memory.
This technique frees programmers from the concerns of memory-storage limitations. Virtual memory
also allows processes to share files easily and to implement shared memory.
Virtual memory involves the separation of logical memory as perceived by users from physical memory.
This separation allows an extremely large virtual memory to be provided for programmers when only a
smaller physical memory is available (Figure 9.1). Virtual memory makes the task of programming much
easier, because the programmer no longer needs to worry about the amount of physical memory
available; she can concentrate instead on the problem to be programmed.
The physical memory may be organized in page frames and that the physical page frames assigned to a
process may not be contiguous. It is up to the memory management unit (MMU) to map logical pages
to physical page frames in memory.
The virtual address space of a process refers to the logical (or virtual) view of how a process is stored
in memory. Typically, this view is that a process begins at a certain logical address—say, address 0—
and exists in contiguous memory, as shown in Figure 9.2.
SSK 8
Unit-4
In Figure 9.2 that we allow the heap to grow upward in memory as it is used for dynamic memory
allocation. Similarly, we allow for the stack to 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. In addition to separating logical memory from physical
memory, virtual
memory allows files and memory to be shared by two or more processes through page sharing
Demand Paging
Loading the entire program into memory results in loading the executable code for all options,
regardless of whether or not an option is ultimately selected by the user. An alternative strategy is to
load pages only as they are needed. This technique is known as demand paging and is commonly used
in virtual memory systems.
With demand-paged virtual memory, pages are loaded only when they are demanded during program
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 (Figure 9.4) where processes
reside in secondary memory (usually a disk). When we want to execute a process, we swap it into
memory. Rather than swapping the entire process into memory, though, we use a lazy swapper.
A lazy swapper never swaps a page into memory unless that page will be needed. In the context of a
demand-paging system, use of the term “swapper” is technically incorrect. A swapper manipulates
entire processes, whereas a pager is concerned with the individual pages of a process. We thus use
“pager,”
rather than “swapper,” in connection with demand paging.
SSK 9
Unit-4
When a process is to be swapped in, the pager guesses which pages will be used before the process is
swapped out again. Instead of swapping in a whole process, the pager brings only those pages into
memory. Thus, it avoids reading into memory pages that will not be used anyway, decreasing the swap
time and the amount of physical memory needed.
The valid–invalid bit scheme described in Section 8.5.3 can be used for this purpose. This time, however,
when this bit is set to “valid,” the associated page is both legal and in memory. If the bit is set to
“invalid,” the page either is not valid (that is, not in the logical address space of the process) or is valid
but is currently on the disk. The page-table entry for a page that is brought into memory is set as usual,
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 disk.
SSK 10
Unit-4
In an operating system, page replacement refers to a scenario in which a page from the main memory
should be replaced by a page from the secondary memory. Page replacement occurs due to page faults.
Over-allocation of memory manifests itself as follows. While a user process is executing, a page fault
occurs. The operating system determines where the desired page is residing on the disk but then finds
that there are no free frames on the free-frame list; all memory is in use. The operating system has
several options at this point. It could terminate the user process. However, demand paging is the
operating system’s attempt to improve the computer system’s utilization and throughput. Users should
not be aware that their processes are running on a paged system—paging should be logically
transparent to the user. So this option is not the best choice.
The operating system could instead swap out a process, freeing all its frames and reducing the level of
multiprogramming. This option is a good one in certain circumstances, and we consider it further in
Section 9.6.
SSK 11
Unit-4
Algorithm
Notice that, if no frames are free, two page transfers (one out and one in) are required. This situation
effectively doubles the page-fault service time and increases the effective access time accordingly.
When we select a page for replacement, we examine its modify bit. If the bit is set, we know that the
page has been modified since it was read in from the disk. In this case, we must write the page to the
disk. If the modify bit is not set, however, the page has not been modified since it was read into memory.
In this case, we need not write the memory page to the disk: it is already there. This technique also
applies to read-only pages (for example, pages of binary code). Such pages cannot be modified; thus,
they may be discarded when desired. This scheme can significantly reduce the time required to service
a page fault, since it reduces I/O time by one-half if the page has not been modified.
SSK 12