Memory Management - Operating Systems, Study notes of Operating Systems

Memory Management - Operating Systems

Typology: Study notes

2017/2018

Uploaded on 03/08/2018

anil-reddy-maram
anil-reddy-maram 🇮🇳

5

(1)

2 documents

1 / 35

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
MEMORY MANAGEMENT
Memory Management
Memory consists of a large array of words or bytes, each with its own address. The CPU
fetches instructions from memory according to the value of the program counter. These
instructions may cause additional loading from and storing to specific memory addresses.
Memory unit sees only a stream of memory addresses. It does not know how they are
generated.
Program must be brought into memory and placed within a process for it to be run.
Input queue – collection of processes on the disk that are waiting to be brought into
memory for execution.
User programs go through several steps before being run.
Address Binding
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23

Partial preview of the text

Download Memory Management - Operating Systems and more Study notes Operating Systems in PDF only on Docsity!

MEMORY MANAGEMENT

Memory Management

  • Memory consists of a large array of words or bytes, each with its own address. The CPU fetches instructions from memory according to the value of the program counter. These instructions may cause additional loading from and storing to specific memory addresses.
  • Memory unit sees only a stream of memory addresses. It does not know how they are generated.
  • (^) Program must be brought into memory and placed within a process for it to be run.
  • Input queue – collection of processes on the disk that are waiting to be brought into memory for execution.
  • User programs go through several steps before being run.

Address Binding

Address binding of instructions and data to memory addresses can happen at three different stages.

Compile time : if memory location known a priori, absolute code can be generated; must recompile code if starting location changes. Example: .COM-format programs in MS-DOS. Load time : Must generate re-locatable code if memory location is not known at compile time. Execution time : Binding delayed until run time if the process can be moved during its execution from one memory segment to another. Need hardware support for address maps (e.g., relocation registers).

  • (^) 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 the desired routine into memory and to update the program‘s address tables to reflect this change. Then control is passed to the newly loaded routine.
  • Better memory-space utilization; unused routine is never loaded.
  • Useful when large amounts of code are needed to handle infrequently occurring cases.
  • No special support from the operating system is required.
  • Implemented through program design.

Dynamic Linking

  • Linking is postponed until execution time.
  • Small piece of code, stub, is used to locate the appropriate memory-resident library routine, or to load the library if the routine is not already present.
  • When this stub is executed, it checks to see whether the needed routine is already in memory. If not, the program loads the routine into memory.
  • Stub replaces itself with the address of the routine, and executes the routine.
  • Thus the next time that code segment is reached, the library routine is executed directly, incurring no cost for dynamic linking.
  • Operating system is needed to check if routine is in processes memory address.
  • (^) Dynamic linking is particularly useful for libraries.

Overlays:-

To enable the process to be larger than the amount of memory to it, we can use overlays.

The idea of overlays is to keep in memory only those instructions and data that are needed at any given time.

When other instructions are needed, they are loaded in to space occupied previously by instructions that are no longer needed.

Swapping

A process can be swapped temporarily out of memory to a backing store, and then brought back into memory for continued execution.

  • Roll out, roll in – swapping variant used for priority-based scheduling algorithms. If a higher priority process arrives and wants service, the memory manager can swap out the lower priority process so that it can load and execute lower priority process can be swapped back in and continued. This variant is sometimes called roll out, roll in.
  • Backing store – fast disk large enough to accommodate copies of all memory images for all users; must provide direct access to these memory images. 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 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 not, and 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 as normal and transfers control to the selected process. Major part of swap time is transfer time; total transfer time is directly proportional to the amount of memory swapped. Modified versions of swapping are found on many systems (i.e., UNIX, Linux, and Windows).

Single-Partition Allocation (Single Contiguous Allocation)

  • Relocation-register scheme used to protect user processes from each other, and from changing operating-system code and data
  • Relocation register contains value of smallest physical address; limit register contains range of logical addresses – each logical address must be less than the limit register
  • The operating system usually resides in either the upper or lower part of the memory. A job is assigned to total memory although it needs and uses only a small fraction. The job has complete control of the CPU, until completion or an error occurs. This allocation is often used for small and inexpensive computing systems. The major problem with this method is that the resources are usually not managed in an efficient manner. There is not enough hardware flexibility to allow efficient allocation of memory

Multiple-partition Allocation

In this method, when a partition is free, a process is selected from input queue and loaded in to the free partition. When the process terminates, the partition becomes available for another process

One of the simplest methods for allocating memory is to divide memory into several fixed sized partitions. There are two variations of this.

Fixed size Partitions

▲ Each Partition has exactly one process. ▲ Degree of multi-programming – bound to - No. of partitions ▲ When a partition is free, a process is selected from the input queue and is loaded into free partition. ▲ When the process terminates, the partition becomes available for another process. ▲ ·This restricts both the number of simultaneous processes and the maximum size of each process, and is no longer used.

Fixed Variable Size Partitions

