/ 유닉스시스템개요 / 파일 / 프로세스
01 File Descriptor file file descriptor file type unix 에서의파일은단지바이트들의나열임 operating system 은파일에어떤포맷도부과하지않음 파일의내용은바이트단위로주소를줄수있음 file descriptor 는 0 이나양수임 file 은 open 이나 creat 로 file descriptor 가새로할당되고, read, write 할때이를이용함 user 가생성하는첫번째파일의 file descriptor 는 3 임 regular file : 0 이상의 data block 을가진일반적인파일 directory : 파일과파일이름을 mapping 시켜주는파일 special file : physical device 를 file system 에 mapping link : 파일을다른이름으로연결 symbolic link : 다른파일을가리키는파일 named pipe(fifo) : process 간의통신을위한파일
01 File Descriptor file descriptor all open files are referred to by file descriptors how to obtain file descriptor return value of open( ), creat( ) when we want to read or write a file, we identify the file with the file descriptor file descriptor is the index of user file descriptor table STDIN_FILENO(0), STDOUT_FILENO(1), STDERR_FILENO(2) (<unistd.h>)
01 File Descriptor file descriptor (cont d) standard input, output, error STDIN_FILENO ( == 0) STDOUT_FILENO ( == 1) STDERR_FILENO ( == 2) defined in <unistd.h> opened by the shell not by the kernel
1. open( ) synopsis #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int open(const char *pathname, int flags); int open(const char *pathname, int flags, mode_t mode);
1. open( ) description attempts to open a file and return a file descriptor flags (defined in <fcntl.h>) O_RDONLY, O_WRONLY or O_RDWR access mode O_CREAT If the file does not exist it will be created O_EXCL When used with O_CREAT, if the file already exists it is an error and the open will fail
1. open( ) int fd; fd = open( /etc/passwd, O_RDONLY); fd = open( /etc/passwd, O_RDWR); fd = open( ap, O_RDWR O_APPEND); fd = open( ap, O_RDWR O_CREAT O_EXCL, 0644);
1. open( ) synchronization #include <stdio.h> #include <unistd.h> #include <fcntl.h> #include <errno.h> #define LOCKFILE "lockfile" #define DELAY 10000000 void delay(void) { int i; for (i = 0; i < DELAY; i++); }
1. open( ) int main(void) { int fd, i; while ((fd = open(lockfile, O_WRONLY O_CREAT O_EXCL, 0644)) < 0) { if (errno!= EEXIST) { perror("open"); exit(1); } } for (i = 'a'; i <= 'z'; i++) { putchar(i); fflush(stdout); delay(); } close(fd); unlink(lockfile); return 0; } [comeng:/export/home/prof/yshin/lec/3 5 ] a.out abcdefghijklmnopqrstuvwxyz
2. lseek( ) synopsis #include <sys/types.h> #include <unistd.h> off_t lseek(int fildes, off_t offset, int whence); description repositions the offset of the file descriptor fildes to the argument offset
2. lseek( ) whence SEEK_SET access mode SEEK_CUR The offset is set to its current location plus offset bytes. SEEK_END The offset is set to the size of the file plus offset bytes.
2. lseek( ) hole allows the file offset to be set beyond the end of the existing end-of-file of the file If data is later written at this point, subsequent reads of the data in the gap return bytes of zeros return value success : the resulting offset location as measured in bytes from the beginning of the file error : -1
2. lseek( ) off_t curpos; curpos = lseek(fd, 0, SEEK_CUR); lseek(fd, 0, SEEK_SET); lseek(fd, 0, SEEK_END); lseek(fd, -10, SEEK_CUR); lseek(fd, 100, SEEK_END);
2. lseek( ) #include <unistd.h> #include <fcntl.h> int main(void) { int fd; fd = creat("holefile", 0644); write(fd, "hello", 5); lseek(fd, 10, SEEK_CUR); write(fd, "world", 5); lseek(fd, 8192, SEEK_SET); write(fd, "bye", 3); } close(fd); return 0;
2. lseek( ) result $ ls -l holefile -rw-r--r-- 1 kim stud 8195 Jul 18 21:37 holefile $ od -c holefile 0000000 h e l l o \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 w 0000020 o r l d \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 0000040 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 * 0020000 b y e 0020003 $ du holefile 8 holefile
3. read( ) synopsis #include <unistd.h> ssize_t read(int fd, void *buf, size_t count); description attempts to read up to count bytes from file descriptor fd into the buffer starting at buf
3. read( ) If count is zero, read() returns zero and has no other results return value On success, the number of bytes read zero indicates end of file On error, -1 is returned
4. write( ) synopsis #include <unistd.h> ssize_t write(int fd, const void *buf, size_t count); description writes up to count bytes to the file referenced by the file descriptor fd from the buffer starting at buf
4. write( ) return value the number of bytes written zero indicates nothing was written On error, -1
5. read() & write() #include <unistd.h> #define BUFFSIZE 8192 int main(void) { int n; char buf[buffsize]; while ((n=read(stdin_fileno,buf,buffsize))>0) if (write(stdout_fileno,buf,n)!=n) printf("write error\n"); } if (n<0) exit(0); printf("read error\n");