Input : I/O system에서 main memory로
Output: main memory 에서 I/O system으로
I/O는 프로세스의 생성과 실행과정에서 서로 다른 프로세스들이 어떻게 파일을 공유하는가에 대해 핵심 역할을 수행한다.
리눅스에서 파일은 연속된 m개의 바이트다.
Text file : ASCII 문자나 유니코드로 이루어진 파일
그 외 모든 파일 : 이진파일
파일 열기:
해당 파일을 열겟다고 커널에 요청하는 방법으로 I/O 디바이스에 접근.
커널은 식별자(descriptor) 라고 하는 unsigned int 리턴
Each process created by a Linux shell begins life with three open files associated with a terminal :
식별자 0 : 표준 입력 : STDIN_FILENO
식별자 1 : 표준 출력 : STDOUT_FILENO
식별자 2 : 표준 에러 : STDERR_FILENO
현재 파일 위치 변경:
커널은 파일을 열때마다 파일 위치 k를 관리. 처음에는 0.
seek 연산을 통해서 현재 파일 위치를 명시적으로 설정
파일 읽기와 쓰기
k에서 시작해서 n >0 바이트를 메모리로 복사하고, k를 n증가시킨다
파일 닫기
커널은 열려있는 모든 파일들을 닫고, 이들의 메모리 자원 반환
int open(char *filename, int flags, mode_t mode);
// Returns: new file descriptor if OK, −1 on error
/*
flags argument
O_RDONLY. Reading only
O_WRONLY. Writing only
O_RDWR. Reading and writing
additional flags argument
O_CREAT. If the file doesn’t exist, then create a truncated (empty) version.
O_TRUNC. If the file already exists, then truncate it.
O_APPEND. Before each write operation, set the file position to the end of
the file.
*/
fd = Open("foo.txt", O_WRONLY|O_APPEND, 0);
int close(int fd); //Returns: 0 if OK, −1 on error
ssize_t read(int fd, void *buf, size_t n);
// Returns: number of bytes read if OK, 0 on EOF, −1 on error
ssize_t write(int fd, const void *buf, size_t n);
// Returns: number of bytes written if OK, −1 on error
read : 식별자 fd의 현재 파일 위치에서 최대 n byte를 메모리 위치 buf로 복사
write: 메모리 위치 buf에서식별자 fd의 현재 파일 위치로 최대 n byte 복사