You are on page 1of 15

Swapping/Segmentation

Swapping
By
Usama-Raheem002
Swapping
• Concept: A process can be swapped temporarily out of memory to a backing store, and then brought
back into memory for continued execution

• Backing Store: fast disk large enough to accommodate copies of all memory images for all users;
must provide direct access to these memory images

• 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

• Note: Major part of swap time is transfer time, total transfer time is directly proportional to the
amount of memory swapped

• Example: Modified versions of swapping are found on many systems (i.e., UNIX, Linux, and Windows)
Diagram of Swapping Process
Swapping

C C C C C
B B B B
A
A A A
D D D
OS OS OS OS OS OS OS

• Memory allocation changes as


• Processes come into memory
• Processes leave memory
• Swapped to disk
• Complete execution
• Gray regions are unused memory
Swapping: leaving room to grow
• Need to allow for programs Stack
to grow Room for
• Allocate more memory for Process B to grow
data
B Data
• Larger stack
• Handled by allocating more Code
space than is necessary at Stack
the start Room for
• Inefficient: wastes memory Process A to grow
that’s not currently in use A
Data
• What if the process requests
too much memory? Code
OS
Allocating memory
• Search through region list to find a large enough space
• Suppose there are several choices: which one to use?
• First fit: the first suitable hole on the list
• Next fit: the first suitable after the previously allocated hole
• Best fit: the smallest hole that is larger than the desired region (wastes least space?)
• Worst fit: the largest available hole (leaves largest fragment)
• Option: maintain separate queues for different-size holes

Allocate 20 blocks first fit Allocate 13 blocks best fit


Allocate 12 blocks next fit Allocate 15 blocks worst fit
1 5 18
- 6 5 - 19 14 - 52 25 - 102 30 - 135 16

- 202 10 - 302 20 - 350 30 - 411 19 - 510 3


15
Freeing memory
• Allocation structures must be updated when memory is freed
• Easy with bitmaps: just set the appropriate bits in the bitmap
• Linked lists: modify adjacent elements as needed
• Merge adjacent free regions into a single region
• May involve merging two regions with the just-freed area

A X B A B

A X A

X B B

X
Limitations of swapping
• Problems with swapping
• Process must fit into physical memory (impossible to run larger
processes)
• Memory becomes fragmented
• External fragmentation: lots of small free areas
• Compaction needed to reassemble larger free areas
• Processes are either in memory or on disk: half and half doesn’t do
any good
Swapping/Segmentation
Segmentation
By
Usama-Raheem002
Segmentation
• Memory-management scheme that supports user view of memory
• A program is a collection of segments. A segment is a logical unit such as:
main program,
procedure,
function,
method,
object,
local variables, global variables,
common block,
stack,
symbol table, arrays
User’s View of a Logical View of
Program Segmentation

1 4

3
2
4

user space physical memory space


Segmentation
Architecture
• Logical address consists of a two tuple:
• <segment-number, offset>,
• Segment table: maps two-dimensional physical addresses, each table entry
has:
• base – contains the starting physical address where the segments reside
in memory
• limit – specifies the length of the segment
• Segment-table base register (STBR) points to the segment table’s location in
memory
• Segment-table length register (STLR) indicates number of segments used by
a program;
segment number s is legal if s < STLR
Address Translation Architecture
Example of Segmentation Sharing of Segments
Segmentation with Paging – Intel
386
As shown in the following diagram, the Intel 386 uses segmentation with paging for memory
management with a two-level paging scheme– below intel 30386 address translated

You might also like