0% found this document useful (0 votes)
37 views

9th Lecture OS

Uploaded by

farwakhalid146
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

9th Lecture OS

Uploaded by

farwakhalid146
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Operating System

CS(407)

Lecture 9
Memory management

Instructor
Sibgha Zia
Department of Computer Science
UAF Sub Campus Burewala
Memory management
 Memory management is the functionality of an operating system which manages
primary memory and moves processes back and forth between main memory
and disk during execution.

 Memory Management is the process of controlling and coordinating computer


memory, assigning portions known as blocks to various running programs to
optimize the overall performance of the system.

 It checks how much memory is to be allocated to processes. It decides which process


will get memory at what time.
Why Memory Management Required

 Allocation and de-allocate memory before and process


execution.
 To minimize fragmentation issue.
 To proper utilization of main memory.
 To maintain data integrity while execution of process.
Memory Management Scheme
Memory-management unit, MMU
 The run time mapping of logical to physical addresses is handled by
the memory-management unit, MMU.

 The MMU can take on many forms.


 One of the simplest is a modification of the base-register scheme.

 The base register is now termed a relocation register, whose value is added
to every memory request at the hardware level.
 Note that
 User programs never see physical addresses.
 User programs work entirely in logical address space, and any
memory references or manipulations are done using purely logical
addresses. Only when the address gets sent to the physical memory
chips is the physical memory address generated
Logical vs. Physical Address Space
● Logical address – generated by the CPU; also referred to as virtual address
● Physical address – address seen by the memory unit
 Logical and physical addresses are same in compile-time and load-time address-binding

schemes;
 Logical (virtual) and physical addresses differ in execution-time address-binding scheme.

● Logical address space is the set of all logical addresses generated by a program
● Physical address space is the set of all physical addresses generated by a program

Compile time address binding is done before loading the program into memory.
Load time address binding is done after loading the program into memory.
Different types of memory management
 Single partition allocation :one processes in memory.
 No operation
 Operation system + one process

 Multiple partition allocation : several processes in memory


 Fixed size partitions
 Variable size partitions
Memory management strategies

 Placement strategies
Where in primary memory should data be placed.
 First fit.

 Best fit.

 Worst fit.

 Replacement strategies

Which pages should be replaced, if primary memory is full.


 Fetch strategies

 When to bring data into primary memory.


Memory allocation Methods
 There are two techniques for memory allocation and these are as
follows:
 Contiguous memory allocation
 Non-Contiguous memory allocation
Contiguous memory allocation
 Contiguous memory allocation ”allocates consecutive blocks of memory to a
file/process.”
 When a process requests for the memory, a single contiguous section of memory
blocks is assigned to the process according to its requirement.
 Dividing the memory into the fixed-sized partition.

 For example, if a user process needs some x bytes of contiguous memory, then all
the x bytes will reside in one place in the memory that is defined by a range of
memory addresses: 0x0000 to 0x00FF.
Non-Contiguous memory
 Non-Contiguous memory allocation allocates separate blocks of
memory to a file/process.
 In this scheme, the process is divided into several blocks and then
these blocks are placed in different parts of the memory according to
the availability of memory space.

 Non contiguous memory allocation methods include Paging and


Segmentation.
Contiguous Memory Non-Contiguous Memory
Swapping

 “Swapping is a memory management technique that swaps a process


into main memory to execute it and then swaps it out to the backing
storage when required”.
Swapping
 A process must be loaded into memory in order to execute.
 If there is not enough memory available to keep all running processes in memory at
the same time, then some processes who are not currently using the CPU may have
their memory swapped out to a fast local disk called the backing store.

 The swapping is done by swapper.


 Suppose that a dispatcher tries to load a process into CPU and finds it is not
currently in memory. it will call the swapper to swap the process back into memory.
Systematic view of swapping

Swapping of two processes using a disk as a backing store


Swapping on Mobile Systems

 Swapping is typically not supported on mobile platforms, for several


reasons:
 Mobile devices typically use flash memory in place of more spacious hard
drives for persistent storage, so there is not as much space available.
 Flash memory can only be written to a limited number of times before it
becomes unreliable.
 The bandwidth to flash memory is also lower.
Paging
 Paging is a storage mechanism that allows OS to retrieve processes
from the secondary storage into the main memory in the form of
pages.
 In the Paging method, the main memory is divided into small fixed-size blocks,
which is called frames.
 Logical Memory is divided into same size blocks called Pages.

 The size of a frame should be kept the same as that of a page to have
maximum utilization of the main memory and to avoid external
fragmentation. Paging is used for faster access to data, and it is a logical
concept.
 Every logical address generated by CPU consists of two parts.
I. A page number (p)
II. A page displacement (d)
Page Replacement Algorithms
Example of Paging
 For example, if the main memory size is 16 KB and Frame size is 1 KB.
Here, the main memory will be divided into the collection of 16 frames
of 1 KB each.
 There are 4 separate processes in the system that is A1, A2, A3, and A4
of 4 KB each. Here, all the processes are divided into pages of 1 KB
each so that operating system can store one page in one frame.
 At the beginning of the process, all the frames remain empty so that all
the pages of the processes will get stored in a contiguous way
Paging Conceptual view

In this example you can see that A2 and A4 are moved to the waiting state after some time.
Therefore, eight frames become empty, and so other pages can be loaded in that empty blocks.
The process A5 of size 8 pages (8 KB) are waiting in the ready queue.
Fragmentation
 A fragmentation is define as when the process is loaded and
removed after execution from memory, it creates a small free
hole. These holes can not be assigned to now process because
holes are not combine or do not fulfill the memory
requirement of the process.
 To achieve a degree of multiprogramming, we must reduce
the waste of memory or fragmentation problem.
Types of Fragmentation
 There are two types of fragmentation called
 Internal fragmentation
 External fragmentation
Fixed Partitioning
Internal fragmentation
Internal fragmentation
 Occur when fixed size memory blocks are allocated to a process
 The memory block allocated to a process is larger than required.
It causes some portion of memory to left unused. This left space
cannot be allocated to another process.

 For example, if a process requires 2MB but it is allocated to a 3MB


memory block, then 1MB block is left unused. This 1MB cannot be
allocated to some other process, and it is a waste.
Internal Fragmentation
Dynamic Partitioning
Dynamic Partitioning example
Dynamic Partitioning example
External Fragmentation
 Occurs when variable size memory space are allocated.

 Example
 Suppose, we want to allocate memory to a process of size 8 MB and as In
the above diagram, we can see that, there is enough space that is 9 MB to
run a process of size 8MB but the memory that is fragments are not
contiguous.
 There is a solution for the problem of External Fragmentation and it
is Compaction.

External Fragmentation
External Fragmentation
Segmentation
 Segmentation is a memory management technique which divides
each process into several segments or sections of different sizes. A
segment can be a main program, function, stacks, symbol tables, data
structures, etc.
 Every segment is a different logical address space of the program.
These segments are of variable length size. In other words, the
segments are not fixed in size.
Segmentation

In this example, the process is


divided into three segments.

Size of segment 1 is 50KB


Segment 2 is of 100KB
Segment 3 is of 60KB.
Segment 1 is stored on address 11
Segment 2 is stored on address 13
Segment 3 is stored on address 14
Advantages

 Allocating memory is easy and cheap.


 More efficient swapping.
 Allows demand paging
 Eliminates external fragmentation.
Fragmentation vs. Segmentation
Task to do

 Internal fragmentation vs. external fragmentation.


 Paging vs. Segmentation.

 Fragmentation vs. Segmentation

You might also like