LinuxUNIX Programming APUE File IO IT What is

  • Slides: 40
Download presentation
Linux/UNIX Programming APUE (File I/O) 문양세 강원대학교 IT대학 컴퓨터과학전공

Linux/UNIX Programming APUE (File I/O) 문양세 강원대학교 IT대학 컴퓨터과학전공

What is a File? APUE (File I/O) A file is a contiguous sequence of

What is a File? APUE (File I/O) A file is a contiguous sequence of bytes. (UNIX에서 파일은 연속적인 바이트 시퀀스로 볼 수 있다. ) No format imposed by the operating system. (UNIX 운영체제는 파일에 특별한 Format을 부여치 않는다. ) Each byte is individually addressable in a disk file. (파일의 각 바이트는 고유의 주소를 가지고(매길 수) 있다. ) Page 3 UNIX System Programming by Yang-Sae Moon

File Descriptor (1/2) APUE (File I/O) open() returns an fd, an integer value. Used

File Descriptor (1/2) APUE (File I/O) open() returns an fd, an integer value. Used in subsequent I/O operations on that file. (Open된 파일의 fd는 이후 File I/O 연산에 사용된다. ) close(fd) closes that file described by fd. All of a process's open files are automatically closed when it terminates. (프로세스가 종료되면, 해당 프로세스가 Open한 파일은 자동적으로 Close된다. ) Page 4 UNIX System Programming by Yang-Sae Moon

