info324 memory
TRANSCRIPT
-
7/29/2019 Info324 Memory
1/84
Info 324Operating Systems II
1
Dr. Samar TAWBI
-
7/29/2019 Info324 Memory
2/84
memory management
2
-
7/29/2019 Info324 Memory
3/84
Memory management: objectives
Optimizing the use of the main memory = RAM
the biggest number of active processes in ordertooptimize the function of the multiprogrammingsyste.
Keep the system the most possible, especially the CPU.
Dynamic allocation when needed.
3
-
7/29/2019 Info324 Memory
4/84
Memory/addresses physical and logical
Logical memory : the adressing space of a programLogical address generated by the CPU; also referred to as
virtual address
Physical memory :the main memory RAM of the machine
Physical address address seen by this memory
Separate these concepts because, the programs, ingeneral are loaded in different memory positions
So physical address address logical
4
-
7/29/2019 Info324 Memory
5/84
Translationlogical address physical address
In the early systems, a program was always loaded in the samememory zone.
the multiprogramming and dynamic allocation leaded to the need ofprogram loading in different positions.
Now, this is done by the MMU meanwhile the prog. is running.
MMU = Hardware device that maps virtual to physical address
In MMU scheme, the value in the relocation register is added to every
address generated by a user process at the time it is sent to memory
The user program deals with logicaladdresses; it never sees the realphysical addresses
5
-
7/29/2019 Info324 Memory
6/84
Translationlogical address physical address.
A translation mecanism is necessaryTransform the symbolic addresses into real addresses
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 l then memory violation3) else f(a) = a = b+a
4) a is a valid address
6
F : S Ra b+a
-
7/29/2019 Info324 Memory
7/84
translationlogical address physical address
7
Base register
Base Register =the lowest memory address used
-
7/29/2019 Info324 Memory
8/84
Programs swapping
A program, or a part of a program, could betemporarly unloaded from memory in order to letother programs to execute
It will be put in secondary memory, normally disk
8
-
7/29/2019 Info324 Memory
9/84
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 processesfrom each other, and from changing operating-system codeand data
Relocation register contains value of smallest physicaladdress; limit register contains range of logical addresseseach logical address must be less than the limit register
-
7/29/2019 Info324 Memory
10/84
Contiguous Allocation
10
OS
prog. 1
prog. 2
prog. 3
available
We have here 4 partitions for the programs each one is
loaded in a single zone in the memory
-
7/29/2019 Info324 Memory
11/84
Relocation and Limit Registers
11
-
7/29/2019 Info324 Memory
12/84
Contiguous Allocation
12
Multiple-partition allocation
Hole block of available memory; holes of various size arescattered throughout memory
When a process arrives, it is allocated memory from a holelarge enough to accommodate it
Operating system maintains information about:a) allocated partitions b) free partitions (holes)
OS
process 5
process 8
process 2
OS
process 5
process 2
OS
process 5
process 2
OS
process 5
process 9
process 2
process 9
process 10
-
7/29/2019 Info324 Memory
13/84
Fragmentation: non used memory
External Fragmentation
total memory spaceexists 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 apartition, but not being used
No solution for internal fragmentation.
13
-
7/29/2019 Info324 Memory
14/8414
Fixed Partitions
Main Memory dividedinto a certain numberofpartitions
Partitions are either ofthe same size or not.
Any process could be
loaded in a partitionhaving enough size tohold the process
-
7/29/2019 Info324 Memory
15/8415
Loading Algorithm for fixed partitions
Partitions of unequal size:using many queues
assign each process to thesmallest partition that it couldfit in.
1 queue for each partitionsize.
Try to minimize the internalfragmentation
Problem: certain queues willbe empty if theres no processof their size(external frag.)
-
7/29/2019 Info324 Memory
16/8416
Loading Algorithm for fixed partitions
Partitions of unequalsize: using one queue
We choose the smallestfree partition able to
contain the next process
the multiprogramminglevel increases in spite ofthe internal fragmentation
-
7/29/2019 Info324 Memory
17/84
17
Fixed partitions
Simple, but...
Inefficiency of the memory use : every program, assmall as it is, should occupy one entire partition. So
theres internal fragmentation.
the unequal sized partitions decrease these problmsbut they remain...
-
7/29/2019 Info324 Memory
18/84
18
Dynamic partitions
Partitions are variable in number and size.
Each process allocates exactly the required memoryspace
Probably unused holes will be formed in the memory:Its the external fragmentation
-
7/29/2019 Info324 Memory
19/84
19
dynamic partitions: example
(d) there is a hole of64Kafter loading 3 processes: No more free space forother processes.
If P2 is blocked (ex. Waiting for an event), it could be unloaded and wecould load P4=128K.
-
7/29/2019 Info324 Memory
20/84
20
dynamic partitions: example
(e-f) P2 is suspended, P4 is loaded. A hole of 224-128=96K is created (externalfragmentation)(g-h) P1 terminates, P2 is reloaded: another hole of 320-224=96K...We have 3 small holes, probably useless.
96+96+64=256K external fragmentationCOMPACTION to create one hole of 256K
-
7/29/2019 Info324 Memory
21/84
Compression (compaction)
A solution for the external fragmentation.
the programs are moved in order to reduce into one bighole all the small available holes.
Is made when a program asking to execute doesnt find afree hole fitting it, but its size is less than the existingexternal fragmentation.
Disadvantages:
transfer time of the programs
Need to recover all the links between the programs addresses
21
-
7/29/2019 Info324 Memory
22/84
22
Allocation Algorithms
First-fit: Allocate the firstholethat is big enoughBest-fit: Allocate the smallesthole that is big enough; must
search entire list, unless ordered bysize. Produces the smallestleftover hole.Worst-fit: Allocate the largesthole; must also search entire list.
Produces the largest leftover hole.Next-fit: choose the first holeafter the last allocated place
First-fit and best-fit better thanworst-fit in terms of speed andstorage use
How to satisfy a request of size nfrom a list of free holes
Which place to allocate in order to reduce the need of compaction
-
7/29/2019 Info324 Memory
23/84
Non contiguous allocation
Used in order to reduce the fragmentation.Divide a program into parts and allow separate allocation for each partThe parts are largely smaller than the whole process, so better use ofthe CPU
The small holes could be used more easily.
There are two techniques to do this:Segmentation uses parts of the program having logical values(modules)
Paging uses arbitrary parts of the program (prog. Division into samesized pages)
Combination
23
-
7/29/2019 Info324 Memory
24/84
Segments are logical parts of the program
24
AB
C D
Main
Progr. data
Sub-
progr.
data
JUMP(D, 100)
LOAD(C,250)
LOAD(B,50)
4 segments: A, B, C, D
-
7/29/2019 Info324 Memory
25/84
the segments comme unites alloc memory
25
0
2
1
3
0
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)
-
7/29/2019 Info324 Memory
26/84
26
Mechanism for the segmentation
An array contains the begin address of all segments in a process
each address in a segment is addedto the begin address of the
segment by the MMU
descriptors table of thesegments
0
3
1
2
Physical memory
Adr of 2Adr of 1
Adr of 0
Adr of 3
Current segment
-
7/29/2019 Info324 Memory
27/84
Details
The logical address consists of a pair:
Where the offset is the address in the segment
the segment table contains: segmentsdescriptorsBase address (register)
Segment LengthProtection Info
In the PCB (Process Control Block) of the process, there will be apointer 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 infos will be loaded in theappropriate registries of the CPU.
27
-
7/29/2019 Info324 Memory
28/84
28
addresses translation in segmentation
If d > length: error!
-
7/29/2019 Info324 Memory
29/84
-
7/29/2019 Info324 Memory
30/84
Segments Sharing: seg. 0 is shared
30
ex: Word program uses the editor for different documents
-
7/29/2019 Info324 Memory
31/84
Segmentation and protection
each segment descriptor could contain the protectioninfo:
Segment Length
User privilages on this segment: read, write, execute
If when calculating the address we find that the user doesnt
have the access right interruptionThese info could vary from one user to another, for the samesegment!
31
limit base read, write, execute?
Segments descriptors table
-
7/29/2019 Info324 Memory
32/84
32
Evaluation of the simple segmentation
Advantages: the allocation unit of the memory issmaller than the whole program
a logical entity known by the program
The segments may change location in memory
the protection and the segments sharing are easy(en principle)
disadvantage: the problem of dynamic partitions:
the external fragmentation is not eliminated:
Holes in memory, compaction?
Another solution is to try to simplify the mecanisme by usingmemory allocation units of equal size.
Paging
-
7/29/2019 Info324 Memory
33/84
Segmentation VS. Paging
The pb with the segmentation is that the memoryallocation unit (the segment) is of variable length.
The paging uses fixed memory allocation units, theproblem is then resolved.
33
-
7/29/2019 Info324 Memory
34/84
34
Simple Paging
the memory is partitioned into same sized small parts: thephysical pages or frames
each process is also partitioned into small parts of samesize called pages (logical)
The logical pages of a process could be assigned toavailable frames anywhere in memory.
Consequences:A process could be peut tre eparpille nimporte o dans the memoryphysical.
the external fragmentation is eliminated
-
7/29/2019 Info324 Memory
35/84
35
example of process loading
-
7/29/2019 Info324 Memory
36/84
36
example of process loading
We can now load a prog. D, that
needsqui demande 5 framesEven if theres not 5 freecontiguous frames
the external fragmentation islimited to the case where theproc. pages number is greaterthan the available frames nb
Only the last page of a prog.Could have internal
fragmentation (average: 1/2frame per proc)
-
7/29/2019 Info324 Memory
37/84
Pages Table
37
the entries in the pages table
are also called pages descriptors
l
-
7/29/2019 Info324 Memory
38/84
38
Page table
the OS should maintain a page table for each process
each page descriptor contains the frame number where thecorresponding page is physically located
A page table is indexed by the page number (logical) in order to obtainthe frame number.
A list of the available frames is also maintained (free frame list)
Ad t l ti
-
7/29/2019 Info324 Memory
39/84
39
Adresses translation
The logical address is easely translated into physicaladdress
The page sizes are powers of 2
the pages begin always at addresses that arepowers of 2
Having as much zeros to the right as the offsetlength
So these 0s are sont replaced by the offset
Ex: if 16 bits are used for the addresses and the pagesize= 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 physicaladdress (k,m) using n as index in the page table andremplacing it by the address found: k
m doesnt change
-
7/29/2019 Info324 Memory
40/84
Mecanisme: material
40
Address Translation (logical physical)
-
7/29/2019 Info324 Memory
41/84
41
Address Translation (logical-physical)for paging
-
7/29/2019 Info324 Memory
42/84
42
Adresses translation: segmentation and paging
In the segmentation case, as well as in the pagingcase, we add the offset to the address found of thesegment or the page.
However, in paging, the addition could be done bysimple concatenation:
11010000+1010=
1101 1010
-
7/29/2019 Info324 Memory
43/84
43
simple Segmentation vs simple Paging
The Paging cares only of the loading problem, while
the segmentation aims also the linking problem
the segmentation is visible to the program but the Paging is not
the segment is a logical protection and sharing unit, while the page isnot
So the protection and the sharing are easier in the segmentation
the segmentation requires a more complexe hardware for theaddresses translation (addition instead of concatanation)
the segmentation suffres of external fragmentation (dynamicpartitions)
the Paging produces internal fragmentation , but not too much (1/2frame per program)
fortunately, the segmentation and the Paging could be combined
-
7/29/2019 Info324 Memory
44/84
Paging and segmentation combined
The programs are divided into segments and thesegments are paged
So each segment address is not a memory address , butan address of the page table of the segment
The segments and pages tables could be also paged
44
-
7/29/2019 Info324 Memory
45/84
Addressing
45
segment table base register: a
CPU register
s p d'
d
-
7/29/2019 Info324 Memory
46/84
Exercise 2-1
(Contiguous Allocation, dynamic partitions)
Consider the following sequence of allocation (+) and liberation (-)requests in memory space of 1000 blocs, en utilisant lallocationcontiguous avec des 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 theallocation using Best Fit, First Fit strategies.
46
-
7/29/2019 Info324 Memory
47/84
Exercise 2-2
Segmentation
We consider the following segment table :
Calculate the real addresses corresponding to the following virtualaddresses :
(0, 128), (1, 99), (1, 100), (2, 465), (3, 888), (4, 100), (4, 344)
Segment Base Length
0 540 2341 1234 128
2 54 328
3 2048 1024
4 976 200
47
-
7/29/2019 Info324 Memory
48/84
Exercise 2-3
Paging
In a paged system, the pages are 256 words each and the memorycontains 4 frames. Consider the following page table
Calculate the size of the physical memory
Give the number of bits needed for thePrograms logical addresses
Calculate the real addresses correspondingto the virtual addresses:
(0,240), (2,34), (2,35), (6,42), (7,230)
Find the real address corresponding to thevirtual address 456
0 3
1 0
2 i
3 i
4 2
5 i
6 i
7 1
48
From Paging and segmentation to
-
7/29/2019 Info324 Memory
49/84
49
From Paging and segmentation tovirtual memory
A process is composed ofparts (pages or segments) doesnt have to
occupy a contiguous place in the memoryVirtual memory separation of user logical memory from physical memory.
Only part of the program needs to be in memory for execution.Logical address space can therefore be much larger than physicaladdress space.Allows address spaces to be shared by several processes.
Allows for more efficient process creation.
Virtual memory can be implemented via:Demand pagingDemand segmentation
So the sum of logical memory of running procs may exceed theavailable physical memory
the base concept of the virtual memory
An image of all the adressing space of the process is kept in secondarymemory (normal. disk) where the missing pages could be taken whenneeded
Swapping mecanisme
virtual memory: lt h i th t
-
7/29/2019 Info324 Memory
50/84
50
virtual memory: result a mechanism thatcombines main and secondary memories
New format of the page table
-
7/29/2019 Info324 Memory
51/84
51
New format of the page table(the same idea as the segment table)
Page address Valid Bit
Valid bit1 if page in mem.
0 if in sec. mem.
At the beginning the valid bit = 0 for all pages
-
7/29/2019 Info324 Memory
52/84
Pages in RAM or on disk
52
Page A in RAM and
on disk
Page E only on disk
Ad t f ti l l di
-
7/29/2019 Info324 Memory
53/84
53
Advantages of partial loading
It can enable processes to share memory
Only few parts of each process are loaded
Many pages or segments that are rarely used will not be loaded
Its now possible to execute a set of processes even if their totalsize exceeds the memory size
Its possible to use for logical address more bits than for the
addresses in the main memory
-
7/29/2019 Info324 Memory
54/84
54
virtual memory: could be huge!
Ex: 16 bits are required to address a memory of 64KB
Using pages of 1KB, 10 bits are required for the offset
for the page number of thelogical address we can use anumber of bits exceeding 6 (more than 26 cases in the page
table), because not all pages are loaded in memorysimultanously
so the limits of the virtual memory is defined by the numberof 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.
P ti
-
7/29/2019 Info324 Memory
55/84
55
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 thatindicates 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 apart that is not in the resident set
page fault
-
7/29/2019 Info324 Memory
56/84
Steps in Handling a Page Fault
56
virtual memory
When the RAM is full but we need a
-
7/29/2019 Info324 Memory
57/84
When the RAM is full but we need apage not in RAM => page replacement
57
P R l
-
7/29/2019 Info324 Memory
58/84
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 replacementalgorithm 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
58
h i i
-
7/29/2019 Info324 Memory
59/84
the victim page...
59
Al ith f l t
-
7/29/2019 Info324 Memory
60/84
60
Algorithms for page remplacement
The optimal algorithme (OPT) Replaces page that willnot be used for longest period of time4 frames example:
1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
How do you know this? (impossible to know the future)Used for measuring how well your algorithm performs
1
2
3
4
6 page faults
Optimal Page Replacement
-
7/29/2019 Info324 Memory
61/84
61
Optimal Page Replacement
-
7/29/2019 Info324 Memory
62/84
LRU Page Replacement
62
chronological Order (LRU)
Least Recently Used
Replace the page that was the oldest to be referenced (thepast used to predict the futur)
l d ( ) l h
-
7/29/2019 Info324 Memory
63/84
63
Least Recently Used (LRU) Algorithm
Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
Counter implementation
Every page entry has a counter; every time page isreferenced through this entry, copy the clock into the counter
When a page needs to be changed, look at the counters todetermine which are to change
1
2
3
5
4
4 3
5
C i OPT LRU
-
7/29/2019 Info324 Memory
64/84
Comparaison OPT-LRU
example: a process of 5 pages if theres only 3 availablephysical pages.
In this example, OPT has 3+3 faults, while LRU has 3+4.
64
-
7/29/2019 Info324 Memory
65/84
Logic: the page that has been for longer time in mem has taken itschance to execute
when the memory is full, the oldest page is replaced.
So: first-in, first-out
simple to apply
But: a page that is frequently used is often the oldest, it will bereplaced replaced by FIFO!
First-In-First-Out (FIFO) Algorithm
-
7/29/2019 Info324 Memory
66/84
666666
Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
3 frames (3 pages can be in memory at a time per process)
4 frames
FIFO Replacement (BeladysAnomaly)more frames more page faults
1
2
3
1
2
3
4
1
2
5
3
4
9 page faults
1
2
3
1
2
3
5
1
2
4
5 10 page faults
44 3
First-In-First-Out (FIFO) Algorithm
C i FIFO LRU
-
7/29/2019 Info324 Memory
67/84
67
Comparaison FIFO - LRU
Contrarly to FIFO, LRU knows that the pages 2 and 5 are the
most frequently usedIn this case, the performance of FIFO is less than LRU:
LRU = 3+4, FIFO = 3+6
FIFO I l t ti
-
7/29/2019 Info324 Memory
68/84
FIFO Implementation
Easy to implement using a queue of frames of the memory thatshould be updated for each page fault
68
Second-Chance (clock) Page-Replacement
-
7/29/2019 Info324 Memory
69/84
69
( ) g pAlgorithm
like FIFO, but it takes into account the recent pages useCircular list Structure
the frames that has just been used (bit=1) are not replaced (secondchance)
the frames make conceptually a circular buffer
When a page is loaded in a frame, a pointeur points to the next frame of thebuffer
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 usedThe next frame of the buffer to be replaced will be the first onehaving the bit used = 0.
During this search, every used bit = 1 encountered is set to 0
Clock Algorithm: an example
-
7/29/2019 Info324 Memory
70/84
70
Clock Algorithm an example
the page 727 is loaded in the frame 4.
the next victim is 5, then 8.
Comparaison: Clock FIFO and LRU
-
7/29/2019 Info324 Memory
71/84
71
Comparaison: Clock, FIFO and LRU
* indicates that the used bit is 1The clock protects from the replacement of the frequently usedpages by setting the used bit to 1 at each reference
LRU = 3+4, FIFO = 3+6, Clock= 3+5
Details for Clock algo
-
7/29/2019 Info324 Memory
72/84
Details for Clock algo.
72
All the bits were 1. while searching all were
changed to 0 => the 1st page has been used
-
7/29/2019 Info324 Memory
73/84
File management System
73
File management System
-
7/29/2019 Info324 Memory
74/84
74
File management System
74
these exist No more!
5.25 3.5
Disk Structure
-
7/29/2019 Info324 Memory
75/84
75
Disk Structure
Hard disk drives are organized as a concentric stack ofdisks or platters
Each platter has 2 surfaces
View of a Hard Drive
-
7/29/2019 Info324 Memory
76/84
7676
View of a Hard Drive
-
7/29/2019 Info324 Memory
77/84
7777
Side View of Cylinders on Disk Drive
-
7/29/2019 Info324 Memory
78/84
7878
Side View of Cylinders on Disk Drive
Double-sided Disk
0
1
Sides orHeads
Cyl = 0Cyl = 79
CompriseCylinder 0
Spindle
Motor
Disk Drive
Cylinders
-
7/29/2019 Info324 Memory
79/84
7979
Cylinders
CYLINDER
Head StackAssembly
Head 0
Head 1
Head 2
Head 3
Head 4
Head 5
TrackSector
-
7/29/2019 Info324 Memory
80/84
80
Unix examples
-
7/29/2019 Info324 Memory
81/84
Unix examples
In the Unix system, the dynamic creation is simple
Creating a process that is an exact copy of the one asking for thecreation.
No parameters are necessary
id = fork();
the only distinction between the creator process (father) and thenew process (child) is in the returned value.
Id == -1 : creation failed
Id == 0 : the child processId != 0 : the identity of the child process
After the fork()function call, the two processes executethe remaining of the same program
81
id is the identifier of thenew process
Unix Example
-
7/29/2019 Info324 Memory
82/84
Unix Example
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 itschildren 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
82
Process creation example
-
7/29/2019 Info324 Memory
83/84
Process creation example#include #include
#include #include
int main(void){pid_t pid = fork();int reason;
switch (pid) {
case -1 : printf("Error in the creation "); exit(1);
case 0 : /* we are in the child */printf("Pid child = %d\n", getpid());sleep(20); //sleeps for 20 secondesbreak;
default : /* we are in the father */printf("Pid father = %d\n", getpid());printf(wait for child termination...\n");pid = wait(&raison);
} /* switch */
return (0);} 83
-
7/29/2019 Info324 Memory
84/84
End