Sie sind auf Seite 1von 57

Memory System

CS2600 (3003)
By
Dr. Sk Subidh Ali
Memory Hierarchy
Memory Hierarchy

0011010100
a.out 0001101001
Cache 0011010100

ALU
0101010000
0001101001
0011010100

0001101001
0101010000
1010010100
1100101010

CPU
0011101010

RAM Registers
a.out
gcc
$gcc a.c TextEd Reg.
a.c FF

Costlier
Faster
Cache
Void main() SRAM
{
int a ,b=9,c=8;
a=b+c Primary Memory
print(a) a.out a.c DRAM
}
Hard Disk
note.doc DAC.php Secondary Storage
Readme.txt Hard disk
$TexEdit a.c
Primary Memory
Organization
Primary Organization
RAM

Bare Machine RAM partitions: Monitor


Entire Mem. available to designer o Monitor S/W
Flexible to designer System
Suitable for embedded systems s/w
Mem. organization efficient o Scratch pad Scratch
Not suitable for general purpose Temporary pad
storage

RAM
0x0000
Resident monitor Monitor
0x00ff
Designer doesnt know executables (OS)
Fence Address 0x00ff
OS must be protected from users

Address Yes Scratch


CPU > pad
No
0ffff
TRAP
Fence Implementation
RAM
Software solution (compiler) 0x0000
Monitor
o Check each address 0x00ff
(OS)
o Pros Fence Address 0x00ff
Flexible
o Cons (1 compr. for 1 addr)
More instrn Address Yes Scratch
Slow CPU > pad
No
0ffff
TRAP

Hardware solution (comparator)


o Check each address
o Pros
Fast
o Cons (fix fence addr) OS size 1MB Fence address 0x0fffff
OS can not be changed OS size 2MB Fence address 0x1ffffe

One option is to use a GPR for fence address


Fence Implementation
Base Register
RAM
0x0000
Monitor
(OS)
Base Address 0x00ff

Logical Physical Scratch


CPU Address + Address pad

0x0ffff

a.out

PC start with 0x0000 0x0000 0001101001 0x00ff


0x0001 0011010100
Actual Mem. Addrs. PC + 0x00ff 0x0002 0101010000
0x0003 1010010100
0x0004 1100101010
0x0005 0011101010 0x0104

Physical Address
How does it protect OS? Logical Address
Multiprogramming
Multiprogramming Environment
Executes multiple programs together
hello
Running
0001101001
0011010100
New Ready Halted/
0101010000 ./hello exited
1010010100
1100101010
0011101010
Wait

Only one process executes


$gcc hello.c in CPU
Process must be in RAM for
a.c execution

0011010100
Need:
Void main()
{ Ready

ALU
int a ,b=9,c=8; queue
a=b+c
print(a) Waiting

0001101001
} queue a.out
gcc
TextEd
RAM
Registers CPU
unning program is called a process
Multiprogramming Environment
Executes multiple programs together

Running

New Ready Halted/


exited

Wait
./hello
Process hello created

HDD OS

ALU
hello
edit
encrypt
RAM
Registers CPU
DD : hard disc
Multiprogramming Environment
Executes multiple programs together

Running

New Ready Halted/


exited

hello Wait
./encrypt

Entire code of hello


is loaded in RAM

HDD OS

ALU
hello
edit
encrypt hello
RAM
Registers CPU
Multiprogramming Environment
Executes multiple programs together

Running

New Ready hello Halted/


exited

encrypt Wait
./edit

Instructions of hello are


getting executed in CPU

0011010100
HDD OS

ALU
hello

0001101001
edit
encrypt
encrypt hello
RAM
Registers CPU
Multiprogramming Environment
Executes multiple programs together

Running

New Ready hello Halted/


exited

encrypt Wait
edit

Instructions of hello is
getting executed in CPU
Void main()

0011010100
{ OS
int a= ,b=6,
I/O operations

ALU
c=8;
scan(a) executes in I/O
c=a+b+c
devices (slow)

0001101001
print(a)
print(c) take more time edit
} encrypt
hello
RAM
Registers CPU
Multiprogramming Environment
Executes multiple programs together

Running

encrypt Halted/
New Ready
exited
edit Wait encrypt start running in CPU

hello
hello is waiting for I/O
(scan(a) to complete)
Void main()

0011010100
{ OS
int a= ,b=6,
I/O operations

ALU
c=8;
scan(a) executes in I/O
c=a+b+c
devices (slow)

0001101001
print(a)
print(c) take more time edit
} encrypt
hello
RAM
Registers CPU
Multiprogramming Environment
Executes multiple programs together

