1 Chapter 3 File Programming Seungjae Baek 13
1 Chapter 3. File Programming Seungjae Baek
디스크 물리적 구조 (1/3) 4 디스크 구성 요소 ü ü ü Head, ARM Platter, Spindle, Surface Track, Sector, Cylinder Seungjae Baek
디스크 물리적 구조 (2/3) 5 데이터 접근 방법 ü Sector addressing : head(surface), track(cylinder), sector ü ü Heads move to appropriate track : 탐색 시간 (Seek time) Wait for the sector to appear under the head : 회전 지연 시간 (rotational latency) Read/Write the sector: 데이터 전달 시간 (transmission time) ü Seek time과 Rotational Latency를 줄이는 것이 중요 ü 다양한 디스크 스케줄링 방법 존재, parallel access 이용 (RAID) Seungjae Baek
디스크 물리적 구조 (3/3) 6 디스크와 플래시 메모리 차이 (Optional) VS ü ü ü No mechanical part (fast, lightweight) Overwrite limitation (Erase before write) Read/Write vs. Erase granularity Endurance SLC, MLC Seungjae Baek
디스크 관리 시스템 프로그램 (2/7) 8 파일 시스템 (file system) ü 파일과 디스크 블록 연결 (inode, FAT) § File: stream of bytes, object with read/write interface ü 파일 속성 제어, 디렉터리 지원, … start offset size Reports. doc 10000 byte File system 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 . . Disk device driver Seungjae Baek 1, 5, 6 디스크 블록들이 아니라 13, 14, 15 디스크 블록들을 할당하면?
디스크 관리 시스템 프로그램 (4/7) 10 inode structure inode type (4 bit) u g s r w x i_inode_number i_mode i_nlink, i_dev i_uid, gid i_op, i_size i_atime, ctime, mtime 7 13 24 31 55 12 direct block …. 3 indirect block S_IFSOCK S_IFLNK S_IFREG S_IFBLK S_IFDIR S_IFCHR S_IFIFO 67 77 72 96 123 83 125 99 128 131 Seungjae Baek
디스크 관리 시스템 프로그램 (6/7) 12 시스템 호출 (system call) ü Support interfaces for file (also task) fd=open(“Reports. doc”, …) or close(fd) read(fd, buf, size) or write(fd, buf, size) System call Seungjae Baek
디스크 관리 시스템 프로그램 (7/7) 13 시스템 호출 (system call) ü open()을 수행하면 § fd (file descriptor)가 복귀됨 § 이후 read(), write(), close()는 fd 사용 § 커널 내부에서 fd는 inode와 연결된다 program … fd=open() ; . . . file_descriptor file structure (file table) offset inode Seungjae Baek
파일 인터페이스 (1/15) 14 example 1: 이미 존재하는 파일 읽기 /* file_test 1. c 파일 읽는 프로그램. 9월 10일 by choijm@dku. edu */ #include <stdio. h> #include <stdlib. h> #include <unistd. h> #include <fcntl. h> #include <errno. h> synopsis 참조 #define MAX_BUF 16 char fname[] = “alphabet. txt"; int main() { int fd, size; char buf[MAX_BUF]; } errro 발생시 그 원인을 알리는 변수 cf) 예외 처리는 매우 중요!! fd = open(fname, O_RDONLY); if (fd < 0) { printf(“Can’t open %s file with errno %dn”, fname, errno); exit(-1); } size = read(fd, buf, MAX_BUF); if (size < 0) { printf(“Can’t read from file %s, size = %dn”, fname, size); exit(-1); } else printf(“size of read data is %dn”, size); close(fd); Seungjae Baek
파일 인터페이스 (2/15) 15 Synopsis int open(const char *pathname, int flags, [mode_t mode]) ü pathname : 절대 경로 or 상대 경로 ü flags (참조 : /usr/include/asm/fcntl. h) § O_RDONLY, O_WRONLY, O_RDWR § O_CREAT, O_EXCL § O_TRUNC, O_APPEND § O_NONBLOCK, O_SYNC § … ü mode § O_CREAT 플래그가 있을 때 유의미 § file access mode (S_IRUSR, S_IWUSR, S_IXUSR, S_IRGRP, …, S_IROTH, …) ü return value § file descriptor if success § -1 if fail int read(int fd, char *buf, int size) // write도 동일 ü fd: file descriptor (return value of open()) ü buf: memory space for keeping data ü size: request size ü return value § read size Seungjae Baek
파일 인터페이스 (3/15) 16 example 1: 수행 결과 /usr/include/asm-generic/errno-base. h 참조 #define ENOENT 2 // No such file or directory Seungjae Baek
파일 인터페이스 (4/15) 17 example 1 확장: 읽은 내용 확인 /* 파일을 읽는 프로그램. 9월 10일 by choijm@dku. edu */ #include <stdio. h> /usr/include/unistd. h 참조 #include <stdlib. h> #include <unistd. h> #define STDIN_FILENO 0 // Standard input #include <fcntl. h> #define STDOUT_FILENO 1 // Standard output #include <errno. h> #define STDERR_FILENO 2 // Standard error #define MAX_BUF 16 char fname[] = “alphabet. txt"; int main() { int fd, read_size, write_size; char buf[MAX_BUF]; } fd = open(fname, O_RDONLY); if (fd < 0) { printf(“Can’t open %s file with errno %dn”, fname, errno); exit(-1); } read_size = read(fd, buf, MAX_BUF); // 자료 크기 제약 때문에 예외 처리 생략 (여러분이 작성할 때는 예외 처리 하시길) write_size = write(STDOUT_FILENO, buf, read_size); close(fd); Seungjae Baek
파일 인터페이스 (6/15) 19 example 2 : mycat 프로그램 작성 /* mycat 프로그램. 9월 10일 by choijm@dku. edu */ #include <stdio. h> #include <stdlib. h> #include <unistd. h> 명령 작성하는 일반적인 방법 #include <fcntl. h> #include <errno. h> #define MAX_BUF 64 int main(int argc, char *argv[]) { int fd, read_size, write_size; char buf[MAX_BUF]; } if (argc != 2) { printf("USAGE: %s file_namen", argv[0]); exit(-1); } fd = open(argv[1], O_RDONLY); if (fd < 0) { // open 예외 처리 } while (1) { read_size = read(fd, buf, MAX_BUF); if (read_size == 0) break; write_size = write(STDOUT_FILENO, buf, read_size); } close(fd); Seungjae Baek
파일 인터페이스 (7/15) 20 example 2 : 수행 결과 Seungjae Baek
파일 인터페이스 (8/15) 21 example 3 : 새로 생성하려면? #include <stdio. h> /* 생성, 쓰고 읽는 예제 */ #include <stdlib. h> #include <unistd. h> #include <fcntl. h> #include <errno. h> #define MAX_BUF 64 char fname[] = "newfile. txt"; char dummy_data[]="abcdefgn"; O_CREAT or creat() int main() { int fd, write_size, read_size; char buf[MAX_BUF]; 프로그램을 다시 수행하면? O_EXCL을 없애고 다시 수행하면? 파일 뒤에 추가 하려면 fd = open(fname, O_RDWR | O_CREAT | O_EXCL, 0664); if (fd < 0) { printf("Can't create %s file with errno %dn", fname, errno); exit(1); } write_size = write(fd, dummy_data, sizeof(dummy_data)); printf("write_size = %dn", write_size); close(fd); close, open을 하지 않으면? fd = open(fname, O_RDONLY); read_size = read(fd, buf, MAX_BUF); printf("read_size = %dn", read_size); write_size = write(STDOUT_FILENO, buf, read_size); close(fd); } Seungjae Baek
파일 인터페이스 (9/15) 22 example 3 : 수행 결과 Seungjae Baek
파일 인터페이스 (10/15) 23 example 4: 파일 내용 “abcdefg”에서 ‘d’부터 읽고 싶으면? /* lseek 예제 프로그램 */ #include <stdio. h> #include <stdlib. h> #include <unistd. h> #include <fcntl. h> #include <errno. h> #define MAX_BUF 64 char fname[] = "newfile_lseek. txt"; char dummy_data[]="abcdefgn"; synopsis 참조 int main() { int fd, write_size, read_size, new_offset; char buf[MAX_BUF]; fd = open(fname, O_RDWR | O_CREAT | O_EXCL, 0664); write_size = write(fd, dummy_data, sizeof(dummy_data)); printf("write_size = %dn", write_size); close(fd); fd = open(fname, O_RDONLY); new_offset = lseek(fd, 3, SEEK_SET); read_size = read(fd, buf, MAX_BUF); printf("read_size = %dn", read_size); write_size = write(STDOUT_FILENO, buf, read_size); close(fd); } Seungjae Baek
파일 인터페이스 (11/15) 24 Synopsis off_t lseek(int fd, off_t offset, int whence) ü ü ü fd : file descriptor offset : offset position whence (참조 : /usr/include/unistd. h) § SEEK_SET : New offset is set to offset bytes. § SEEK_CUR: New offset is set to its current location plus offset bytes. § SEEK_END: New offset is set to the size of the file plus offset bytes ü return value § new offset if success § -1 if fail 음수가 될 수도 있음 파일 크기보다 커질 수는? Seungjae Baek
파일 인터페이스 (12/15) 25 파일 인터페이스: 그 외 ü ü ü link(), unlink() dup(), dup 2() pipe() mknod() stat(), fstat() mkdir(), readdir(), rmdir() sync(), fsync() ioctl(), fcntl() creat() // open() with flag O_WRONLY | O_CREAT | O_TRUNC 와 동일 read(), write()에서 사용하는 buffer의 크기는? perror() 라이브러리 함수는 errno의 의미도 함께 출력 Seungjae Baek
파일 인터페이스 (13/15) 장치 파일 /* 장치 파일 접근 예제. choijm@dku. edu */ #include <unistd. h> #include <sys/types. h> #include <sys/stat. h> #include <fcntl. h> #define MAX_BUF 4 char fname[] = "test. txt"; char tmp_data[] = "abcdefghijklmn"; test. txt 26 /dev/pts/2 abcd ef… inode int main() { int fd, size; char buf[MAX_BUF]; fd = open(fname, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); write(fd, tmp_data, sizeof(tmp_data)); close(fd); fd = open(fname, O_RDONLY); lseek(fd, 5, SEEK_SET); size = read(fd, buf, MAX_BUF); close(fd); } 터미널 같은 장치도 파일 인터페이스로 접근된다. fd=open(“/dev/pts/2”, O_WRONLY); write(fd, buf, MAX_BUF); close(fd); Seungjae Baek
파일 인터페이스 (14/15) 27 Redirection (입출력 재지정) ü 동일한 fd로 다른 객체 지정 /* Redirection example 프로그램. 9월 16일 by choijm@dku. edu */ #include <stdio. h> #include <stdlib. h> #include <unistd. h> #include <fcntl. h> #include <errno. h> #define MAX_BUF 64 int main(int argc, char *argv[]) { int fd, fd 1, read_size, write_size; char buf[MAX_BUF]; if (argc != 2) { printf("USAGE: %s file_namen", argv[0]); exit(-1); } fd = open(argv[1], O_RDONLY); // for redirection. (eg. "mycat inputfile. txt > outputfile. txt“) close(STDOUT_FILENO); fd 1 = open("outputfile. txt", O_RDWR | O_CREAT, 0641); // redirection end } while (1) { read_size = read(fd, buf, MAX_BUF); if (read_size == 0) break; write_size = write(STDOUT_FILENO, buf, read_size); } close(fd); Seungjae Baek
수행 예: sample 30 Seungjae Baek
- Slides: 30