File Descriptor (2/2) APUE (File I/O) file descriptor: 0 ~ N (N = 19?

File Descriptor (2/2) APUE (File I/O) file descriptor: 0 ~ N (N = 19? or more) // unistd. h Value 0 1 2 3. . 19 Meaning standard input standard output standard error fds for users Page 5 UNIX System Programming by Yang-Sae Moon

파일을 위한 Kernel 자료 구조 – skip process table entry open file table fd

파일을 위한 Kernel 자료 구조 – skip process table entry open file table fd status flags current file offset v-node ptr file descriptors fd 0: fd flags ptr fd 1: fd 2: fd 3: . . APUE (File I/O) v-node table v-node information i-node information current file size fd status flags current file offset v-node ptr v-node information i-node information current file size The meaning of each field will be explained in next slides. Page 6 UNIX System Programming by Yang-Sae Moon

Process Table Entry – skip APUE (File I/O) 프로세스 테이블 (Process Table) • 커널(kernel)

Process Table Entry – skip APUE (File I/O) 프로세스 테이블 (Process Table) • 커널(kernel) 자료구조 • 프로세스 목록 • 프로세스 테이블 항목 (Process Table Entry) • 파일 구분자(file descriptor)의 배열을 포함 Page 7 UNIX System Programming by Yang-Sae Moon

Open File Table – skip APUE (File I/O) 파일 테이블 (File Table) • 커널

Open File Table – skip APUE (File I/O) 파일 테이블 (File Table) • 커널 자료구조 • 열려진 모든 파일 목록 • 열려진 파일 테이블의 항목 파일 테이블 항목 (File Table Entry) • 파일 상태 플래그 (read, write, append, sync, nonblocking, …) open()시 주는 정보 • 파일의 현재 위치 (current file offset) • v-node에 대한 포인터 Page 8 UNIX System Programming by Yang-Sae Moon

파일 관련 System Calls APUE (File I/O) open() – 열기 creat() - 파일 생성

파일 관련 System Calls APUE (File I/O) open() – 열기 creat() - 파일 생성 close() – 닫기 read() – 읽기 write() – 쓰기 lseek() - 이동 Page 10 UNIX System Programming by Yang-Sae Moon

open() – 파일 열기 APUE (File I/O) #include <sys/types. h> #include <sys/stat. h> #include

open() – 파일 열기 APUE (File I/O) #include <sys/types. h> #include <sys/stat. h> #include <fcntl. h> int open (const char *pathname, int oflag, [ mode_t mode ]); 파일을 연다. 파일이 없으면 경우에 따라 새로 만들어 질 수도 있다. • pathname : 파일의 이름 • mode : 파일의 access permission 값으로, 생략가능 새로운 파일을 만드는 경우에만 사용됨 (creat() 함수 설명 참조) • 리턴 값 : 파일 식별자(file descriptor), 실패하면 -1 Page 11 UNIX System Programming by Yang-Sae Moon

예제: open. c APUE (File I/O) Page 14 UNIX System Programming by Yang-Sae Moon

예제: open. c APUE (File I/O) Page 14 UNIX System Programming by Yang-Sae Moon

creat() – 파일 생성 APUE (File I/O) #include <sys/types. h> #include <sys/stat. h> #include

creat() – 파일 생성 APUE (File I/O) #include <sys/types. h> #include <sys/stat. h> #include <fcntl. h> int creat ( const char *pathname, mode_t mode ); 새로운 파일을 생성한다. • pathname : 파일의 이름 • mode : 파일의 access permission 값 • 리턴 값 : 파일 식별자(file descriptor), 실패하면 -1 Page 15 UNIX System Programming by Yang-Sae Moon

creat()의 파라미터 APUE (File I/O) 다음 두 함수의 호출은 동일한 기능을 수행함 • fd

creat()의 파라미터 APUE (File I/O) 다음 두 함수의 호출은 동일한 기능을 수행함 • fd = creat ( pathname, mode ); • fd = open ( pathname, O_WRONLY | O_CREAT | O_TRUNC, mode); 두 번째 파라미터인 mode는 permission mode를 나타냄 • 0644 -rw-r--r- • 0755 -rwxr-xr-x • 0444 -r--r--r-- Page 16 UNIX System Programming by Yang-Sae Moon

close() – 파일 닫기 APUE (File I/O) #include <sys/types. h> #include <sys/stat. h> #include

close() – 파일 닫기 APUE (File I/O) #include <sys/types. h> #include <sys/stat. h> #include <fcntl. h> int close ( int filedesc ); 작업이 끝난 후 파일을 닫는다. 파일을 닫지 않더라도 프로세스가 종료하면, 모든 열려진 파일들은 자동 적으로 닫힌다. • filedesc : 닫고자 하는 파일의 파일 식별자 (File Descriptor) • 리턴 값 : 파일 식별자(file descriptor), 실패하면 -1 Page 17 UNIX System Programming by Yang-Sae Moon

예제: count. c (문자 수 세는 프로그램) (1/2) Page 20 APUE (File I/O) UNIX

예제: count. c (문자 수 세는 프로그램) (1/2) Page 20 APUE (File I/O) UNIX System Programming by Yang-Sae Moon

예제: count. c (문자 수 세는 프로그램) (2/2) Page 21 APUE (File I/O) UNIX

예제: count. c (문자 수 세는 프로그램) (2/2) Page 21 APUE (File I/O) UNIX System Programming by Yang-Sae Moon

예제: lseek 1. c (1/3) APUE (File I/O) Page 25 UNIX System Programming by

예제: lseek 1. c (1/3) APUE (File I/O) Page 25 UNIX System Programming by Yang-Sae Moon

예제: lseek 1. c (2/3) APUE (File I/O) lseek 1. c 의 출력 파일

예제: lseek 1. c (2/3) APUE (File I/O) lseek 1. c 의 출력 파일 “file. hole”의 내용 0 1 2 3 4 5 6 7 8 9 0 a b c d e f g h i j 10 20 30 40 A B C D E F G H I J UNIX 명령어인 od (octal dump)를 사용하여 확인할 수 있음 Page 26 UNIX System Programming by Yang-Sae Moon

예제: lseek 1. c (3/3) APUE (File I/O) Page 27 UNIX System Programming by

예제: lseek 1. c (3/3) APUE (File I/O) Page 27 UNIX System Programming by Yang-Sae Moon

fcntl() – 파일 제어 APUE (File I/O) #include <sys/types. h> #include <fcntl. h> #include

fcntl() – 파일 제어 APUE (File I/O) #include <sys/types. h> #include <fcntl. h> #include <unistd. h> int fcntl (int filedes, int cmd, … /* int arg */ ); 역할: 파일의 속성을 알아내거나 변경한다. cmd: F_GETFL, F_SETFL, F_GETFD, F_SETFD, F_DUPFD, … 리턴 값 • 성공인 경우: 파라미터인 cmd에 따라 다름 • 실패인 경우: -1 Page 28 UNIX System Programming by Yang-Sae Moon

fcntl()의 주요 사용법 APUE (File I/O) F_GETFL : file table 의 file status flags를

fcntl()의 주요 사용법 APUE (File I/O) F_GETFL : file table 의 file status flags를 리턴 F_SETFL : file status flags 를 설정 File status flag Description O_RDONLY O_WRONLY O_RDWR open for reading only open for writing only open for reading and writing O_APPEND O_NONBLOCK O_SYNC O_ASYNC append on each write nonblocking mode wait for writes to complete asynchoronouse I/O Page 29 UNIX System Programming by Yang-Sae Moon

예제: fileflags. c (1/2) APUE (File I/O) Page 30 UNIX System Programming by Yang-Sae

예제: fileflags. c (1/2) APUE (File I/O) Page 30 UNIX System Programming by Yang-Sae Moon

예제: fileflags. c (2/2) APUE (File I/O) Page 31 UNIX System Programming by Yang-Sae

예제: fileflags. c (2/2) APUE (File I/O) Page 31 UNIX System Programming by Yang-Sae Moon

예제: write 2. c (1/3) APUE (File I/O) Page 33 UNIX System Programming by

예제: write 2. c (1/3) APUE (File I/O) Page 33 UNIX System Programming by Yang-Sae Moon

예제: write 2. c (2/3) APUE (File I/O) Page 34 UNIX System Programming by

예제: write 2. c (2/3) APUE (File I/O) Page 34 UNIX System Programming by Yang-Sae Moon

예제: write 2. c (3/3) APUE (File I/O) Page 35 UNIX System Programming by

예제: write 2. c (3/3) APUE (File I/O) Page 35 UNIX System Programming by Yang-Sae Moon

실험 결과 APUE (File I/O) BUFFSIZE User CPU (seconds) System CPU (seconds) Clock time

실험 결과 APUE (File I/O) BUFFSIZE User CPU (seconds) System CPU (seconds) Clock time (seconds) #loops asynchrohous 8, 192 0. 0 1. 1 3. 6 12, 800 synchrohous 8, 192 0. 0 3. 4 180. 3 12, 800 상기 결과에서 볼 수 있듯이 Synchronous Write가 훨씬 더 느림 DBMS(Database Management System)처럼 실제로 데이터가 디스크에 기 록되었는지 여부가 중요한 경우에 사용됨 Page 36 UNIX System Programming by Yang-Sae Moon

write_num. c / read_num. c (1/3) Page 38 APUE (File I/O) UNIX System Programming

write_num. c / read_num. c (1/3) Page 38 APUE (File I/O) UNIX System Programming by Yang-Sae Moon

write_num. c / read_num. c (2/3) Page 39 APUE (File I/O) UNIX System Programming

write_num. c / read_num. c (2/3) Page 39 APUE (File I/O) UNIX System Programming by Yang-Sae Moon

write_num. c / read_num. c (3/3) Page 40 APUE (File I/O) UNIX System Programming

write_num. c / read_num. c (3/3) Page 40 APUE (File I/O) UNIX System Programming by Yang-Sae Moon