Running

encrypt Halted/
New Ready
exited
edit Wait
hello encrypt start running in CPU

Each new process will move previous process to HDD


scan(a) completed

A lot of I/O Process Swap in

0011010100
HDD OS

ALU
Slow processing
hello

0001101001
edit edit
encrypt
encrypt hello
RAM
Swap out
ProcessRegisters CPU
Multiprogramming Environment
Executes multiple programs together

Running

encrypt Halted/
New Ready
exited
edit Wait encrypt start running in CPU

hello
hello is waiting for I/O
(scan(a) to complete)

0011010100
OS

ALU
Memory should be partitioned
for different processes

0001101001
edit
encrypt
hello
RAM
Registers CPU
Fixed Memory Partitioning
Memory Partitioning

n=6 MFT: Multiprogramming with Fixed number of Tasks/Proces

OS
OS MFT can handle up to n number of processes

Loader: Load processes to different partitiones


6K Scan
There are multiple process allocation strategies
4K
Strategy 1: First Fit
2K
While loading a process in memory scan top to bottom:
4K If a partition is available
If the size of process is less than the partition
6K
Load the process
1K
Fixed Memory Partitioning
Memory Partitioning

n=6 MFT: Multiprogramming with Fixed number of Tasks/Proces

OS
OS MFT can handle up to n number of processes

Loader: Load processes to different partitiones


6K
There are multiple process allocation strategies
4K
Strategy 1: First Fit
2K
While loading a process in memory scan top to bottom:
4K If a partition is available
If the size of process is less than the partition
6K
Load the process
1K

Process P1 size is 4K
Fixed Memory Partitioning
Utilized Memory Partitioning
Wasted

n=6 MFT: Multiprogramming with Fixed number of Tasks/Proces

OS MFT can handle up to n number of processes

P1 Loader: Load processes to different partitiones


6K
There are multiple process allocation strategies
4K
Strategy 1: First Fit
2K
While loading a process in memory scan top to bottom:
4K If a partition is available
If the size of process is less than the partition
6K
Load the process
1K

Process P1 size is 4K
Fixed Memory Partitioning
Utilized Memory Partitioning
Wasted

n=6 MFT: Multiprogramming with Fixed number of Tasks/Proces

OS
OS MFT can handle up to n number of processes

P1 Loader: Load processes to different partitiones


6K
There are multiple process allocation strategies
4K
Strategy 1: First Fit
2K
While loading a process in memory scan top to bottom:
4K If a partition is available
If the size of process is less than the partition
6K
Load the process
1K

Process P1 size is 4K
Process P2 size is 1k
Fixed Memory Partitioning
Utilized Memory Partitioning
Wasted/fragmentation

n=6 MFT: Multiprogramming with Fixed number of Tasks/Proces

OS
OS MFT can handle up to n number of processes

P1 Loader: Load processes to different partitiones


6K
There are multiple process allocation strategies
4K P2
Strategy 1: First Fit
2K
While loading a process in memory scan top to bottom:
4K If a partition is available
If the size of process is less than the partition
6K
Load the process
1K
Pros
Simple
Process P1 size is 4KCons
Process P2 size is 1k Wastage or fragmentation of memory
Internal fragmentation is high
Fixed Memory Partitioning
Memory Partitioning

n=6 MFT: Multiprogramming with Fixed number of Tasks/Proces

OS
OS MFT can handle up to n number of processes

Loader: Load processes to different partitiones


6K
There are multiple process allocation strategies
4K
Strategy 2: Best Fit
2K
While loading a process in memory scan all:
4K Choose the best partition
Load the process
6K

1K

Process P1 size is 4K
Fixed Memory Partitioning
Memory Partitioning

n=6 MFT: Multiprogramming with Fixed number of Tasks/Proces

OS
OS MFT can handle up to n number of processes

Loader: Load processes to different partitiones


6K
There are multiple process allocation strategies
4K
Strategy 2: Best Fit
2K
While loading a process in memory scan all:
4K Choose the best partition
Load the process
6K

1K

Process P5 size is 5K
Fixed Memory Partitioning
Memory Partitioning

n=6 MFT: Multiprogramming with Fixed number of Tasks/Proces

OS
OS MFT can handle up to n number of processes

Loader: Load processes to different partitiones


