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

Week 12

Uploaded by

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

Week 12

Uploaded by

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

CS-303 Operating Systems

Dr. Samia Ijaz


Fall - 2024

Department of Computer
Science
HITEC University Taxila
Chapter 9: Main Memory
Background

● Program must be brought (from disk) into


memory to run
● Main memory and registers are only
storage CPU can access directly
● Register access in one cycle of CPU clock
(or less)
● Completing memory access may take
many cycles of the
clock.
● Memory stall may occur
● Cache sits between main memory and
CPU registers
Base and Limit Registers

● Each process has a separate


memory space.
● Separate per-process memory
space protects the processes from
each other
● A pair of base and limit registers
define the legal physical address
space of each process
● CPU must check every memory
access generated in user mode to
be sure it is between base and
limit for that user
Hardware Address Protection
Address Binding

● Address binding is the process of assigning


memory addresses to a program’s instructions
and data.
● Compile Time Binding (during compilation) If
the location in memory where the program will
run is already known at compile time, the
absolute addresses are directly generated.
● These addresses cannot change during
execution.
● If the program is fixed to start at memory
address 1000, all addresses in the program are
set relative to 1000 during compilation.
Address Binding

● Load Time Binding (when the program is


loaded into memory before execution): If the
memory location is unknown at compile time, the
compiler generates relocatable addresses
(relative addresses).
● The loader translates these relocatable addresses
into absolute addresses when loading the
program into memory.
● If a program uses address 356 (relative) and the
loader decides to place the program at address
12000, the actual (absolute) address will be:
● Absolute Address=12000+356=12356
Address Binding
● Run Time Binding:
4 f the process can move to a different memory location while
running, address binding is done at runtime.
4 The CPU generates a logical address (e.g., 356), and the
Memory Management Unit (MMU) translates it to a physical
address using a relocation register (base address).
4 CPU generates logical address 356.
4 MMU adds the base address 12000 (relocation register
value):
4 Physical Address=356+12000=12356
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 the 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
Memory-Management Unit (MMU)
Multistep Processing of a User Program
Dynamic Loading
Dynamic Loading
Dynamic Loading
Dynamic Loading
Dynamic Loading
Dynamic Loading
Dynamic Linking
Dynamic Linking
Dynamic Linking
Dynamic Linking
Swapping
● A process can be swapped temporarily out of
memory to a backing store, and then brought
back into memory for continued execution
● Total physical memory space of processes
can exceed physical memory
● Backing store – fast disk large enough to
accommodate copies of all memory images
for all users;
● Roll out, roll in – swapping variant used for
priority-based scheduling algorithms; lower-
priority process is swapped out so higher-
priority process can be loaded and executed
● Major part of swap time is transfer time; total
transfer time is directly proportional to the
amount of memory swapped
Swapping (Cont.)
● Does the swapped out process need to swap
back in to same physical addresses?
● Depends on address binding method
● Modified versions of swapping are found on
many systems (i.e., UNIX, Linux, and Windows)
● Swapping normally disabled
● Started if more than threshold amount of
memory allocated
● Disabled again once memory demand
reduced below threshold
Schematic View of Swapping
Context Switch Time including Swapping

● If next processes to be put on CPU is not in


memory, need to swap out a process and
swap in target process
● Context switch time can then be very high
● 100MB process swapping to hard disk with
transfer rate of 50MB/sec
● Time = process size / transfer rate.
● Swap out time of (100/50=2 sec) = 2000
ms
● Plus swap in of same sized process
● Total context switch swapping component
time of 4000ms (4 seconds)
Example

• User process size is 2048Kb.


• Data transfer rate is 1Mbps = 1024 kbps.
● What is the Total context switch swapping
component time?
Memory Allocation

OS 2m 4m 2m 3m 3
b b b b m
b
p p p p p
1 2 3 4 7

Memory Allocation

Contiguous Non- Contiguous


Memory Allocation

Memory Allocation

Contiguous Non- Contiguous

Fixed Dynamic Paging Segmentation


Partitioning Partitioning

Equal size Variable size


Partitioning Partitioning
Memory Allocation

● Contiguous memory allocation:


● A method in which a single
contiguous section/part of
memory is allocated to a
process
● Non-Contiguous memory
allocation:
● A method that allocates the
memory space present in
different locations to the
process as per it’s
requirements.
Memory Allocation

● Equal Size Fixed Partitioning:


● Equal blocks of memory

OS

8 MB

8 MB

8 MB
Memory Allocation

● Variable Size Fixed Partitioning:


● Variable sized blocks of memory
● Number of blocks and size of block
is fixed
OS

2 MB

10 MB

4 MB
4 MB
Memory Allocation

● Variable Size Fixed Partitioning:


● First-fit: Allocate the first hole
that is big enough

● Best-fit: Allocate the smallest


hole that is big enough; must
search entire list, unless ordered by
size

● Worst-fit: Allocate the largest


hole; must also search entire list
Fragmentation

● External Fragmentation – total memory


space exists to satisfy a request, but it is
not contiguous
● Internal Fragmentation – allocated
memory may be slightly larger than
requested memory; this size difference is
memory internal to a partition, but not
being used
Example
● Process P is of size 4 KB. How to fit the process in the two
available memory spaces of 2KB.
Fragmentation (Cont.)

● Reduce external fragmentation by


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
Memory Allocation

● Compaction:
● Compaction is a technique to collect all
the free memory present in form of
fragments into one large chunk of
free memory,
● which can be used to run other processes.
● Reduce external fragmentation by
compaction
● Move all the processes towards one end of
the memory and all the available free
space towards the other end of the
memory so that it becomes contiguous.
● Compaction is possible only if relocation is
dynamic, and is done at execution time
Memory Allocation

● Advantages of Compaction:
● Reduces external fragmentation
● Memory becomes contagious
● More processes can be loaded to
memory
● Disadvantages of Compaction:
● A huge amount of time is wasted in
performing compaction.
● CPU sits idle for a long time.
Memory Allocation

● Limitations of Fixed Partitioning:


● Internal Fragmentation
4 The process size is smaller than
the memory allocated to it so free
space is created in the memory
block which cannot be used by
any other process.
● External Fragmentation
4 Total available space is sufficient
to hold a process in it but it isn’t
contiguous; thus cannot be used
● Fixed Degree of multiprogramming
Memory Allocation

● Dynamic Partitioning / Dynamic


Memory Allocation:
● The process of assigning the
memory space during the
execution time or the run time.
● Efficient utilization of memory
Memory Allocation

● Advantages of Dynamic Partitioning:


● No Internal Fragmentation
● Degree of multiprogramming is
dynamic
● Disadvantages of Dynamic
Partitioning:
● External Fragmentation
● Allocation and deallocation is
complex

You might also like