Hi there!

I am a student studying computer science.

시스템 프로그래밍

시스템 프로그래밍 3장 - System call

만능성구 2020. 5. 5. 11:57
728x90

System call file I/O

직접적으로 실행가능한 file io

Linux file system

- linux파일 시스템은 여러가지로 모듈화 되어있다

- 가장 중요! disk의 file을 관리한다.

- user에게 system call API를 제공한다.

이를 통해서 유저는 kernel에 접근 가능하다.

kernel은 자신이 관리하는 구조에 대해서 접근,쓰기,읽기 권한을 관리한다.

각 file은 inode를 가지고 그것을 통해서 file에 대한 정보, metadata를 관리된다.

이건은 Data structure이다.

inode contents

  • filename
  • file type
  • file owner id
  • access perission 접근 권한 rwx r-x r-x(사용자, 그룹, 다른)
  • creation/modified time 접근, 생성, 수정 시간
  • file size
  • file addr disk에 어디에 적혀잇는지
  • file data block addr. table(see the next page) inode에 기록되어있는 정보 중에서 block address

Inode structure

inode의 block단위는 일반적 큰사이즈 512byte, 1kb, 2kb, 4kb로 구성되고 대부분 4kb이다.

disk를 4kb로 나누고 하나의 block의 block address를 할당한다. 이것이 하나의 파일이다.

block pointer의 크기는 4bytes이다.

inode의 처음에 information이 있다. id, 이름, addr등

File types

  • regular file
  • directory file
  • FIFO file
  • special file
  • special files
  • symbolic link files

oridinarly file (regular file)

일반파일, ex)텍스트, 실행, 이미지, 비디오, 등 ..binary

directory file

dir도 하나의 파일이다.

자신의 가진 파일에 대한 정보를 가지고 있다. # inode와 file이름을 list로 가진다.

special file

연결된 장치에 user가 접근할 수 있게 해주기 위해서 file형태의 구조체를 통해서 장치에 접근할 수 있는 통로를 만들어주는 파일

  • character special file
    • I/O device에 대한 I/O연산을 수행할 수 있도록 제공하는 file
    • Block-oriented devie z.B.) keybord, mouse //character형태로 되어있는 device
  • block special file
    • 큰 영역의 용량의 data를 주고받는 것 z.B.) hdd, 이더넷 block-oriented device

FIFO file

파이프 형태로 process간 data를 주고받기 위한 매개체를 file로 만든 것

symbolic link

다른 file에서 우회로 접근할 수 있는 경로를 만들어주는 파일


File descriptor

- small, non-negative integer

- kernel에서 file을 handle하기 위해, kernel에서 file에 할당해 준 정수.

- kernel 내부에서 사용한다.

위에서 본 ionode는 file system에서 정보를 구분하기 위한 매체로 실제로는 file descriptor를 사용학는 것이다.

user가 조작을 하기위해서 반환값을 통해서 조작을하는데 file descirptor

  • file descritor of stdin : 0
  • file descritor of stdout : 1
  • file descritor of stderr : 2

어떤 파일을 user levek에서 조작할 때, open할 때 마다 file descriptor을 반환 받을 수있다.

Maximum number of files

그런데 너무 많이 받으면 resoure자원의 한계를 느낀다.

이것을 방지하기 위해 조작을 할 수 있는 파일의 개수를 1024개로 제한해놓았다


Basic file I/O

open(2)

close(2)

lseek(2)

read(2)

write(2)

#include<fcntl.h>

int open(const char *path, int oflag);

int open(const char *path, int oflag, mode_t mode);

path : file 이름

oflag : 옵션

     무조건

     O_RDONLY : 읽기만 , 0

     O_WRONLY : 쓰기만 ,1

     O_RDWR : 일고쓰기, 2

선택

    O_CREAT : 없으면 만들어와라

    O_EXCL : O_CREATE와 같이 쓸 때 파일이 존재하면 error발생,

    O_TRUNC : 파일이 존재할 경우에는 기존의 파일의 크기를 0으로 만들어라

    O_APPEND : 파일이 존재할 경우 마지막에 data를 추가해라

    O_SYNC : file을 open한 이후로 동기화를 맞추어서 읽고 써라

mode : 접근 권한 (생성할 때)

file acces modes 15개 r/w/x/rwx

return file descriptor / 1

#incldue<unistd.h>

int close(int fd);

kernel 내부에서 memory에 만들어진 data struct를 자유롭게 풀어준다.  user가 닫아준다.

fd: file descriptor

return 0, error -1

open

File creation

#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>

int creat(const cahr *path, mode_t mode);

open할 때 create 할 수 있어서 안해도 되긴함

path : file 이름

mode : 접근 권한

return file descriptor / -1
#include<sys/types.h>
#include<unistd.h>

off_t lseek(int fd, off_t offset, int whence);

fd: file descriptor

offset : 특정위치에서 얼마나 떨어져 있냐

whence : SEEK_SET 시작

            SEEK_CUR 현재

            SEEK_END 끝

return new offset / -1

read write

#include<unistd.h>

ssize_t read(int fd, void *buf, size_t nbyte);

ssize_t write(int fd, const void *buf, size_t nbtye);

buffer에서 data를 읽고 쓴다. write할 때는 buffer에 data가 있어야 한다.

fd: file descriptor

buf : buf address

nbyte : read or write data양

return read or write byte 개수(nbyte) / -1

728x90