6K
There are multiple process allocation strategies
4K
Strategy 2: Best Fit
2K
While loading a process in memory scan all:
4K Choose the best partition
Load the process
6K
External fragmentation
1K

Process P5 size is 5K
Fixed Memory Partitioning
Memory Partitioning

n=6 MFT: Multiprogramming with Fixed number of Tasks/Proces

OS
OS MFT can handle up to n number of processes

Loader: Load processes to different partitiones


6K
There are multiple process allocation strategies
4K
Strategy 2: Best Fit
2K
While loading a process in memory scan all:
4K Choose the best partition
Load the process
6K
External fragmentation
1K
Pros
Less internal fragmentation
Process P5 size is 5K Cons
Complex
External fragmentation
Variable Memory Partitioning
Memory Partitioning

MVT: Multiprogramming with variable number of Tasks/


10K OS
OS Initially, the memory is one segment
Keep loading process:

Processes in ready queue:


P1 (10K)<=P2 (15K)<=P3 (20K)<=P4 (20K)
50K
Fixed Memory Partitioning
Memory Partitioning

MVT: Multiprogramming with variable number of Tasks/


10K OS
OS Initially, the memory is one segment
P1(10K) Keep loading process:

P2(15) Processes in ready queue:


P1 (10K)<=P2 (15K)<=P3 (20K)<=P4 (20K)
50K P1 (10K)+P2 (15K)+P3 (20K)=45K
P3(20K) 5K of external fragmentation

External fragmentation
Fixed Memory Partitioning
Memory Partitioning

MVT: Multiprogramming with variable number of Tasks/


10K OS
OS Initially, the memory is one segment
P1(10K) Keep loading process:

Processes in ready queue:


P1 (10K)<=P2 (15K)<=P3 (20K)<=P4 (20K)
50K P1 (10K)+P2 (15K)+P3 (20K)=45K
P3(20K) 5K of external fragmentation
P2 completes 20K free space still P4 can not run
External fragmentation
Pros
No internal fragmentation
Cons
Complex implementation
External fragmentation
Fixed Memory Partitioning
Memory Partitioning
Compaction
Shift existing processes byte-by-byte up or down to make
a big partition
10K OS
OS
P1(10K)

50K
P3(20K)
Fixed Memory Partitioning
Memory Partitioning
Compaction
Shift existing processes byte-by-byte up or down to make
a big partition
10K OS
OS
P1(10K) Compaction can overcome
external fragmentation up to
P3(20K) certain extent
50K
How to keep track of partitions

Maintaining a table will waste memory

Use link list


Fixed Memory Partitioning
Memory Partitioning
Head: Points the first partition
LL Size Tag
FHead
RL

10K OS
OS
Up Link Tag

LL Size Tag RL AHead

50K
Up Link Tag
P3(30K)
LL Size Tag RL There will be two link
lists: empty and allocated

Up Link Tag
Size: Size of the partition
Tag=0/1 (partition is empty/allocated) List should have option to
merge to empty partition
Left Link (LL): points to previous empty partition
Null
Right Link (RL): points to next empty partition
Up Link : points to its own LL
Fixed Memory Partitioning
Memory Partitioning
Head: Points the first partition
LL Size Tag
FHead
RL

10K OS
OS
Up Link Tag
AHead
LL Size Tag RL
P