▲ Keeps a table of unused (free) memory blocks (holes), and to find a hole of a suitable size whenever a process needs to be loaded into memory. ▲ (^) Hole – block of available memory; holes of various size are scattered throughout memory. ▲ When a process arrives, it is allocated memory from a hole as large enough to accommodate it. ▲ Operating system maintains information about: a) Allocated partitions b) Free partitions (hole) ▲ A set of holes of various sizes, is scattered throughout memory at any given time. When a process arrives and needs memory, the system searches this set for a hole that is large enough for this process. If the hole is too large, it is split into two: 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. If the new hold is adjacent to other holes, these adjacent holes are merged to form one larger hole. ▲ This procedure is a particular instance of the general dynamic storage allocation problem , which is how to satisfy a request of size n from a list of free holes. There are many solutions to this problem. The first-fit, best-fit and worst-fit strategies are the most common ones used to select a free hole from the set of available holes. First-fit: Allocate the first hole that is big enough.

  • This occurs when the requested memory is available at different locations in the memory i.e., the requested space is available, but not as a whole
  • Suppose a new process arrives and needs 100 words space is available but not as whole.
  • This is known as External Fragmentation

FIG: - External Fragmentation

The solution to external fragmentation is known as compaction.

Compaction : -

  • Shuffle memory contents to place all free memory together in one large block.
  • Compaction is possible only if relocation is dynamic, and is done at execution time
  • Compaction helps to solve the problem of fragmentation, but it requires too much of CPU time.

Internal Fragmentation:

  • The allocated memory may be slightly larger than requested memory; this size difference is memory internal to a partition, but not being used.
  • (^) Suppose a process arrives in to system and it needs 300 KB. We accommodate it in the main memory and free memory is 12 KB. The difference between the block length and request is known as internal fragmentation
  • The paging is one of the solution to the internal fragmentation

FIG: - Internal Fragmentation

Paging

  • Paging is a memory management scheme that permits the physical address space of a process to be non-contiguous.
  • Divide physical memory into fixed-sized blocks called frames (size is power of 2, for example 512 bytes).
  • Divide logical memory into blocks of same size called pages. When a process is to be executed, its pages are loaded into any available memory frames from the backing store.

Fig: - Paging Model of Logical and Physical Memory

  • Keep track of all free frames.
  • To run a program of size n pages, need to find n free frames and load program.
  • Set up a page table to translate logical to physical addresses.
  • Internal fragmentation may occur.

Let us take an example.

Consider the following micro example, in which a process has 16 bytes of logical memory, mapped in 4 byte pages into 32 bytes of physical memory. (Presumably some other processes would be consuming the remaining 16 bytes of physical memory.)

Figure - Paging example for a 32-byte memory with 4-byte pages

  • Note that paging is like having a table of relocation registers, one for each page of the logical memory.
  • There is no external fragmentation with paging. All blocks of physical memory are used, and there are no gaps in between and no problems with finding the right sized hole for a particular chunk of memory.
  • There is, however, internal fragmentation. Memory is allocated in chunks the size of a page, and on the average, the last page will only be half full, wasting on the average half a page of memory per process. ( Possibly more, if processes keep their code and data in separate pages. )
  • Larger page sizes waste more memory, but are more efficient in terms of overhead. Modern trends have been to increase page sizes, and some systems even have multiple size pages to try and make the best of both worlds.
  • Page table entries ( frame numbers ) are typically 32 bit numbers, allowing access to 2^32 physical page frames. If those frames are 4 KB in size each, that translates to 16 TB of addressable physical memory. ( 32 + 12 = 44 bits of physical address space. )
  • 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

FIG:- Paging Hardware With TLB

  • 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, its frame number is immediately available and is used to access memory. The whole task may take less than 10 percent longer than it would if an unmapped memory reference were used.
  • If the page number is not in the TLB (known as a 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.

Hit Ratio

Hit Ratio: The percentage of times that a page number is found in the associative registers or TLB is called HIT RATIO

For example, if it takes 20 nanoseconds to search the associative memory and 100 nanoseconds to access memory; for a 98-percent hit ratio, we have

Effective memory-access time = 0.98 x 120 + 0.02 x 220 = 122 nanoseconds.

Valid or Invalid Bit In A Page Table

  1. Memory protection implemented by associating protection bit with each frame.
  2. Valid-invalid bit attached to each entry in the page table:
    • “Valid” indicates that the associated page is in the process‘ logical address space, and is thus a legal page.
    • “Invalid” indicates that the page is not in the process‘ logical address space.
  3. Pay attention to the following figure. The program extends to only address 10,468, any reference beyond that address is illegal. However, references to page 5 are classified as valid, so accesses to addresses up to 12,287 are valid. This reflects the internal fragmentation of paging.

Figure - Valid (V) Or Invalid (I) Bit In Page Table

Address-translation scheme for a two-level 32-bit paging architecture is shown in below figure

Fig: - Address translation for a two-level 32-bit paging architecture

Hashed Page Tables:

A common approach for handling address spaces larger than 32 bits is to use a hashed page table, with the hash value being the virtual page number. Each entry in the hash table contains a linked list of elements that has to the same location.

Each element consists of three fields:

(a) The virtual page number (b) The value of the mapped page frame (c) A pointer to the next element in the linked list.

The algorithm works as follows:

The virtual page number in the virtual address is hashed into the hash table. The virtual page number is compared to field (a) in the first element in the linked list. If there is a match, the corresponding page frame (field (b)) is used to form the desired physical address. If there is no match, subsequent entries in the linked list are searched for a matching virtual page number.

The scheme is shown in below figure – Hash Page Table

Inverted Page Table:

  • One entry for each real page (frame) of memory.
  • Entry consists of the virtual address of the page stored in that real memory location, with information about the process that owns that page.
  • There is only one page table in the system. Not per process.
  • Decreases memory needed to store each page table, but increases time needed to search the table when a page reference occurs.