본문 바로가기

OS

[운영체제] 5. Memory and Address Space

  1. Memory 관련 여러 이슈
    1. Memory Leak : branch instruction 등을 계속 사용해 jump 할 경우 free를 하지 못해 할당한 공간 계속 유지되는 경우 생기는데, 이를 heap의 memory 영역이 샌다고 해서 memory leak이라고 부름
    2. Dangling Pointer : dangling pointer가 가리키는 메모리가 free되어 더이상 유효하지 않음. 이렇게 free된 부분에 접근될 경우 예측 불가능한 동작, 만약 이 메모리에 접근이 불가능할 경우에는 segfault
    3. double free: 이미 free한 메모리를 한번 더 free 갈김: undefined error; 컴파일러가 아무거나 해도 됨
  2. memory 할당
    1. calloc() : malloc과 유사하지만 할당 후 공간을 0으로 채움
    2. void *calloc(size_t num, size_t size) // size_t num : number of blocks to allocate // size_t size : size of each block
    3. realloc() : memory block의 size를 변경하는 것을 의미:
    4. void *realloc(void *ptr, size_t size) // void* ptr: pointer to memory block allocated with malloc, calloc, or realloc // size_t size: New size for the memory block
    5. malloc 시 쓰는 system call
      1. brk → heap 공간의 끝단을 조절하는 함수
      2. sbrk → brk와 유사; 얼마만큼 늘려줘~
    6. memory mapping 시 쓰는 system call
      1. mmap : memory와 file을 mapping해줌 → annonymous memory region을 생성할 수 있음
      #include <sys/mman.h>
      
      void *mmap(void *ptr, size_t length, int port, int flags,
      int fd, off_t offset)
      
      //open한 file의 fd를 넣으면, file에 읽고 쓸 수 잇음 (file에 매핑)
      
  3. memory virtualization : hardware 지원 필요하다,,
    1. register, TLB, page-table
  4. Address Translation
    1. Hardware 수행 (CPU; MMU)
    2. virtual address → physical address (실제 저장되어 있는 주소)
    3. OS : limited direct execution 을 위해 특정 예외조건에서는 운영체제가 관여 (e.g. trap → OS가 handle 후 return to trap) → 여러 가지 케이스 있음
    4. base and bound register :
      1. base : 시작 주소 만들 때 사용,, 프로그램 시작할 때 물리 메모리의 어느 부분에서 process가 load되어야 하는지 → physical address = virtual address + base
      2. bound : virtual address의 상한선 만들기 위해 사용
      3. base-and-bound 구현할 때, OS가 끼어들 타이밍
        1. process 실행 시작할 때 → physical address에서 연속된 공간 찾아줘야 → free list 탐색
        2. process 작업 종료 → 프로세스 자원 뺏고 다른 곳이 쓸 수 있도록 → process 종료 시 다시 free list에 넣어줄 때
        3. context swith 발생 : base-and-bounds 쌍을 저장하고 다른 프로세스의 것 가져와야 함. (process 구조체나 process control block에 이 값들 저장)
반응형

'OS' 카테고리의 다른 글