0% found this document useful (0 votes)
80 views48 pages

OS - Chapter - 4 - Memory Management

The document discusses memory management in operating systems. It describes how operating systems abstract the physical memory hierarchy into a useful model for programs. It covers different memory management models from simple to sophisticated. It specifically focuses on how main memory can be managed through different approaches like having no memory abstraction, running programs without abstraction using protection keys, introducing the abstraction of address spaces using base and limit registers, and using swapping to manage processes in memory when total RAM needs exceed physical memory size.

Uploaded by

abelcreed1994
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views48 pages

OS - Chapter - 4 - Memory Management

The document discusses memory management in operating systems. It describes how operating systems abstract the physical memory hierarchy into a useful model for programs. It covers different memory management models from simple to sophisticated. It specifically focuses on how main memory can be managed through different approaches like having no memory abstraction, running programs without abstraction using protection keys, introducing the abstraction of address spaces using base and limit registers, and using swapping to manage processes in memory when total RAM needs exceed physical memory size.

Uploaded by

abelcreed1994
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

Operating Systems and System Programming

Meti Dejene
[email protected]
Haramaya University
Chapter 4 – Memory Management
Introduction
 Memory management mainly deals with how operating systems create abstractions from
memory and how they manage them.

 There is a memory hierarchy in computer systems.

 a few megabytes of very fast, expensive, volatile cache memory,

 a few gigabytes of medium-speed, medium-priced, volatile main memory, and

 a few terabytes of slow, cheap, nonvolatile magnetic or solid-state disk storage, not to
mention removable storage, such as DVDs and USB sticks.

 It is the job of the operating system to abstract this hierarchy into a useful model and then
manage the abstraction.
Cont.
 The part of the operating system that manages the memory hierarchy is called the memory
manager.

 Its job is to efficiently manage memory:

 keep track of which parts of memory are in use,

 allocate memory to processes when they need it, and

 deallocate it when they are done.

 There are several different memory management models, ranging from very simple to
highly sophisticated.

 We will focus on the programmer’s model of main memory and how it can be managed.
No Memory Abstraction Scheme

 The simplest memory abstraction is to have no abstraction at all.

 Early mainframe computers (before 1960), early minicomputers (before 1970), and
early personal computers (before 1980) had no memory abstraction.

 In this model every program simply sees the physical memory.

 When a program executed an instruction like

MOV REGISTER1,1000

the computer just move the contents of physical memory location 1000 to REGISTER1.

 Thus, the model of memory presented to the programmer was simply physical memory, a
set of addresses from 0 to some maximum.
Cont.
 In these conditions, it was not possible to have two running user programs in memory at
the same time.

 Overall, when the system is organized in this way, generally only one user process at a
time can be running.

 As soon as the user types a command, the operating system copies the requested
program from disk to memory and executes it.

 When the process finishes, the operating system displays a prompt character and
waits for a user new command.

 When the operating system receives the command, it loads a new program into
memory, overwriting the first one.
Cont.

 One way to get some parallelism in a system with no memory abstraction is to program
with multiple threads.

 This is possible because all threads in a process are supposed to see the same
memory image.

 However, the problem is what people often want is unrelated programs to be running
at the same time, something the threads abstraction does not provide.
Running Programs Without a Memory Abstraction

Option 1

 The operating system has to save the entire contents of memory to a disk file, then bring in
and run the next program.

 As long as there is only one program at a time in memory, there are no conflicts when
memory referencing instructions are executed.
Cont.
Option 2

 With the addition of some special hardware (as in the early models of the IBM 360)

 In this approach, Memory was divided into 2-KB blocks and each was assigned a 4-bit
protection key held in special registers inside the processor.

 So, a machine with a 1-MB memory needed only 512 of these 4-bit registers for a total
of 256 bytes of key storage.

 Any attempt by a running process to access memory with a protection code different
from its own is forbidden.

 Since only the operating system could change the protection keys, user processes were
prevented from interfering with one another and with the operating system itself.
Cont.
 Nevertheless, this solution had a major drawback.

 For example, if we have two programs, each 16 KB in size, as shown in Fig. 3-2(a) and (b).

 The first program starts out by jumping to address 24, which contains a MOV
