Memory Management
본 글은 다음 강의를 들으며 정리한 내용입니다.
강의 정보 : 운영체제 / 이화여대 반효경
강의 링크
1. Logical vs Physical Address
주소 바인딩 : 주소를 결정하는 것 (주소 변환) ↙ 이 시점
Symbolic Address ➔ Logical Address ➔ Physical Address
2. 주소 바인딩 (Address Binding)

3. Memory-Management Unit (MMU)
3-1. Dynamic Relocation

3-2. Hardware Support for Address Translation

운영체제 및 사용자 프로세스 간의 메모리 보호를 위해 사용하는 레지스터
4. Some Terminologies (몇가지 용어)
4-1. Dynamic Loading
-
프로세스 전체를 메모리에 미리 다 올리는 것이 아니라 해당 루틴이 불려질 때 메모리에 load하는 것
-
memory utilization의 향상
-
가끔씩 사용되는 많은 양의 코드의 경우 유용 (ex. 오류 처리 루틴)
-
운영체제의 특별한 지원 없이 프로그램 자체에서 구현 가능 (OS는 라이브러리를 통해 지원 가능)
Loading : 메모리로 올리는 것
4-2. Dynamic Linking
4-3. Overlays
4-4. Swapping

5. Allocation of Physical Memory
메모리는 일반적으로 두 영역으로 나뉘어 사용
-
OS 상주 영역
- interrupt vector와 함께 낮은 주소 영역 사용
-
사용자 프로세스 영역
사용자 프로세스 영역의 할당 방법 :
- Contiguous allocation (연속 할당)
- 각각의 프로세스가 메모리의 연속적인 공간에 적재되도록 하는 것
- Fixed partition allocation (고정분할 방식)
- Variable partition allocation (가변분할 방식)
- Noncontiguous allocation (불연속 할당) (현대식)
- 하나의 프로세스가 메모리의 여러 영역에 분산되어 올라갈 수 있음
- Paging
- Segmentation
- Paged Segmentation
5-1. Contiguous Allocation


- Hole
- 가용 메모리 공간
- 다양한 크기의 hole들이 메모리 여러 곳에 흩어져 있음
- 프로세스가 도착하면 수용 가능한 hole을 할당
- 운영체제는 다음의 정보를 유지
5-1-1. Dynamic Storage-Allocation Problem
가변 분할 방식에서 size n
인 요청을 만족하는 가장 적절한 hole을 찾는 문제
-
First-fit
- Size가 n 이상인 것 중 최초로 찾아지는 hole에 할당
-
Best-fit
- Size가 n 이상인 가장 작은 hole을 찾아서 할당
- hole들을 리스트가 크기순으로 정렬되지 않은 경우 모든 hole의 리스트를 탐색해야 함
- 많은 수의 아주 작은 hole들이 생성됨
-
Worst-fit
- 가장 큰 hole에 할당
- 역시 모든 리스트를 탐색해야 함
- 상대적으로 아주 큰 hole들이 생성됨
Firtst-fit과 Best-fit이 Worst-fit보다 속도와 공간 이용률 측면에서 효과적인 것으로 알려짐 (실험적인 결과)
- Compaction
- external fragmentation 문제를 해결하는 한 가지 방법
- 사용 중인 메모리 영역을 한군데로 몰고 hole들을 다른 한 곳으로 몰아 큰 block을 만드는 것
- 매우 비용이 많이 드는 방법임
- 최소한의 메모리 이동으로 compaction하는 방법 (매우 복잡한 문제)
- compaction은 프로세스의 주소가 실행 시간에 동적으로 재배치 가능한 경우에만 수행될 수 있다.
5-2. Noncontiguous Allocation
5-2-1. Paging
-
Paging
- process의 virtual memory를 동일한 사이즈의 page 단위로 나눔
- virtual memory의 내용이 page 단위로 noncontiguous하게 저장됨
- 일부는 backing storage에, 일부는 physical memory에 저장
-
Paging Basic Method
- physical memory를 동일한 크기의 frame으로 나눔
- logical memory를 동일 크기의 page로 나눔 (frame과 같은 크기)
- 모든 가용 frame들을 관리
- page table을 사용하여 logical address를 physical address로 변환
- External fragmentation 발생 안함
- Internal fragmentation 발생 가능
Example :

Address Translation Architecture

- Implementation of Page Table
- Page table은 main memory에 상주
- Page-table base register(PTBR)가 page table을 가리킴
- Page-table length register(PTLR)가 table 크기를 보관
- 모든 메모리 접근 연산에는 2번의 memory access 필요
- Page table 접근 1번, 실제 data/instruction 접근 1번
- 속도 향상을 위해 associative register 혹은 translation look-aside buffer(TLB)라 불리는 고속의 lookup hardware cache 사용
Paging Hardware with TLB



