수정입니다

16. Segmentation 본문

전공/운영체제

16. Segmentation

nongdamgom 2024. 1. 9. 00:58

 

Inefficiency of the Base and Bound Apporach

  • 기존 base-bound 처럼 virtual addr 전체를 physical로 mapping 하면, free 상태인 사용하지 않는 공간도 함께 physical address를 사용하게 되므로, 메모리 낭비를 하게 됨

 

Segmentation

  • address space의 contiguous한 어떤 부분 부분들을 의미한다
  • 보통 code, stack, heap segment를 이야기함.
  • 각각의 segment들은 physical의 서로 다른 곳에 mapping 될 수 있고 , 크기도 다를 수 있음 
  • 각 segment들은 각자의 base-bound 를 가진다.

=> 사용하는 공간만 phsical에 relocation 하므로 절약 가능

 

 

Address Translation on Segmentation

physical address = offset + base
  • offset은 virtual address에서 각 section 내에서의 구분되는 위치 

 

 

 

Segmentation Fault or Violation

  • 할당되지 않은 공간을 사용하려 했을 때 나오는 에러 
  • hardware의 memory management unit이 out of bounds를 체크해서 OS에 알려줘서 띄운다 
int *a = (int *) = 7KB;  // 주소값 자체는 문제 x
int b = *a; // heap 할당 영역이 4KB~6KB 이므로 7KB 주소의 값에 접근하려 하면 에러가 난다.

 

 

 

Explicit approach

  • virtual address만 보고, offset과 base를 빠르게 알기 위해서 개발된 기법
  • 만약 address space가 16KB 이고, 한 segment가 4KB라고 가정했을 때,
  • 전체 virtual addr는 2^14B -> 14bit로 표현, 그 중 segment offset 2^12B = 12bit 제외
  • 남은 2bit로 어떤 segment인지 판단

 

 

Referring to Segment

Segment = (VirtualAddress & SEG_MASK) >> SEG_SHIFT

Offset = VirtualAddress & OFFSET_MASK
if(Offset >= Bounds[Segment])
	RaiseException(PROTECTION_FAULT)
else
	PhysAddr = Base[Segment] + Offset
    Register = AccessMemory(PhysAddr)

  • 뭐라고 딱히 설명하기가 힘들다. 걍 이렇게 한댄다.

 

 

Referring to Stack Segment

  • Stack은 아래에서 위로 자람 (code, heap은 위에서 아래로)
  • segment grow 가 1이면 위에서 아래, 0이면 아래에서 위로
Grow pysical address
1 physical address = offset + base
0 physical address =
(offset - maximum_segment_size) +base
  • 방향 따라 실제 메모리 계산 방법이 다르니 주의하자.

 

 

Support for Sharing

  • 같은 프로그램에서 여러개의 프로세스를 만들 때, code segment는 동일할 것임(같은 프로그램이니까..)
  • 동일한 code를 갖는 segment를 전부 physical mem에 할당하는 것은 낭비임
  • Code Sharing을 통하여 code가 같다면 공유한다.

==> 처음 만들어지는 프로세스의 code segment의 Protection을 Read-Execute로 설정.

==> 여러 프로세스가 공유하므로 write를 못하게 막는다.

 

 

Fine-Grained and Coarse-Grained

  • Coarse-Grained : segment를 적은 수로 만드는 것

=> 현재 더 많이 쓰는 방법

  • Fine-Grained : segment를 more flexibility하게 만드는 것 

=> 예전에 더 많이 썼고, 그 많은 segment를 관리하기 위해 segment table을 이용했다고 함.

 

 

OS support : Fragmentation

  • External Fragmentaion(외부 단편화)
  • physical memory에 free space들이 부분 부분 쪼개져서 존재하는 것
  • 예를 들어 실제로는 24KB가 free 임에도, 6KB 씩 쪼개져서 존재한다면, 20KB 요청이 들어왔을 때 제공할 수 없음

=> Compaction 을 통해 해결

  • physical mem의 segment를 한 쪽으로 몰아서 rearraging 해줌
  • 1. Stop running process (physical에 proces가 접근하지 못하도록 멈춤)
  • 2. Copy data to somewhere (physical에서 segment 복사 후 옮김)
  • 3. change segment register value(relocaion후 정보 update)

=> 매우 costly 하다.

'전공 > 운영체제' 카테고리의 다른 글

18. Paging: Introduction  (0) 2024.01.14
17. Free-Space Management  (1) 2024.01.11
15. Address Translation  (1) 2024.01.04
14. Memory API  (0) 2024.01.04
13. The Abstraction: Address Space  (1) 2024.01.04