Week 5 (2013-2014) Operating Systems
Week 5 (2013-2014) Operating Systems
Lecture 5
Lecture 5 contents
Memory Management 1. Contiguous Memory Allocation
1. 2. 3. 4. 1. Fixed Partitioning Variable Partitioning Compaction Relocation Paging
1. 2. Page table implementation Page sharing
2.
Segmentation
2
3. Comparison
Memory management
Contiguous memory allocation: Each process is allocated a single contiguous area in memory.
Non-Contiguous memory allocation : Portions of a process address space are distributed among different memory areas
Operating Systems
Contiguous Allocation
1 Fixed Partitioning
memory
OS n KB 3n KB small Medium
6n KB
Large
In this method, memory is divided into partitions whose sizes are fixed. OS is placed into the lowest bytes of memory. Relocation of processes is not needed
1 Fixed Partitioning
memory
OS n KB 3n KB small Medium
Processes are classified on entry to the system according to the memory they require. We need one Process Queue (PQ) for each class of process.
6n KB
Large
large area Q
1 Fixed Partitioning
memory OS n KB 3n KB small Medium
6n KB
Large
large area Q
If a process is selected to allocate memory, then it goes into memory and competes for the processor. The number of fixed partition gives the degree of multiprogramming. Since each queue has its own memory region, there is no competition between queues for the memory.
7
1 Fixed Partitioning
memory OS n KB 3n KB small Medium
6n KB
Large large area Q
The main problem with the fixed partitioning method is how to determine the number of partitions, and how to determine their sizes.
OS 2K 6K P1 P2
P3
P4 P5
12K
empty
empty
This is a version of fixed partitioning that uses RRS with some time quantum. When time quantum for a process expires, it is swapped out of memory to disk and the next process in the corresponding process queue is swapped into the memory.
P3
6K
P2
P4 P5
12K
empty
empty
10
P3 P1
P2
6K
P4 P5
12K
empty
empty
11
P1
6K
P2
P4 P5
12K
empty
empty
12
P1
6K
P2
P4 P5
12K
empty
empty
13
P1 P3
P2
6K
P4 P5
12K
empty
empty
14
P3
6K
P2
P4 P5
12K
empty
empty
15
P3
6K
P2
P4 P5
12K
empty
empty
16
Fragmentation
Fragmentation leads to poor memory utilization
memory OS 2K 6K P1 (2K) Empty (6K) If a partition is being used by a process requiring some memory smaller than the partition size, then it is called an internal fragmentation.
17
If a whole partition is currently not being used, then it is called an external fragmentation.
12K
P2 (9K) empty
Empty (3K)
2. Variable Partitioning
With fixed partitions we have to deal with the problem of determining the number and sizes of partitions to minimize internal and external fragmentation. If we use variable partitioning instead, then partition sizes may vary dynamically. In the variable partitioning method, we keep a table (linked list) indicating used/free areas in memory.
18
2. Variable Partitioning
Initially, the whole memory is free and it is considered as one large block. When a new process arrives, the OS searches for a block of free memory large enough for that process. We keep the rest available (free) for the future processes. If a block becomes free, then the OS tries to merge it with its neighbors if they are also free.
19
2. Variable Partitioning
There are three algorithms for searching the list of free blocks for a specific amount of memory. First Fit Best Fit Worst Fit
20
first fit
First Fit : Allocate the first free block that is large enough for the new process. This is a fast algorithm.
21
first fit
OS P1 12 KB <FREE> 10 KB
P2 20 KB <FREE> 16 KB
P3 6 KB <FREE> 4 KB
22
first fit
OS P1 12 KB <FREE> 10 KB
P4 of 3KB arrives
P2 20 KB <FREE> 16 KB
P3 6 KB <FREE> 4 KB
23
first fit
OS
P1 12 KB P4 3 KB <FREE> 7 KB P2 20 KB <FREE> 16 KB
P3 6 KB <FREE> 4 KB
24
first fit
OS P1 12 KB
P5 of 15KB arrives
P4 3 KB <FREE> 7 KB P2 20 KB <FREE> 16 KB
P3 6 KB <FREE> 4 KB
25
first fit
OS
26
Best fit
Best Fit : Allocate the smallest block among those that are large enough for the new process. In this method, the OS has to search the entire list, or it can keep it sorted and stop when it hits an entry which has a size larger than the size of new process. This algorithm produces the smallest left over block. However, it requires more time for searching all the list or sorting it If sorting is used, merging the area released when a process terminates to neighboring free blocks, becomes complicated.
27
Best fit
OS P1 12 KB <FREE> 10 KB
P2 20 KB <FREE> 16 KB
P3 6 KB <FREE> 4 KB
28
Best fit
OS P1 12 KB
P4 of 3KB arrives
<FREE> 10 KB
P2 20 KB <FREE> 16 KB
P3 6 KB <FREE> 4 KB
29
Best fit
OS
P1 12 KB <FREE> 10 KB
P2 20 KB <FREE> 16 KB
P3 6 KB P4 3 KB <FREE> 1 KB
30
Best fit
OS P1 12 KB
P5 of 15KB arrives
<FREE> 10 KB
P2 20 KB <FREE> 16 KB
P3 6 KB P4 3 KB <FREE> 1 KB
31
Best fit
OS
P1 12 KB <FREE> 10 KB
P2 20 KB P5 15 KB <FREE> 1 KB P3 6 KB P4 3 KB <FREE> 1 KB
32
Worst fit
Worst Fit : Allocate the largest block among those that are large enough for the new process. Again a search of the entire list or sorting it is needed. This algorithm produces the largest over block.
33
Worst fit
OS
P1 12 KB <FREE> 10 KB
P2 20 KB <FREE> 16 KB
P3 6 KB <FREE> 4 KB
34
Worst fit
OS P1 12 KB
P4 of 3KB arrives
<FREE> 10 KB
P2 20 KB <FREE> 16 KB
P3 6 KB <FREE> 4 KB
35
Worst fit
OS P1 12 KB P4 of 3KB Loaded here by WORST FIT <FREE> 10 KB
P2 20 KB P4 3 KB <FREE> 13 KB P3 6 KB <FREE> 4 KB
36
Worst fit
OS
P1 12 KB <FREE> 10 KB
P2 20 KB P4 3 KB <FREE> 13 KB P3 6 KB <FREE> 4 KB
37
Worst fit
OS
P1 12 KB <FREE> 10 KB
P2 20 KB P4 3 KB <FREE> 13 KB P3 6 KB <FREE> 4 KB
Compactio n is needed !!
38
3. Compaction
Compaction is a method to overcome the external fragmentation problem. All free blocks are brought together as one large block of free space. Compaction requires dynamic relocation. Certainly, compaction has a cost and selection of an optimal compaction strategy is difficult. One method for compaction is swapping out those processes that are to be moved within the memory, and swapping them into different memory locations
39
3. Compaction
OS
P1 12 KB <FREE> 10 KB
P2 20 KB P4 3 KB <FREE> 13 KB P3 6 KB <FREE> 4 KB
40
3. Compaction
OS P1 12 KB
P2 20 KB P4 3 KB
Swap out P2
P3 6 KB
41
Compaction
OS P1 12 KB P2 20 KB
Swap in P2
Secondary storage
P4 3 KB
P3 6 KB
42
Compaction
OS P1 12 KB P2 20 KB
P3 6 KB
43
compaction
OS P1 12 KB P2 20 KB P4 3 KB
Secondary storage
44
compaction
OS P1 12 KB P2 20 KB P4 3 KB Secondary storage Swap out P3 P3 6 KB
45
compaction
OS P1 12 KB P2 20 KB P4 3 KB P3 6 KB
Swap in P3
Secondary storage
46
compaction
OS P1 12 KB
P2 20 KB P4 3 KB P3 6 KB <FREE> 27 KB
47
3. Compaction
OS P1 12 KB P2 20 KB P4 3 KB P3 6 KB P5 12 KB <FREE> 12 KB
P5 of 15KB is loaded
48
4. Relocation
sensitive
4. Relocation
50
In contiguous memory allocation each process is allocated a single contiguous area in memory
Compaction requires a relocation register Lack of this register is also a problem for swapping
51
Operating Systems
Contiguous Allocation
Memory types
Physical memory : It referred to as the physical storage or the real storage. RAM is the amount of real storage, and is the total amount of memory installed on a computer. Logical memory : Is the address space, assigned to a logical partition, that the operating system perceives as its main memory. Virtual memory involves the separation of logical memory as perceived by the users and the physical memory. The separation allows an extremely large virtual memory for programmers. The programmer no longer need to worry about the amount of physical memory available.
53
1. Paging
Paging permits a program to allocate noncontiguous blocks of memory. The OS divide each process into pages which are blocks of small and fixed size. Then, it divides the physical memory into frames which are blocks of size equal to page size. Each page is matched to a corresponding frame in a page table saved by the OS in the registry for faster access or in memory for large page tables or both. Each page and each frame is subdivided into an equal set of words
Logical memory P0 P1 P2 P3 page 0 1 2 3
PAGE TABLE
frame 4 3 1 5 Attributes
Physical memory f0 P2 f1 f2 P1 P0 P3 f3 f4 f5
54
1. Paging
Logical memory
Word 0 Word 1 Word 7 Word 8 Page 0 (P0)
Physical memory
Word 0 Word 1 Frame 3 (f3) Word 7
PAGE TABE
Page Frame
Word 9
Word 15 Word 16 Word 17 Word 23
Page 1
(P1)
0
1 2
3
6 4
Word 16
Word 17 Word 23 Frame 4 (f4)
Page 2 (P2)
55
1. Paging
The OS uses a page table to map program pages to memory frames. Page size (S) is defined by the hardware. Logical Physica
With this arrangement, the words in the program have an address called as logical address. Every logical address is formed of <p,d> pair
addres s p d
l addres s f d
frame
page
attr
f
p f
physical memory
logical memory
For example : If the program is 7 statement (words), the page is 4 words only. The program will be placed in two pages (p0 and p1). Then the page p of the six word is (6 div 4 = 1 p1) and 56 the displacement d of the fifth word is (6 mode 4 = 2 2 words from the start).
Every access to memory should go through the page table. Therefore, it must be implemented in an efficient way.
Using fast dedicated registers Keep the page table in main memory Use content-addressable associative registers
57
Keep page table in fast dedicated registers. Only the OS is able to modify these registers. However, if the page table is large, this method becomes very expensive since requires too many registers.
58
d
physical address YES
P<PTLR
NO ERROR
access PT in register s
rat
access memor y
mat
In this second method, the OS keeps a page table in the memory, instead of registers. For every logical memory reference, two memory accesses are required:
1. 2.
To access the page table in the memory, in order to find the corresponding frame number. To access the memory word in that frame
60
PTBR: Page Table Base Register PTLR: Page Table Length Register
YES
P<PTLR
NO ERROR
access memor y
mat
This is a mixing of first two methods. Associative registers are small, high speed registers built in a special way so that they permit an associative search over their contents. That is, all registers may be searched in one machine cycle simultaneously. However, associative registers are quite expensive. So, a small number of them should be used. In the overall, the method is not-expensive and not-slow.
62
No (MISS) P<PTL R
No ERROR Yes
search PT in AR
rat
physical address
d access memory
mat
h: hit ratio
Assume we have a paging system which uses associative registers. These associative registers have an access time of 30 ns, and the memory access time is 470 ns. The system has a hit ratio of 90 %. rat=30 ns mat=470ns h=0.9
64
Now, if the page number is found in one of the associative registers, then the effective access time: ematHIT = 30 + 470 = 500 ns. Because one access to associative registers and one access to the main memory is sufficient.
65
On the other hand, if the page number is not found in associative registers, then the effective access time: ematMISS = 30 + (470+470) = 970 ns. Since one access to associative registers and two accesses to the main memory are required.
66
Then, the emat is calculated as follows: emat= h *ematHIT + (1-h) * ematMISS = 0.9 * 500 + 0.1 * 970 = 450 + 97 = 547 ns
67
Sharing Pages
Sharing pages is possible in a paging system, and is an important advantage of paging. It is possible to share system procedures or programs, user procedures or programs, and possibly data area. Sharing pages is especially advantageous in time-sharing systems.
68
Sharing Pages
A reentrant program (non-self-modifying code = read only) never changes during execution. So, more than one process can execute the same code at the same time. Each process will have its own data storage and its own copy of registers to hold the data for its own execution of the shared program.
69
Sharing Pages
Example 3.4 Consider a system having page size=30 MB. There are 3 users executing an editor program which is 90 MB (3 pages) in size, with a 30 MB (1 page) data space. To support these 3 users, the OS must allocate 3 * (90+30) = 360 MB space. However, if the editor program is reentrant (non-self-modifying code = read only), then it can be shared among the users, and only one copy of the editor program is sufficient. Therefore, only 90 + 30 * 3 = 180 MB of memory space is enough for this case
70
P0 P1 P2 P3
User-1 e1 e2 e3 data1
Frame# 8 4 5 7
P0 P1 P2 P3
User-2 e1 e2 e3 data2
Frame# 8 4 5 12
P0 P1 P2 P3
User-3 e1 e2 e3 data3
Frame# 8 4 5 10
Physical Memory OS OS OS e2 e3
data1 e1
71
P0 P1 P2 P3
User-1 e1 e2 e3 data1
Frame# 8 4 5 7
P0 P1 P2 P3
User-2 e1 e2 e3 data2
Frame# 8 4 5 12
P0 P1 P2 P3
User-3 e1 e2 e3 data3
Frame# 8 4 5 10
Physical Memory OS OS OS e2 e3
data1 e1
data 2
72
P0 P1 P2 P3
User-1 e1 e2 e3 data1
Frame# 8 4 5 7
P0 P1 P2 P3
User-2 e1 e2 e3 data2
Frame# 8 4 5 12
P0 P1 P2 P3
User-3 e1 e2 e3 data3
Frame# 8 4 5 10
Physical Memory OS OS OS e2 e3
data1 e1 data3
data 2
73
P0 P1 P2 P3
User-1 e1 e2 e3 data1
PT-1 Page# 0 1 2 3
Frame# 8 4 5 7
User-2 PT-2 User 2 terminates: P0 e1 Page# Data2 page is removed P1memory, e2 but editr 0 from P2 1 pagese3 remain.. P3 data2 2 3 User-3 e1 e2 e3 data3 PT-3 Page# 0 1 2
Frame# 8 4 5 12
P0 P1 P2 P3
Frame# 8 4 5 10
Physical Memory OS OS OS e2 e3
data1 e1 data3
74
P0 P1 P2 User 1 P3
Frame# 8 4 5 7
Frame# 8 4 5 12
P0 P1 P2 P3
User-3 e1 e2 e3 data3
Frame# 8 4 5 10
Physical Memory OS OS OS e2 e3
e1
data3
75
P0 P1 P2 P3
User-1 e1 e2 e3 data1
Frame# 8 4 5 7
P0 P1 P2 P3
User-2 e1 e2 e3 data2
When User 3 terminates: PT-3 User-3 Data-3 P0 e1 and also editor Page# segments are P1 e2 0 removed from 1 P2 e3 memory. P3 data3 2
3
Frame# 8 4 5 12
Frame# 8 4 5 10
Physical Memory OS OS OS
76
2. Segmentation
In segmentation, programs are divided into variable size segments, instead of fixed size pages. This is similar to variable partitioning, but programs are divided to small parts. Every logical address is formed of a segment name and an offset within that segment. In practice, segments are numbered. Programs are segmented automatically by the compiler or assembler.
77
OS
2. Segmentation
main Func 1
For example, a C compiler may create segments for: the code of each function the local variables for each function the global variables. main
Func 2
Data 1
Data 3
Data 2
Data 3
Logical memory
Physical memory
2. Segmentation
For logical to physical address mapping, a segment table is used. When a logical address <s, d> is generated by the processor:
1.
2.
3.
Base and limit values corresponding to segment s are determined using the segment table The OS checks whether d is in the limit. (0 d < limit) If so, then the physical address is calculated as (base + d), and the memory is accessed.
79
2. Segmentation
Logical address s d No ERROR 0d <limit
base
OS
Segment Table
d
segment s
Yes
seg. #
limit
base
attr
2. Segmentation
Example 3.5 Generate the memory map according to the given segment table. Assume the generated logical address is <1,1123>; find the corresponding physical address.
Segment 0 1 2 3
OS
2. Segmentation
1000
Segment 0
Limit 1500
Base 1000
s0
1
2 3
200
700 2000
5500
6000 3500
s3
2000
5500
s1
s2
Physical memory
82
OS
2. Segmentation
1000
Segment 0
Limit 1500
Base 1000
s0
1
2 3
200
700 2000
5500
6000 3500
s3
2000
5500
Logical address: <3,1123> s=3, d=1123 Check if d<limit? 1123<2000, OK Physical address= base+d=3500+1123=4623
s1 s2
200
700
Physical
83
OS
2. Segmentation
1000
Segment
Limit
Base
s0
Base= 3500
0
1 2 3
1500
200 700 2000
1000
5500 6000 3500
s3
2000 5500
Logical address: <3,1123> s=3, d=1123 Check if d<limit? 1123<2000, OK Physical address= base+d=3500+1123=4623
s1
s2
Physical memory
84
OS
2. Segmentation
1000
Segment 0
Limit 1500
Base 1000
Base= 3500
s0
1
2 3
200
700 2000
5500
6000 3500
4623 d= 1123
s3
2000 5500
Logical address: <3,1123> s=3, d=1123 Check if d<limit? 1123<2000, OK Physical address= base+d=3500+1123=4623
s1
s2
Physical memory
85
2. Segmentation
Segment tables may be implemented in the main memory or in associative registers, in the same way it is done for page tables. Also sharing of segments is applicable as in paging. Shared segments should be read only and should be assigned the same segment number.
86
OS
Sharing Segments
user1 s0 s1
Editr
Data-1
1000
ST1 seg 0 1
Editr
Data-1
2000 5500
Physical memory
87
OS
Sharing Segments
user1
s0 s1
Editr ST1 seg 0 1 lim 1500 2000 base 1000 3500 Data-1 lim 1500 200 base 1000 5500 Data-2
200 5700 2000 5500 1000
Editr
Data-1
user2 s0 s1
Editr Data-2
ST2 seg 0 1
Physical memory
88
OS
Sharing Segments
Editr Data-1 ST1 seg 0 1 lim 1500 2000 base 1000 3500 Editr
1500 2500 3500 1000
Editr Data-2
ST2 seg 0 1
Data-1
2000 5500
Data-2 ST3 seg Editr 0 1 lim 1500 700 base 1000 6000 Data-3
Data-3
Physical memory
89
OS
user1 s0 s1
Sharing Segments
Editr Data-1 ST1 seg 0 1 lim 1500 2000 base 1000 3500 Editr
1500 2500 3500 1000
User 2 terminates: Data-2 removed from memory, but editr remains.. user3 s0 s1
Editr
Data-1
2000 5500
ST3 seg 0
6000
lim 1500
base 100
Dat-3
700 6700
Data-3
700
6000
Physical memory
90
OS
Sharing Segments
User 1 terminates: Data-1segment is removed from memory.
1000
Editr
1500 2500
user3 s0 s1
Editr ST3 seg 0 1 Data-3 lim 1500 700 base 100 6000
700
6000 6700
Data-3
Physical memory
91
OS
Sharing Segments
When User 3 terminates: Data-1 Data-3and segment also editor and also segments editor segment are are removed removed from from memory.
Physical memory
92
Paging vs segmentation
Paging: separates physical organization from logical address space Uses fixed-size units called pages, which are stored in page frames. Requires page table to keep track of (possibly many) pages. Segmentation: preserves users structural view of logical address space Uses variable-size units called segments.
93
94
Operating Systems
Lecture 5