3.2 File descriptors
- UNIX shells associate file descriptor 0 with the process' standard input, file descriptor 1 with standard output
, and file descriptor 2 with standard error. This is the convention used by UNIX shells and many applications, regardless of the kernel - ** In POSIX .1 applications, the magic numbers 0, 1, and 2 should be replaced by the symbolic constants STDIN_FILENO, STDOUT_FILENO, and STDERR_FILENO. These constants are defined in the header file <unistd.h>. **
- The range of the file descriptor is 0 ~ OPEN _ MAX (see Table 2-7). The upper limit used by early UNIX versions is 19 (allowing each process to open 20 files), and now many systems increase it to 63
3.3 open function
#include <sys/types.h>
#include <sys/stat.h>
#include <fnctl.h>
int open(const char *pathname, int oflag, ...);
3.4 creat function
This function is equivalent to:
open(pathname, O_WRONLY | O_CREAT | O_TRUNC, mode);
3.6 lseek function
- By default, when opening a file, unless the O_APPEND option is specified, the offset is set to 0.
- An open file can be explicitly located by calling the lseek function.
-
#include <sys/types.h> #include <unistd.h> off_t lseek(int filedes, off_t offset, int whence);
- If whence is SEEK_SET, set the offset of the file to offset bytes from the beginning of the file;
- If whence is SEEK_CUR, set the offset of the file to its current value plus offset, offset can be positive or negative;
- If whence is SEEK_END, set the offset of the file to the file length plus offset, offset can be positive or negative;
- If lseek is successfully executed, the new file displacement will be returned
3.7 read function
null
3.10 File Sharing
3.12 dup and dup2 functions
did not understand
3.13 fcntl function
#include <sys/types.h>
#include <uinistd.h>
#include <fcntl.h>
int fcntl(int filedes, int cmd, ...);
- Duplicate a sink village's descriptor (cmd=F_DUPFD)
- get/set file descriptor flags (cmd = F_GETFD or F_SETFD)
- Get/set file status flags (cmd = F_GETFL or F_SETFL)
- Get/set asynchronous I/O authority (cmd = F_GETOWN or F_SETOWN)
- Get/set record lock (cmd = F_GETLK, F_SETLK or F_SETLKW)
- O_ACCMODE is used to take out the lower two bits in the flag, which are used to describe the write read in the file status flag