if (P+size)->Tag=0
50K {
Up Link Tag
P=(P+size +(P+size)->size +
LL Size Tag RL P->Tag=0
P->UpLink=P->Uplink->LL
}

Up Link Tag
Size: Size of the partition
Tag=0/1 (partition is empty/allocated)
Left Link (LL): points to previous empty partition
Null
Right Link (RL): points to next empty partition
Up Link : points to its own LL
Fixed Memory Partitioning
Memory Partitioning
Head: Points the first partition
LL Size Tag
FHead
RL

10K OS
OS
Up Link Tag
AHead
LL Size Tag RL
P

if (P+size)->Tag=0
50K {
Up Link Tag
P=(P+size +(P+size+1)->siz
LL Size Tag RL P->Tag=0
P->UpLink=P->Uplink->LL
}

Up Link Tag
Size: Size of the partition
Tag=0/1 (partition is empty/allocated)
Left Link (LL): points to previous empty partition
Null
Right Link (RL): points to next empty partition
Up Link : points to its own LL
Fixed Memory Partitioning
Memory Partitioning
Head: Points the first partition
LL Size Tag
FHead
RL

10K OS
OS
Up Link Tag
AHead
LL Size Tag RL
P

if (P+size)->Tag=0
50K {
Up Link Tag
P=(P+size +(P+size+1)->siz
LL Size Tag RL P->Tag=0
P->UpLink=P->Uplink->LL
}

Up Link Tag
Size: Size of the partition
Tag=0/1 (partition is empty/allocated)
Left Link (LL): points to previous empty partition
Null
Right Link (RL): points to next empty partition
Up Link : points to its own LL
Fixed Memory Partitioning
Memory Partitioning
Head: Points the first partition
LL Size Tag
FHead
RL

10K OS
OS
Up Link Tag
AHead
LL Size Tag RL
P

if (P+size)->Tag=0
50K {
Up Link Tag
P=(P+size +(P+size+1)->siz
LL Size Tag RL P->Tag=0
P->UpLink=P->Uplink->LL
}

Up Link Tag
Size: Size of the partition
Tag=0/1 (partition is empty/allocated)
Left Link (LL): points to previous empty partition
Null
Right Link (RL): points to next empty partition
Up Link : points to its own LL
Fixed Memory Partitioning
Memory Partitioning
Head: Points the first partition
LL Size Tag
FHead
RL

10K OS
OS
Up Link Tag
AHead
LL Size Tag RL
P

if (P+size)->Tag=0
50K {
Up Link Tag
P=(P+size +(P+size+1)->siz
LL Size Tag RL P->Tag=0
P->UpLink=P->Uplink->LL
P->uplink->RL=
P->uplink->RL->
Up Link Tag
Size: Size of the partition
}
Tag=0/1 (partition is empty/allocated)
Left Link (LL): points to previous empty partition
Null
Right Link (RL): points to next empty partition
Up Link : points to its own LL
Fixed Memory Partitioning
Memory Partitioning
Head: Points the first partition
LL Size Tag
FHead
RL

10K OS
OS

if (P+size)->Tag=0
50K {
P=(P+size +(P+size+1)->siz
P->Tag=0
P->UpLink=P->Uplink->LL
P->uplink->RL=
P->uplink->RL->
Up Link Tag Ahead=NULL
Size: Size of the partition
}
Tag=0/1 (partition is empty/allocated)
Left Link (LL): points to previous empty partition AHead
Null
Right Link (RL): points to next empty partition
Up Link : points to its own LL
Fixed Memory Partitioning
Memory Partitioning
LL Size Tag RL

10K OS
OS
Up Link Tag

LL Size Tag RL

50K
Up Link Tag
P3(30K)
LL Size Tag RL There will be two link
lists: empty and allocated

Up Link Tag
Size: Size of the partition
Tag=0/1 (partition is empty/allocated) List should have option to
merge to empty partition
Left Link (LL): points to previous empty partition
Right Link (RL): points to next empty partition
Up Link : points to its own LL
Fixed Memory Partitioning
Memory Partitioning
LL Size Tag RL

10K OS
OS
Up Link Tag

LL Size Tag RL

50K
Up Link Tag
P3(30K)
LL Size Tag RL There will be two link
lists: empty and allocated

Up Link Tag
Size: Size of the partition
Tag=0/1 (partition is empty/allocated) List should have option to
merge to empty partition
Left Link (LL): points to previous empty partition
Right Link (RL): points to next empty partition
Up Link : points to its own LL
Paged Memory
Organization
Paging
Physical
Process Memory

4K

12K
Paging
Memory partitions are of equal size Physical
Process Memory
0 1K Block Page 0
1
1
2 1K Block Frame
2
3
3
4K Logical 4
5
6
7
8
9
10
11
12K Physical
OS is omitted in Fig.
Paging
Memory partitions are of equal size Physical
Process Memory
0
0 2 0
1
1 7 1
2
2 8 2
3
3 10 3
4K Logical 4
Page Mapping Table(PMT)
5
6
7
8
9
10
11
12K Physical
Paging
Memory partitions are of equal size Physical
Process Memory
0
0 2 0
1
1 7 1
2
2 8 2
3
3 10 3
4K Logical 4
5
6
CPU PNo Offset FNo Offset
7
Logical Addr Physical Addr
8
9
Your memory management
10
allows
at max 64 pages to a 11
process. The page size is 12K Physical
512Byte and Physical
memory is 2MB. What is the
size of physical and logical
address?
Multiple Process in Paging
Memory partitions are of equal size Physical
P0 Memory
0 0 2
0
1 1 7
1
2 2 8
2
3 3 10
3
4K Logical PMT1 4
P1
0 0 5
0
1 1 6
1
2 3 7
2
3 4 8
3
4 11 9
4
PMT2 10
5K Logical
11
No need for contagious allocation
12K Physical
Pros: No external fragmentation
Cons: Complex Mapping and internal
fragmentation
Code Sharing in Paging
Memory partitions are of equal size Physical
P0 Memory
0 0 5
0
1 1 6
1
2 2 9
2
3 3 10
3
4K Logical PMT1 4
P1
0 5 5
0
1 6 6
1
2 9 7
2
3 4 8
3
4 11 9
4
PMT2 10
5K Logical
11
P0 and P1 share Firefox i.e., open
two instances of Firefox
12K Physical
P0 Data

P1 Data
Firefox code should not be modified by P0 and P1
Code of Reentrant code
Segment Memory
Organization
Segmentation
Memory partitions are of equal size Physical
P0 Memory
0
Each module get a 0
Main
new memory 1
segment 2
Squire 1
Segment Table (ST) 3
printf 2
3 0 4
scanf
1 5
Structure programming
2 BA Lmt 6
3 7 Squire
Can we share code?
8 printf
9
CPU SNo Offset BA 10 scanf
Logical Addr 11
12K Physical
Y
SNo: Segment no
BA: Base address
< +
N Physical Addr
Lmt: Limit of the address Trap
Paged Segment Memory
Organization
Paged Segmentation
P0
Physical
Memory
0
0
Main
How offset and offset1 related? 1 Main
1 2
Squire
ST 3
printf 2
scanf 3 4
PT 5
Structure programming
BA Lmt 6
7 Squire
Lmt 8 printf
Y BA
< 9
N 10 scanf
Trap
+ 11
12K Physical
CPU SNo Offset PNo Offset1 FNo Offset1

Logical Addr Physical Addr


Demand Paging/Virtual
Memory
Paging
Memory cannot accommodate all processes
Process Physical Memory
0 Fno Flag
0
1 0 2 1 1
2 1 7 1 2
3 2 8 1 3
4K Logical 3 0
4
5
6
CPU PNo Offset FNo Offset
Physical Addr
7
Request Instn/Data Logical Addr
8
9
10
Memory Management Unit (MMU)
11
12K Physical
When a requested page is not there in memory a Page
Fault interrupt occurs

No of page allocated to a process depends on MMU


Page Fault Processing
age fault: Page Pi is not in memory (flag of Pi is 0)

if (Flag=0/ page fault)


{
Halt current instruction execution /*Processor wait for I/O*/
if (frame available)
Load the Pi to memory
else /*Replace existing page in memory*/
{
Select a page Pm in memory /*Follow page replacement Algo*/
Adjust page table of Pm
If (Pm modiified)
{
write Pm to disc
Load Pi in the frame of Pm
}
else
Load Pi in the frame of Pm
}
}
Page Replacement Algorithm
Page Replacement Algo.
ocal vs. Global: In local same process's page is replaced where as in global other
rocesss page is replace

First in First Out (FIFO):

70120304
7 7 7 2 2 2 2 4
0 0 0 0 3 3 3
1 1 1 1 0 0

Belady Anomally
Page Replacement Algo.
ocal vs. Global: In local same process's page is replaced where as in global other
rocesss page is replace

First in First Out (FIFO):

70120304
7 7 7 2 2 2 2 4
0 0 0 0 3 3 3
1 1 1 1 0 0

Belady Anomally

123412512345
1 1 1 4 4 4 5 3 3 3
2 2 2 1 1 1 1 4 5
3 3 3 2 2 2 2 5
1234567778910
Page Replacement Algo.
ocal vs. Global: In local same process's page is replaced where as in global other
rocesss page is replace

First in First Out (FIFO): Least Recent Used (LRU)

70120304 Thrashing: Is a situation or state


when due to
7 7 7 2 2 2 2 4 heavy page fault, MMU keep
0 0 0 0 3 3 3 increasing degree
1 1 1 1 0 0 of multiprogramming which again
increase page fault. It end up CPU
Belady Anomally doing nothing
123412512345
1 1 1 1 5 5 5 5 4 4
123412512345
2 2 2 2 1 1 1 1 5
1 1 1 4 4 4 5 3 3 3 3 3 3 3 2 2 2 2
2 2 2 1 1 1 1 4 5 4 4 4 4 3 3 3
3 3 3 2 2 2 2 5
1234445678910
1234567778910

Das könnte Ihnen auch gefallen