Академический Документы
Профессиональный Документы
Культура Документы
memory management
Physical memory :
the main memory RAM of the machine Physical address address seen by this memory
Separate these concepts because, the programs, in general are loaded in different memory positions
So physical address address logical
Base Register (b): the lowest memory address used by a process Length (l): the length of the physical allocated space. Could be written in this form:
1) if a<0 then memory violation 2) else if a > l then memory violation 3) else f(a) = a = b+a 4) a is a valid address
F:S a
R b+a
Programs swapping
A program, or a part of a program, could be temporarly unloaded from memory in order to let other programs to execute
It will be put in secondary memory, normally disk
Contiguous Allocation
Main memory usually divided into two partitions:
Resident operating system, usually held in low memory User processes then held in high memory
Single-partition allocation
Relocation-register scheme used to protect user processes from each other, and from changing operating-system code and data Relocation register contains value of smallest physical address; limit register contains range of logical addresses each logical address must be less than the limit register
Contiguous Allocation
OS prog. 1 prog. 2
available
prog. 3 We have here 4 partitions for the programs each one is loaded in a single zone in the memory
11
12
Contiguous Allocation
Multiple-partition allocation
Hole block of available memory; holes of various size are
scattered throughout memory When a process arrives, it is allocated memory from a hole large enough to accommodate it Operating system maintains information about: a) allocated partitions b) free partitions (holes)
OS process 5 OS process 5 OS process 5 process 9 process 8 process 2 process 2 process 2 OS process 5 process 9 process 10 process 2
13
14
Fixed Partitions
Main Memory divided into a certain number of partitions
Partitions are either of the same size or not. Any process could be loaded in a partition having enough size to hold the process
15
17
Fixed partitions
Simple, but... Inefficiency of the memory use : every program, as small as it is, should occupy one entire partition. So theres internal fragmentation.
the unequal sized partitions decrease these problems but they remain...
18
Dynamic partitions
Partitions are variable in number and size. Each process allocates exactly the required memory space Probably unused holes will be formed in the memory: Its the external fragmentation
19
(d) there is a hole of 64K after loading 3 processes: No more free space for other processes. If P2 is blocked (ex. Waiting for an event), it could be unloaded and we could load P4=128K.
20
(e-f) P2 is suspended, P4 is loaded. A hole of 224-128=96K is created (external fragmentation) (g-h) P1 terminates, P2 is reloaded: another hole of 320-224=96K... We have 3 small holes, probably useless. COMPACTION to create one hole of 256K
21
Compression (compaction)
A solution for the external fragmentation. the programs are moved in order to reduce into one big hole all the small available holes. Is made when a program asking to execute doesnt find a free hole fitting it, but its size is less than the existing external fragmentation.
Disadvantages:
transfer time of the programs Need to recover all the links between the programs addresses
22
Allocation Algorithms
How to satisfy a request of size n from a list of free holes Which place to allocate in order to reduce the need of compaction
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. Produces the smallest leftover hole. Worst-fit: Allocate the largest hole; must also search entire list. Produces the largest leftover hole. Next-fit: choose the first hole after the last allocated place First-fit and best-fit better than worst-fit in terms of speed and storage use
23
24
A
Main Progr.
B
data
C data
D Subprogr.
LOAD(B,50)
4 segments: A, B, C, D
25
2 3
1 2
user space
Physical memory
Given that the segments are smaller than entire programs, this technique implies less fragmentation (external in this case)
26
0 3
Current segment
1 2
Details
The logical address consists of a pair: <seg nb, offset>
Where the offset is the address in the segment
In the PCB (Process Control Block) of the process, there will be a pointer to the address in memory of the segments table There will be also the number of segments in the process At the context exchange time, these info will be loaded in the appropriate registries of the CPU.
28
29
Final address
30
31
limit
base
Another solution is to try to simplify the mechanism by using memory allocation units of equal size. Paging
33
The pb with the segmentation is that the memory allocation unit (the segment) is of variable length. The paging uses fixed memory allocation units, the problem is then resolved.
34
Simple Paging
the memory is partitioned into same sized small parts: the physical pages or frames each process is also partitioned into small parts of same size called pages (logical) The logical pages of a process could be assigned to available frames anywhere in memory. Consequences:
A process could be distributed in the physical memory. the external fragmentation is eliminated
35
36
the external fragmentation is limited to the case where the proc. pages number is greater than the available frames nb. Only the last page of a prog Could have internal fragmentation (average: 1/2 frame per proc)
37
Page Table
the entries in the pages table are also called pages descriptors
38
Page table
Adresses translation
The logical address is easely translated into physical address
The page sizes are powers of 2 the pages begin always at addresses that are powers of 2 Having as much zeros to the right as the offset length So these 0s are sont replaced by the offset
Ex: if 16 bits are used for the addresses and the page size= 1K: if we need 10 bits for the offset, remains then 6 bits for the page number The logical address (n,m) is translated into the physical address (k,m) using n as index in the page table and remplacing it by the address found: k
m doesnt change
40
Mecanisme: material
41
42
11010000+1010
=
1101 1010
43
the segmentation requires a more complex hardware for the addresses translation (addition instead of concatenation) the segmentation suffers of external fragmentation (dynamic partitions) the Paging produces internal fragmentation , but not too much (1/2 frame per program) fortunately, the segmentation and the Paging could be combined
44
45
Addressing
s p d d'
46
Page-table base register (PTBR) points to the page table. Page-table length register (PTLR) indicates the size of page table.
One for page table, one for data/instruction Two-memory access problem solved by use of special fastlookup hardware cache (i.e. cache page table in registers)
This procedure may be handled by the MMU, but today it is often handled by software; i.e. the operating system.
48
49
If A is in associative register, get frame # Otherwise, need to go to page table for frame#
If a TLB hit takes 1 clock cycle, a miss takes 30 clock cycles, and the miss rate is 1%, the effective memory cycle rate for page mapping
1 X 0.99 + (1+30) X 0.01=1.30 1.30 clock cycles per memory access
51
Associative lookup time = time unit Assume Memory cycle time = 1 microsecond Hit ratio = Effective access time (EAT) EAT = (1+ ) + (2+ ) (1-) EAT = (2+ - ) s
52
Exercise 2-1
(Contiguous Allocation, dynamic partitions)
Consider the following sequence of allocation (+) and liberation (-) requests in memory space of 1000 blocs, using the contiguous allocation with dynamic partitions: +300, +200, +260, -200, +100, -300, +250, +400, -260, +150, +120, -100, -120, +200, -150, -250, +100, -400, +600, -100, -200, -600 Indicate how, starting from a free memory, the OS realizes the allocation using Best Fit, First Fit strategies.
53
Exercise 2-2
Segmentation
We consider the following segment table : Segment Base Length
0
1
540
1234
234
128
2
3 4
54
2048 976
328
1024 200
Calculate the real addresses corresponding to the following virtual addresses : (0, 128), (1, 99), (1, 100), (2, 465), (3, 888), (4, 100), (4, 344)
54
Exercise 2-3
Paging
In a paged system, the pages are 256 words each and the memory contains 4 frames. Consider the following page table
0 1 2 3 4 5 6 7
3 0 i i 2 i i 1
55
So the sum of logical memory of running processes may exceed the available physical memory
the base concept of the virtual memory
An image of all the addressing space of the process is kept in secondary memory (normal. disk) where the missing pages could be taken when needed
Swapping mechanism
56
57
Page address
Valid Bit
58
59
Many pages or segments that are rarely used will not be loaded Its now possible to execute a set of processes even if their total size exceeds the memory size
Its possible to use for logical address more bits than for the addresses in the main memory
60
so the limits of the virtual memory is defined by the number of bits reserved for the address
the logical memory is called virtual memory
Its maintained in secondary memory the parts are loaded in main memory only on demand.
61
Process execution
the OS loads in memory only few parts of the program (including the starting point) each entry of the page table (or segments) has a valid bit that indicates whether this page or segment is in memory or not The resident set is the portion of process loaded in the memory An interruption is generated when a logical address refers to a part that is not in the resident set page fault
62
virtual memory
63
When the RAM is full but we need a page not in RAM => page replacement
64
Pages Replacement
1. Find the location of the desired page on disk 2. Find a free frame: - If there is a free frame, use it - If there is no free frame, use a page replacement algorithm to select a victim frame 3. Read the desired page into the (newly) free frame. 4. Update the page and frame tables. 5. Restart the process
65
66
How do you know this? (impossible to know the future) Used for measuring how well your algorithm performs
67
68
69
Counter implementation
Every page entry has a counter; every time page is referenced through this entry, copy the clock into the counter When a page needs to be changed, look at the counters to determine which are to change
70
Comparaison OPT-LRU
example: a process of 5 pages if theres only 3 available physical pages. In this example, OPT has 3+3 faults, while LRU has 3+4.
71
But: a page that is frequently used is often the oldest, it will be replaced replaced by FIFO!
3
4 frames 1 2 3
2
5 1 2
4
4 5 10 page faults
3
4
Contrarly to FIFO, LRU knows that the pages 2 and 5 are the most frequently used In this case, the performance of FIFO is less than LRU: LRU = 3+4, FIFO = 3+6
74
FIFO Implementation
Easy to implement using a queue of frames of the memory that should be updated for each page fault
75
the frames that has just been used (bit=1) are not replaced (second chance)
the frames make conceptually a circular buffer When a page is loaded in a frame, a pointeur points to the next frame of the buffer
for each frame of the buffer, a bit used is set to 1 (by the hardware) when:
A page is newly loaded in the frame Its page (of this frame) is used
The next frame of the buffer to be replaced will be the first one having the bit used = 0.
During this search, every used bit = 1 encountered is set to 0
76
the page 727 is loaded in the frame 4. the next victim is 5, then 8.
77
* indicates that the used bit is 1 The clock protects from the replacement of the frequently used pages by setting the used bit to 1 at each reference LRU = 3+4, FIFO = 3+6, Clock= 3+5
78
All the bits were 1. while searching all were changed to 0 => the 1st page has been used
79
80
5.25
3.5
81
Disk Structure
Hard disk drives are organized as a concentric stack of disks or platters Each platter has 2 surfaces
82
83
84
Double-sided Disk
Cyl = 79
Cyl = 0
Sides or Heads 1 0
Spindle Motor
Comprise Cylinder 0
Disk Drive
85
Cylinders
C Y L I N D E R
Sector
Track
86
Unix Examples
In the Unix system, the dynamic creation is simple
Creating a process that is an exact copy of the one asking for the creation. No parameters are necessary
id = fork();
the only distinction between the creator process (father) and the new process (child) is in the returned value.
Id == -1 : creation failed Id == 0 : the child process Id != 0 : the identity of the child process
After the fork() function call, the two processes execute the remaining of the same program
87
Unix Examples
If the father process terminates before its child , the child is adopted by the root process
The father process can wit the termination of one of its children using the C language function:
id_child = wait(&status);
id_child : identifier of the terminated child the function wait puts in status the way the process has terminated If theres active children, the father process waits and if not, the function returns -1
88
89
End
90