instruction.

 The second program starts out by jumping to address 28, which contains a CMP
instruction.

 When the two programs are loaded consecutively in memory starting at address 0, we
have the situation of Fig. 3-2(c).

 After the programs are loaded, they can be run.

 Since they have different memory keys, neither one can damage the other.
Cont.
 But the problem is when the first program starts, it executes the JMP 24 instruction, which
jumps to the instruction, as expected.  This program functions normally.

 However, after the first program has run long enough, the operating system may decide
to run the second program, which has been loaded above the first one, at address
16,384.

 The first instruction executed is JMP 28, which jumps to the ADD instruction in the first
program, instead of the CMP instruction it is supposed to jump to.  The program will most
likely crash. Why?

 Because the two programs both reference absolute physical memory.


A Memory Abstraction: Address Spaces
 To allow multiple applications to be in memory at the same time without interfering with
each other two problems have to be solved : protection and relocation.

 So, a better solution is to invent a new abstraction for memory: the address space.

 The notion of address space is to create a kind of abstract memory for programs to live in.

 An address space is the set of addresses that a process can use to address memory.

 Each process has its own some set of address space it can use, typically running from 0
up to some maximum, independent of those belonging to other processes.

 So, having a separate address space

 Determines the legal addresses that the process can access legally

 Ensures that a process can access only these legal addresses.


Base and Limit Registers
 The question is how to give each program its own address space ?

 The answer is Dynamic Relocation using Base and Limit registers.

 The classical solution is to equip CPU with two special hardware registers, usually called the
base and limit registers.

 When a process is run,

 the base register is loaded with the physical address where its program begins in
memory and

 the limit register is loaded with the length of the program.


Cont.
 Then, every time a process executes an instruction that references memory, either to
fetch an instruction or read or write a data word,

 the CPU hardware automatically adds the value in the base register to the address
generated by the user process before sending the address out on the memory bus.

 This whole technique of the operating system, to load a program into memory at any
available location and adjust the program's memory references accordingly during
runtime is what we call Dynamic relocation.

 Programs are loaded into memory locations wherever there is room and without
relocation during loading.

 In short, what Dynamic relocation does is map each process’ address space onto a
different part of physical memory in a simple way.
Cont.
 In such a way,

1. A pair of base and limit registers, define the range of legal addresses that a process
may access.

2. Protection of memory space is accomplished by having the CPU hardware compare


and check address generated during relocation with respect to the limit register.

 Every address generated during relocation must fall within the range specified by
the limit register.

 A trap is generated for any attempt by a user process to access beyond the limit.

 Meaning a fault is generated and the access is aborted.


Cont.
Example

 Based on Fig. 3-2(c), the base and limit values that would be loaded into these hardware
registers when the first program is run are 0 and 16,384, respectively.

 The values used when the second program is run are 16,384 and 32,768, respectively.

 If a third 16-KB program were loaded directly above the second one and run, the base
and limit registers would be 32,768 and 16,384.

 Thus, in the case of the first instruction of the second program in Fig. 3-2(c), the process
executes a
JMP 28
instruction, but the hardware treats it as though it were
JMP 16412
so it lands on the CMP instruction as expected.
Cont.

 So, using base and limit registers is an easy way to give each process its own private

address space because every memory address generated automatically has the base-

register contents added to it before being sent to memory.


Swapping

 In practice, the total amount of RAM needed by all the processes is often much more
than what can fit in memory.

 Consequently, keeping all processes in memory all the time requires a huge amount of
memory and cannot be done if there is insufficient memory.

 Two general approaches to deal with memory overload have been developed over the
years.

 Swapping and Virtual memory


Cont.
 Swapping is the process of temporarily taking a process out of main memory by writing
(putting)it into secondary memory to make space for another process and then bringing
another new process for execution or previously swapped process for continued execution.
Cont.

 A space on secondary memory where temporarily


swapped processes reside is called a Backing store or
Swap space.

 Swapping is required

 when there is no sufficient memory space to further