Example :
-
logical address (on 32-bit machine with 4KB page size)의 구성
- 20-bit의 page number
- 12-bit의 page offset
-
page table 자체가 page로 구성되기 때문에 page number는 다음과 같이 나뉜다. (각 page table entry가 4B)
- 10-bit의 page number
- 10-bit의 page offset
-
따라서 logical address는 다음과 같다 :

2단계 paging에서의 Address-Translation Scheme :

- Multilevel Paging and Performance
- Address space가 더 커지면 다단계 page table 필요
- 각 단계의 page table이 메모리에 존재하므로 logical address의 physical address 변환에 더 많은 메모리 접근 필요
- TLB를 통해 메모리 접근 시간을 줄일 수 있음
- 4단계 page table을 사용하는 경우
- 메모리 접근 시간이 100ns, TLB 접근 시간이 20ns이고 TLB hit ratio가 98%인 경우
➔ effective memory access time = 0.98 x 120 + 0.02 x 520 = 128 nanoseconds
➔ 결과적으로 주소 반환을 위해 28ns만 소요
Valid(v) / Invalid(i) Bit in a Page Table

- Memory Protection
- Page table의 각 entry마다 아래의 bit를 둔다.
- Protection bit
- page에 대한 접근 권한 (read/write/read-only)
- Valid-invalid bit
- valid는 해당 주소의 frame에 그 프로세스를 구성하는 유효한 내용이 있음을 뜻함 (접근 허용)
- invalid는 해당 주소의 frame에 유효한 내용이 없음을 뜻함 (접근 불허)
해당 주소의 frame에 유효한 내용이 없다 :
- 프로세스가 그 주소 부분을 사용하지 않는 경우
- 해당 페이지가 메모리에 올라와 있지 않고 swap area에 있는 경우
- Inverted Page Table
- page table이 매우 큰 이유
- 모든 process 별로 그 logical address에 대응하는 모든 page에 대해 page table entry가 존재
- 대응하는 page가 메모리에 있든 아니든 간에 page table에는 entry로 존재
- Inverted page table
- page frame 하나당 page table에 하나의 entry를 둔 것 (system-wide)
- 각 page table entry는 각각의 물리적 메모리의 page frame이 담고 있는 내용 표시 (process-id, process의 logical address)
- 단점 : 테이블 전체를 탐색해야 함
- 조치 : associative register 사용 (expensive)
Inverted Page Table Architecture

-
Shared Page
- Re-entrant Code (= Pure code)
- read-only로 하여 프로세스 간에 하나의 code만 메모리에 올림 (ex. text editors, compilers, window systems)
- Shared code는 모든 프로세스의 logical address space에서 동일한 위치에 있어야 함
-
Private code and data
- 각 프로세스들은 독자적으로 메모리에 올림
- Private data는 logical address space의 아무 곳에 와도 무방
Example :

5-2-2. Segmentation
-
프로그램은 의미 단위인 여러 개의 segment로 구성
- 작게는 프로그램을 구성하는 함수 하나하나를 segment로 정의
- 크게는 프로그램 전체를 하나의 segment로 정의
- 일반적으로는 code, data, stack 부분을 하나씩 segment로 정의
-
Segment는 다음과 같은 logical unit들임
- main()
- function
- global variables
- stack
- symbol table
- arrays
-
Segmentation Architecture
- Logical address는 다음의 두 가지로 구성
- Segment table
- each talbe entry has :
- base - starting physical address of the segment
- limit - length of the segment
- Segment-table base register (STBR)
- 물리적 메모리에서의 segment table의 위치
- Segment-table length register (STLR)
- 프로그램이 사용하는 segment의 수
- segment number
s
is legal if s < STLR
Segmentation Hardware

- Segmentation Architecture (Cont.)
- Protection
- 각 segment 별로 protection bit가 있음
- each entry :
- valid bit = 0 ➔ illegal segment
- read/write/execution 권한 bit
- Sharing
- shared segment
- same segment number
segment는 의미 단위이기 때문에 공유(sharing)와 보안(protection)에 있어 paging보다 훨씬 효과적이다.
- Allocation
- first fit / best fit
- external fragmentation 발생
- segment의 길이가 동일하지 않으므로 가변 분할 방식에서와의 동일한 문제점들이 발생
Example of Segmentation

Sharing of Segments

5-2-3. Segmentation with Paging
- pure segmentation과의 차이점
- segment-table entry가 segment의 base address를 가지고 있는 것이 아니라 segment를 구성하는 page table의 base address를 가지고 있음
