[Linux, OS] Memory 관련 System Call | File System 관련 System Call

pos++·2024년 1월 4일
0

Linux

목록 보기
14/16
post-thumbnail

2023.11.16 TIL

Code, Image source: The Linux Programming Interface, Michael Kerrisk

Memory 관련 System Call

Memory Mapping

mmap() system call

  • Process address space에 새로운 memory mapping 생성
    • Mapping type : File
    • Mapping type : Anonymous

Process sharing

  • Process
    • File mapping으로 공유
    • fork()로 생성된 child process 참조

Private/Shared

  • MAP_PRIVATE
    • Process간 공유 X
  • MAP_SHARED
    • Process간 공유

File, Private

  • File을 memory처럼 읽을 수 있음. Memory 할당 없이!

File, Shared

  • IPC
    • tlpi-dist/mmap/t_mmap.c
    • $ dd if=/def/zero of=s.txt bs=1 count=1024 → file copy
    • $ ./t_mmap s.txt hello
    • $ ./t_mmap s.txt goodbye
    • $ od -c -w8 s.txt

Mapping 생성 - mmap()

#include <sys/mman.h>

void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);
  • flags : Private / Shared
  • Ex: addr = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); if(addr == MAP_FAILED) errExit("mmap");
  • tlpi-dist/mmap/mmcat.c

Mapp 영역 해제 - munmap()

#include <sys/mman.h>

int munmap(void *addr, size_t length);

Memory Protection Mode Changing - mprotect()

#include <sys/mman.h>

int mprotect(void *addr, size_t length, int prot);
  • tlpi-dist/vmem/t_mprotect.c

Memory lock - mlock(), mlockall()

#include <sys/mman.h>

int mlock(void *addr, size_t length);
int munlock(void *addr, size_t length);
  • tlpi-dist/vmem/memlock.c

File System 관련 System Call

File I/O Buffering

  • Kernel buffer cache

File System 구조

  • Disk virtualization

I-node

  • File format, owner, group, size, block number, block pointer

VFS(Virtual File System)

  • Virtualization of open, read, write, close
  • 하나의 interface로 호출

File System mount - mount()

#include <sys/mount.h>

int mount(const char *source, const char *target, const char *fstype, unsigned long mountflags, const void *data);

File attributes


  • Symbolic Link
    • 원본 파일을 지워도 I-node에 남아있다

File event monitoring

#include <sys/inotify.h>

int inotify_init(void);  // return fd on success, -1 on error

int inotify_add_watch(int fd, const char *pathname, uint32_t mask);  // return watch descriptor on success, -1 on error
  • File, directory 변화 감지
  • tlpi-dist/inotify/demo_inotify.c
profile
밀린 TIL 업로드 조금씩 정리중...

0개의 댓글