accommodate additional processes.

 when there is no sufficient memory space to


accommodate growing need of space by currently
executing processes.
Cont.
 Sometimes swapping creates multiple free memory spaces broken into little pieces that

are not contiguous and not large enough to hold a process.

 This is known as fragmentation.

 Fragmentation is of two types:

 External fragmentation: arises when total memory space is enough to satisfy a request or

to reside a process in it, but it is not contiguous, so it cannot be used.

 Internal fragmentation: occurs when memory blocks allocated to processes are larger

than necessary, and the allocated space isn't fully utilized resulting in unused wasted

space within the partition.


Cont.

 When this happens it is possible to shuffle memory contents by moving all processes

toward one end of the memory so as to place all free memory together in one large block

and produce one large hole of available memory.

 This technique is known as memory compaction.


Cont.
 Another question is how much memory should be allocated for a process when it is
created or swapped in? The answer is:

1. If processes are created with a fixed size that never changes, then the allocation is simple:
the operating system allocates exactly what is needed, no more and no less.

2. If, however, processes’ data segments can grow, a problem might occur whenever a
process tries to grow.

 Therefore, to avoid this, if it is expected that processes will grow as they run,

 It is a good idea to allocate a little extra memory whenever a process is swapped in or


moved, to reduce the overhead associated with moving or swapping processes that
no longer fit in their allocated memory.
Managing Free Memory

 The operating systems way to keep track of assigned and free memory.

 There are two methods to keep track of memory usage:

I. Using Bitmaps and

II. Using Linked lists


Memory Management with Bitmaps

 With a bitmap, memory is divided into allocation units as small as a few words and

as large as several kilobytes.

 Corresponding to each allocation unit is a bit in the bitmap, which is 0 if the unit is

free and 1 if it is occupied (or vice versa).


Memory Management with Linked Lists
 Maintain a linked list of allocated and free memory segments, where a memory unit either

contains a process or is an empty hole between two processes.

 Each entry in the list specifies a hole (H) or process (P), the address at which it starts, the

length, and a pointer to the next item.

 A terminating process normally has two neighbors.

 These may be either processes or holes, leading to the four combinations shown in Fig.

3-7.
Memory Allocation Algorithms
 Several algorithms can be used to allocate memory for a created process or an existing
process being swapped in from disk.

I. First fit

 The memory manager scans along the memory units until it finds a hole that is big
enough.

 Simple and fast because it searches as little as possible.

II. Next fit

 It works the same way as first fit, except that it keeps track of where it is whenever it
finds a suitable hole.

 The next time it is called to find a hole, it starts searching the list from the place
where it left off last time, instead of always at the beginning, as first fit does.
Cont.
III. Best fit

 Well-known and widely used.

 Searches the entire memory, from beginning to end, and takes the smallest hole that is
adequate enough a given process.

 Rather than breaking up a big hole that might be needed later, best fit tries to find a hole
that is close to the actual size needed, to best match the request and the available holes.

IV. Worst fit

 always take the largest available hole, so that the new hole will be big enough to be
useful later.

V. Quick fit

 Maintains separate lists for some of the more common sizes requested.
Quiz - 1
1. Which memory allocation algorithm searches the entire memory from
beginning to end and allocates the smallest hole that is adequate enough
for a process?

2. What are the 2 methods used by the operating system to keep track of
allocated and free memory units?

3. What is memory compaction?

4. The process of temporarily taking a process out of main memory by writing


(putting)it into secondary memory to make space for another process and
then bringing other new process for execution or previously swapped
process for continued execution is called ______________________________.
Memory Partitioning Scheme
1. Fixed Memory Partitioning

 In here, the memory is divided into fixed-size partitions or blocks.

 These partitions remain static and do not change in size during execution of the system.

 Partitions created can be equally sized or unequally sized (different size).

 When a process arrives, it is allocated a partition or a number of consecutive partitions


that is large enough to accommodate it.

 Advantages: Simple to implement, less overhead in managing memory.

 Disadvantages: (1) Potential for internal fragmentation If a process is smaller than the
