Chapter 2 File I/O
1. the kernel maintains a per-process list of open files, called the file table
i. index: nonnegative int "file descriptors"
a. file descriptors is int type
b. by default, the maximum is 1024, but it can be configured as high as 1,048,576 (2^20)
c. by convention:
* stdin: fd = 0 (STDIN_FILENO)
* stdout: fd = 1 (STDOUT_FILENO)
* stderr: fd = 2 (STDERR_FILENO)
ii. a pointer to an in-memory copy of the file's backing inode(?) and associated metadata
2. by default, a child process receives a copy of its parent's file table (but the change will not effect the parent)
3. open files
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open (const char* filename, int flags[, mode_t mode]);
return: file descriptor if success, -1 if error
flags: O_RDONLY, O_WRONLY, or O_RDWR, etc. // the access right of the open file