560 likes | 836 Views
Operating Systems Chapter 4: Memory Management. Ki-Joune Li http://quantos.cs.pusan.ac.kr/html/lik.html. Basic Concepts of Memory Management. Memory (Main Memory) Manager Minix 에서는 세번째 Layer Unix 에서는 Process Scheduler 와 같이 동작 Windows/NT 에서는 Microkernel 위의 Layer.
E N D
Operating SystemsChapter 4: Memory Management Ki-Joune Li http://quantos.cs.pusan.ac.kr/html/lik.html
Basic Concepts of Memory Management • Memory (Main Memory) Manager • Minix에서는 세번째 Layer • Unix에서는 Process Scheduler와 같이 동작 • Windows/NT에서는 Microkernel 위의 Layer
가장 단순한 Memory Management • Problems • Relocation and Protection의 문제 • 제한된 Main Memory 에서 다양한 사용자의 요구를 만족 • Multiprogramming 환경에서의 Memory Management • 주요 개념 • Relocation and Protection • Swapping, Paging, and Segmentation User Space User Program - Code - Stack - Dynamic Memory OS Space 0x000000 Main Memory
Relocation User Space Loading User Program 0x100000 CodeStackDynamic Memory OS Space 0x0000 0x000000 Main Memory Variable A : 0x0010 Variable A : 0x0010 + 0x100000 Relocation User Program의 Address를 Main Memory의 Address로 Mapping
Protection Main Memory User Space *pA=malloc(100); // pA=0x100010*pB=pA-0x001000; //pB = ? 0x100000 Illegal Access OS Space 0x000000 How to protect it from illegal access ? Hint : Remember registers of Intel processor
제한된 Memory를 이용하여 사용자의 요구를 만족 User Program CodeStackDynamic Memory User Space 200 Mbytes > 120 Mbytes OS Space Main Memory 어떻게 사용자 요구를 만족시킬까 ?
Memory Management for Multiprogramming CodeStackDynamic Memory User Process 1 User Space CodeStackDynamic Memory User Process 2 OS Space CodeStackDynamic Memory Main Memory User Process 3 어떻게 여러 Process들이 사이 좋게 Memory를 공유할까 ?
Process 1 Partition 1 Process 2 Partition 2 Partition 3 Process 3 ? OS Space Partition Memory • Main Memory를 여러 개의 Partition 으로 쪼갬 • 여러 개의 Process가 하나의 Memory를 공유 • Partition의 크기는 ? • Fixed(Static Partition) : 미리 크기를 정하여 Partition • Variable Partition : Process의 요구 크기에 따라 가변적
Unused Space Process 2 Process 2 Process 1 Process 1 OS Space OS Space OS Space Problem : Process 2 Fragment Process 2 Process 3 OS Space MemoryCompaction OS Space No space for Process 3 Variable Partition
Data Structure for Memory Management An Unit of Memory Allocation A B C D E 8 16 24 방법 1 : Bitmap Array 방법 2 : Linked List Allocated Unused Length Process Hole 1 1 1 1 1 0 0 0 A 0 5 H 5 3 Start Unit 1 1 1 1 1 1 1 1 1 1 0 0 1 1 1 1 B 8 6 C 14 4 1 1 1 1 1 0 0 0 H 18 2 D 20 6
Bitmap vs. Linked List • Bitmap • 장점 : 무지 간단 • 단점 : 빈 공간을 찾기 위하여서는 많은 Bit을 검사 • Linked List • 단점 : 약간 복잡 (참을 만큼만) • 장점 : 빈 공간(Unused Space)의 관리 및 검색이 효과적 • Memory Compaction • Search Free Space and Swapping A 0 5 H 5 3 H 0 8 B 8 6 C 14 4 B 8 6 H 18 2 D 20 6 H 14 6 D 20 6
Search Free Space • 만일 Process K 가 N Units의 Memory 할당을 요구하면 • First Fit • Linked List 에서 가장 처음 나오는 N 크기이상의 Hole 할당 • 장점 : 빠름 • 단점 : Memory 낭비 또는 Fragmentation 이 발생 • Best Fit • Linked List 에서 N 크기 이상이며 낭비가 가장 적은 Hole • 장점 : Memory 낭비 혹은 Fragmentation이 가장 작음 • 단점 • 모든 Linked List의 Node를 조사 • 작은 Fragment 발생 : 오히려 Memory 낭비가 많아질 수도 • Worst Fit • Linked List 에서 N 크기 이상이며 가장 낭비가 많은 Hole
Swapping Virtual Memory • 제한된 Memory로 다양한 사용자의 요구를 만족하는 방법 • Multiprogramming 을 위한 Memory 관리 방법 • 개념 • Process가 요구하는 일부의 Memory만 Main Memory에. • 나머지는 Disk Space 를 이용 • 만일 Process 다른 부분이 Main Memory 참조 : Swapping 일부의 Memory Main Memory Disk Space
Paging • Memory를 할당하는 단위를 어떻게 결정할 것인가 ? • 1 Byte : 너무 많은 Overhead • 1 M Bytes • 심한 낭비 : 사용 안 하는 부분이 많음 • Swapping 시 발생하는 Disk Access가 비효율적 • 적당한 크기 : 512 bytes (1 sector) ~ 64 K bytes • 보통 1 K 나 4 K 정도가 가장 많이 사용 • Page : Memory를 할당하는 기본 단위 • 전체 Main Memory(Physical Memory)를 Page로 구분 • Page Frame • Main Memory의 Addressing : (Page 번호 + Offset)으로
8 – 12K 28 – 32K 6 X Virtual Memory Space Virtual Memory Space 44 – 48K 7 40 – 44K X 36 – 40K 5 Physical Memory Space 32 – 36K X 7: 28 – 32K 24 – 28K X 6: 24 – 28K 20 – 24K 3 5: 20 – 24K 16 – 20K 4 4: 16 – 20K 12 – 16K 0 3: 12 – 16K 2: 8 – 12K 4 – 8K 1 1: 4 – 8K 0 – 4K 2 0: 0 – 4K Disk Main Memory
Virtual Address : 3nd Virtual Page 6th Physical Page : 24K (=24576) Mapping from Virtual Address to Physical Address • Example • Move Reg, 8192 • Transformed to “Move Reg, 24576” • Memory Management Unit • 최대한 빠른 처리를 위하여 CPU Board 내의 전용 Chip • Virtual Address를 Physical Address로 전환 • Virtual Memory Address • Address Bit가 허용하는 한 무한정
MMU의 처리과정 • MMU의 처리과정 • 단계 1 : Virtual Page Number Physical Page Number • 단계 2 : Physical Page Number + Offset • Example • 8196 : 0010 0000 0000 0000 0100 Page Table 참조 Virtual Page Number Offset Page 2 110 Page 1 001 110 0000 0000 0000 0100 Page 0 010 Physical Page Number
Page Table • 기능 • Virtual Memory 에서 Physical Memory 대응관계 서술 • 각 Process 마다 하나씩 소유 • 내용 • Page Frame Number (Physical Page Number) • Absence/Presence Bit • Reference Bit : 참조가 되었는지를 나타내는 Flag • Caching Disable Bit : 특별한 I/O Device의 경우 • Dirty Bit : Main Memory에서 Modified가 되었는지 표시 • Dirty : Modified, Clean : Not Modified • Protection : Illegal 한 Update를 방지 • Size • 32 bit address space 와 Page 크기가 4K • 220 entries/Process !!
Page Table의 Size로 발생하는 문제 • Page Table의 존재 • 큰 Size로 Main Memory에 저장 • Instruction의 수행 : Memory Operand는 Page Table 참조 • 원래의 Main Memory Access + • Page Table 참조를 위한 Main Memory Access • 한 번 이상의 추가 Memory Access : 잠재적 Bottleneck • Page Table 자체가 Virtual Memory • 너무 Page Table 이 커지게 되면 • 예. 32 bits addressing, 4 K Page Size : Page Table의 Entry 수 : 220 Entries 필요 Page Table의 크기 220 *(4+4+20) bits =3.5 M bytes • Page Table을 참조하기 위하여 Page Table을 참조
Multilevel Page Table PT1 PT2 Offset Top Level 10 10 12 0 1 . . . 1022 1023 Second Level 0 1 . . . 1022 1023 . . . Page Frames Less Pages In Main Memory
Multilevel Page Table Ex. 0x00403004 PT1=1, PT2=3, Offset=4 0 1 . . . 1022 1023 0 1 2 3 . . . 0 1 2 3 . . . . . . Page Frames
Multilevel Page Table Ex. 0x00403004 PT1=1, PT2=3, Offset=4 0 1 . . . 1022 1023 0 1 2 3 . . . 0 1 2 3 . . . . . .
Multilevel Page Table Ex. 0x00403004 PT1=1, PT2=3, Offset=4 0 1 . . . 1022 1023 0 1 2 3 . . . 0 1 2 3 . . . . . .
Multilevel Page Table Ex. 0x00403004 PT1=1, PT2=3, Offset=4 0 1 . . . 1022 1023 0 1 2 3 . . . 0 1 2 3 . . . . . .
Virtual Page 22 23 221 140 0 0 0 Modified 1 R X R X Protection RW RW Page Frame 22 45 35 32 TLB(Translation Lookaside Buffer) • Virtual Address를 Physical Address로 전환하는 Hardware Device • Hardware적인 구현으로 빠른 Access • Associative Memory • RISC Machine 은 CPU 가 복잡 : S/W로 처리 Virtual Page Page Frame
Page Replacement Algorithms • Page Fault • 원하는 Virtual Page가 Main Memory에 없을 때 • Page Fault 가 발생하면 Swapping : Disk Access • Hit Ratio • Page Fault가 발생할 때, 어떤 놈을 쫓아내야 할까 ? • Page Replacement : 다음에 쓰여질 확률이 낮은 놈 • 적적한 Page Replacement는 성능을 크게 향상 • 즉 가능한 Hit Ratio가 증가하도록 • 집중성의 법칙 (Principle of Locality)
어떤 Page는 Replace되면 안될까 ? • 자주 사용되는 데이터나 프로그램 • Page Replacement가 일어나지 않도록 Lock • 어떤 Page ? • Kernel • I/O Buffer • Page Table • 그 외 중요한 Control Block
NRU : Not Recently Used • 사용된 지 가장 오래된 Page를 Replace • Physical Memory에 있는 Page 를 4 가지 종류로 분류 • 1. Not Referenced and Not Modified • 2. Not Referenced but Modified • 3. Referenced but Not Modified • 4. Referenced and Modified • 주기적으로 (e.g. 20ms) Referenced Bit를 reset • NRU : 위의 차례로 해당되는 Page를 Replace • 단순하면서 어느 정도의 성능은 제공 • 왜 위와 같은 순서 ?
First-In First-Out • Queue에 차례대로 Page를 기록 • Page Fault 가 발생하면, • Queue의 Front에 있는 Page를 Replace • 즉, 가장 오래 전부터 사용되던 Page를 Replace • Why ? • No Problem ?
Clock Page Replacement Algorithm Replaced
0 1 0 1 0 1 0 0 1 1 0 0 1 0 0 0 0 0 1 1 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 0 1 0 1 0 0 0 1 0 0 1 0 0 0 0 0 0 1 0 1 0 0 0 0 1 1 0 0 LRU : Least Recently Used • Page Fault 가 발생되면 • 가장 오랫동안 사용되지 않은 Page를 Replace • 가능한 처리과정 • Linked List로 관리 • Front : 가장 오래된 Page • Rear : 가장 최근에 사용된 Page • Page Replace : Linked List의 작업이 오래 걸림 • 다른 방법 : Matrix를 이용하는 Hardware 0 Page 0 Page 1 Page 3 Page 2 1
LFU : Not Frequently Used • 각 Page Frame에 대하여 • Counter를 관리 : Counter(P) • 각 Clock Tick 마다 : Counter(P) Counter(P) + R(P) • 만일 사용되지 않았으면 : R(P)=0 : Counter(P)는 불변 • Page Fault가 발생하면 • 가장 작은 Counter 값을 가지고 있는 Page를 Repalce
Thrashing • Working Set • 하나의 Process가 사용하는 Page의 집합 • 만일 Working Set의 전부가 Main Memory에 있으면 • Page Fault가 거의 일어나지 않음 • 일부의 Working Set이 Main Memory에 있으면 • 잦은 Page Fault가 발생할 수 있음 • Locality에 따라 Hit Ratio 는 가변적 • Thrashing • 각 Process에 할당된 Memory가 작을 경우 • 각 Instruction 의 수행마다 Page Fault : 최악의 경우
Time Sharing System의 경우 • Context Switching • Process가 점유하고 있던 Page Frame을 모두 반환 • 다시 CPU를 받아 실행될 경우 • 일정한 기간동안에 Page Fault가 발생 • 매우 심각한 성능의 저하 유발 • Prepaging • Working Set Model • 각각의 Process가 사용하는 Virtual Page의 Working Set에 대하여 사용빈도를 조사 • 사용빈도가 많은 Page 여러 개를 Context Switching 시 미리 Loaging
Local Allocation vs. Global Allocation • Local Allocation : Page Fault가 발생하면 • 각 Process의 Page Frame 중에서 Replace 할 것을 선택 • 일반적으로 각 Process 에게 정해진 Frame의 개수를 할당 • 예. 300 개의 Page Frames, 10개의 Process : 30 Frames/Process • 각 Process별로 일정한 Memory만 사용 • Process별로 서로 다른 크기의 Memory를 요구 : 비효율적 • Global Allocation : Page Fault가 발생하면 • 전체 Page Frame 중에서 Replace 할 것을 선택 • 각 Process별로 할당된 Page Frame의 수는 가변적 • Local Allocation 보다는 일반적으로 효율적
Local Allocation vs. Global Allocation A0 10 A0 A0 A1 7 A1 A1 A2 5 A5 A2 A3 6 A3 A3 + A5 A4 8 A4 A4 B0 5 B0 B0 B1 3 B1 B1 B2 2 B2 B2 B3 6 B3 B3 B4 7 B4 B4 B5 6 B5 B5 C0 4 C0 C0 C1 1 C1 A5 C2 3 C2 C2 Local Allocation Global Allocation
How Many Frames per Process • 간단한 관계 • n : 전체 page frame의 수 • p : page frame을 할당 받은 process의 수 • np : 각 process 별로 할당 받은 page frame의 평균 수 n =p np • 만일 np를 증가시키면 • Page Fault 가 감소하나 (hit ratio 가 증가) • p가 감소 : context switching overhead 증가 • 만일 np를 감소시키면 • Page Fault 가 증가하나 (hit ratio 가 감소) • p가 증가 : context switching overhead 감소
Page Fault와 평균 할당된 Page Frame의 수 Page Fault F L Process별로 할당된 평균 Page의 수 현재의 값 평균 Page 수를 증가 (Main Memory에 있는 Process의 수를 감소)
Process의 수와 전체적인 성능 Processor Utilization Process의 수 Process의 수가 증가함에 따라, Concurrency 증가 Process당 Page의 수가 작아 잦은 Page Fault : Thrashing
Page Size • Page Size 결정 요인 • Disk Access를 위하여 Block Size보다는 크게 • 가능한 최소의 Page Table • 작은 Page Size : 큰 Page Table • Internal Fragmentation의 최소 • 너무 큰 Page Size : 많은 Internal Fragmentation • 간단한 식 • s : 각 process가 요구하는 평균 memory 크기 (byte) • e : page table의 하나의 entry 크기 (byte) • p : page 크기 (byte) • C : overhead C = se/p + p/2 : p=(2se)1/2일 때, C가 최소 • 예: s=128K, e=8, p=1448 => 1 or 2 K bytes • 보통 512 bytes ~ 64K bytes
Segmentation • 하나의 Program (또는 Process)가 요구하는 Memory • Linear Space (Consecutive Addressing) 또는 • 별도로 관리되는 여러 개의 조각난 Memory • 각 조각은 물론 Linear Space • Stack, Code, Dynamic Memory • Segment • 하나의 Program 내에서 별도로 관리되는 조각난 Memory • 예 : • Intel Processor의 SS(Stack Segment), CS(Code Segment), DS(Data Segment) 등 • 각각의 Segment는 별도의 크기를 지정 • Segment가 없을 경우와 비교 : • 관리의 효율성
Segmentation의 장점 • 동적으로 변하는 각 종류의 Memory를 효과적으로 관리 • Loading 과 Linking 이 간편하여짐 • Protection • Modularity Code Stack Dynamic Memory Dynamic Memory Stack Code Linear Space
Segmentation의 구현 • 각 Segment 관리를 위한 정보 필요 • Segment의 Base Address • Modified/Referenced • Protection • 그 외 필요한 정보 Segment Descriptor • 각 Process(Program) 별로 • Code/Data/Stack 등이 각각의 Segment • Process : 여러 개의 Segments • 하나의 Process는 여러 개의 Segment Descriptor
Segment Process Virtual Page Virtual Page 순수한 Paging Segmentation + Paging Segmentation과 Paging • 독립적인 개념 • Segment : Memory를 관리하는 단위 • Paging : Memory를 할당하는 단위 • 따라서, 두 가지 개념을 동시에 사용 가능 • Segmentation with Paging
Segmentation with Paging 의 Addressing Segment Descriptor Table Process