partition size and (2) inefficient utilization of memory as smaller processes might occupy
larger partitions, limiting the number of processes that can be accommodated
simultaneously.
Fixed partitioning Scheme

Equally sized partition Unequally sized partition


Cont.
2. Dynamic Memory Partitioning

 In here, memory is allocated and deallocated based on the actual memory requirements

of processes.

 It involves dividing memory into variable-sized partitions that can change dynamically

based on the size of the arriving processes.

 Advantages: Efficient utilization of memory, reduced internal fragmentation as partitions

adjust to process sizes, flexibility in accommodating varying process sizes.

 Disadvantages: Overhead in managing variable-sized partitions, potential for external

fragmentation (smaller unused memory blocks scattered throughout memory).


Paging
 Paging is a memory management scheme that permits a process’s physical address

space to be noncontiguous.

 The basic idea behind paging is that each program has its own address space, which is

broken up into fixed-size chunks (units) called pages.

 Each page is a contiguous range of addresses and has assigned page number.

 Physical memory is broken up into fixed-sized blocks called frames or page frames.

 Pages and page frames are generally of the same size.

 When a process is to be executed, its pages are mapped and loaded into any

available free physical memory frames.


Before allocation After allocation
Page Table
 To achieve this, the operating system maintains

 A list of free frames and

 A page table per process

 The page table shows which page is in which frame and it contains base address of

each page in physical memory.

 The page number is used as an index into the page table, yielding the number

of the page frame corresponding to that page.

 From the page table entry, the page frame number (if any) is found.

 Looking up the mapping in the page table hierarchy is known as a page table walk.
Structure of a Page Table Entry
Cont.
 Protection bits

 tell what kinds of access are permitted (with 0 for read/write and 1 for read only).

 Modified bit (aka dirty bit)

 If the page has been modified (i.e., is ‘‘dirty’’), it must be written back to the disk.

 If it has not been modified (i.e., is ‘‘clean’’), it can just be abandoned, since the disk
copy is still valid.

 Referenced bit: Set whenever a page is referenced, either for reading or for writing.

 used to help the operating system choose a page to evict when a page fault occurs.

 Pages that are not being used are better candidates than pages that are, and this bit
plays an important role in page replacement algorithms.

 Cache disable bit allows caching to be disabled or enabled for the page.
Cont.
 A Present/absent bit indicates if a page is physically present in the main memory i.e.
mapped or not.

 What happens if the program references an unmapped address during execution?

 A page fault happens.

 In such cases the operating system

 picks a page frame to evict and writes its contents back to the disk,

 then fetches and maps the page that was just referenced into the page frame just
freed, changes the map, and restarts instruction.
Virtual Memory
 While memory sizes are increasing rapidly, software sizes are increasing much faster.

 As a consequence, there is a need to run programs that are too large to fit in memory.

 The question is how to manage this bloatware?

 One solution is the method of virtual memory.

Virtual Memory is a technique of repurposing part of secondary memory mainly disk


storage to use it as extension of RAM.

 This disk space designated for this purpose is called virtual memory and it has similar
structure with RAM.

 Addresses in virtual memory are called virtual addresses and form the virtual address
space.
How Virtual Memory Works
 Implementation of Virtual Memory is achieved using the technique of Paging.

 Large programs are broken down into pages and stored on an area designated as
Virtual memory.

 Then depending on the free space available on the main memory, pages from the
virtual memory are mapped onto the physical memory frames for execution.

 The rest will await in the virtual memory until they are required for execution.

 Page table holds page mapping information.

 MMU (Memory Management Unit) maps the virtual addresses onto the physical
memory addresses.

 This approach allows execution of process which are not completely in main memory.
Logical implementation of Virtual Memory
Segmentation
 A given program layout has different sections (the executable program code, program

data, program stack and others).

 Segmentation is another memory management technique in which memory is divided

into variable size segments based on this logical structure/layout of a program.

 So each segment contains different parts of a program.

 For example, a code segment may contain the actual code instructions, a data

segment may store initialized data, and a stack segment may handle function calls

and local variables.


Logical View of Segmentation

Segment Number

You might also like