Most file I/O requires only 5 functions: Open, read, write, Lseek, close
This chapter describes the I/O without buffering (read write calls a system call in the kernel)
File descriptor
For the kernel, all files are referenced by a file descriptor.
The file descriptor is a non-negative integer
When you open or create a new file, the kernel returns a file descriptor
The range of file descriptors is 0~open_max-1
Functions Open and Openat
Call the Open or Openat to open or create a file
int open (const char *path, int oflag, .../* mode_t mode */)
int Openat (int fd, const char *path, int oflag, .../* mode_t mode */
The last schedule parameter is used only when creating a new file.
The Oflag parameter can be used to illustrate several options for this function:
O_rdonly read-only Open
O_wronly only Write Open
O_RDWR Read/write Open
The file descriptor returned by open and Openat must be the smallest unused descriptor value
function creat
Call the creat function to create a new file
int creat (const char *path, mode_t mode)
The effect is equivalent to open (path, o_wronly | O_creat | O_trunc, Mode)
Creat opens the created file as a write-only method.
function close
Call this function to close an open file.
int close (int fd)
function Lseek
Each open file has a file offset. Used to measure the number of bytes computed at the beginning of a file.
Read, the write operation starts at the current offset and increases the read and write offset.
off_t lseek (int fd, off_t offset, int whence);
Whence parameters:
Seek_set a cheap offset byte from the beginning of the file
Seek_cur setting the current value from the file offset plus offset
Seek_end set offset to file length plus offset
Lseek returns a new offset after a successful call
function read
Read data from an open file
ssize_t Read (int fd, void *buf, size_t nbytes)
The call successfully returns the number of bytes read to the end of the file and returns 0.
Function write
Write data to an open file
ssize_t Write (int fd, void *buf, size_t nbytes)
The return value is usually the same as nbytes.
File sharing
The kernel uses 3 data structures to represent open files:
(1) Process table entry
1. File descriptors
2. Pointers to File table entries
(2) File Table entry
1. File status flags (such as read, write, non-blocking, etc.)
2. Current file offset
3. Pointers to V-node table entries
(3) V-node table entry
Each open file has a V-node structure.
Functions Pread and Pwrite
belongs to atomic operations.
Do not update the current file offset
function DUP and dup2
To copy an existing file descriptor
int dup (int fd)
int dup2 (int fd, int fd2)
A new file descriptor returned by the function shares the same file table entry as the parameter FD
Invoke DUP (FD) equivalent to Fcntl (FD, f_duped, 0)
Call Dup2 (FD,FD2) is equivalent to close (FD2); Fcntl (fd,f_duped, FD2);
function Sync, Fsync and Fdatasync
Writes data from the buffer to disk
function Fcntl
To change the properties of a file that has been opened
int fcntl (int fd, int cmd, .../* int arg */);
Parameter cmd:
Copy an existing descriptor F_DUPFD or f_dupfd_cloexec
Get/Set file descriptor F_getfd or F_SETFD
Get/Set file status flag F_GETFL or F_SETFL
Get/Set asynchronous I/O ownership f_getown or F_setown
Get/Set record lock F_getlk f_setlk f_setlkw
Advanced Programming File I/O for UNIX environments