One, file sharing
The kernel uses three data structures to represent open files, and the relationship between them determines the possible impact a process might have on another process in terms of file sharing.
1, each process in the process table has a record entry, the record entry contains an open File description table
2. The kernel maintains a file table for all open files
3. Each open file has a V-node structure, and the V node contains a pointer to the file type and functions that perform various operations on the file. Instead of using the V node in the ps:linux, the common I node structure is used.
File descriptor flags are used only for one descriptor of a process
The file status flag applies to all descriptors in any process that points to the given file table entry
Second, atomic operation
1. pread function and Pwrite function
#include <unistd.h>
ssize_t pread (int fd,//file descriptor to read data from
void *buf,//data buffer pointer, storing read-out data
size_t nbytes,//number of bytes read data
off_t offset//read from address = file start +offset
); Return value: The number of bytes read, returns 0 if the end of the file, or 1 if an error occurs
ssize_t pwrite (int fd,//file descriptor to write data to
const void *BUF,//data buffer pointer, storing data to be written to the file
size_t nbytes,//number of bytes written to data
off_t Offset//writes the start address to the address = File start +offset
); Return value: Returns the number of bytes written if successful, or 1 if an error occurs
Calling Pread is equivalent to calling Lseek and read sequentially, but when Pread is called, it cannot break its location and read operations and does not update the file pointer.
Calling Pwrite is equivalent to sequentially calling Lseek and write.
2, atomic operation refers to a multi-step operation, if the operation is performed atomically, then all steps are executed. Either step or not, it is not possible to perform only a subset of all the steps.
Iii. DUP and DUP2 functions
#include <unistd.h>
int dup (int fd); The new file descriptor returned must be the minimum value of the currently available file descriptor
int dup2 (int fd,int fd2); Specify the value of the new descriptor with the FD2 parameter
Two functions can be used to copy an existing file descriptor, the return value of two functions: If successful, a new file descriptor is returned, or 1 if an error occurs.
The use of these two functions: the return of the new file descriptor and the parameter FD share a file table entry, to share a file status flag (read, write, add, etc.) and the same current file offset. As shown in the following:
Iv. sync, Fsync, and Fdatasync functions
Although deferred write reduces disk read and write times, it reduces the speed of file content updates so that data written to the file is not written to disk for a period of time. In the event of a system failure, this delay may result in the loss of file update content. The sync, Fsync, and Fdatasync functions ensure that the actual file system on the disk is consistent with the memory in the buffer cache.
#include <unistd.h>
int fsync (int fd);
int fdatasync (int fd); The return value of these two functions: returns 0 if successful, or 1 if an error occurs
void sync (void);
Fsync: only works with a single file specified by the file descriptor FD, and waits for the write disk operation to end, and then returns
Fdatasync: Affects only the data portion of the file, and Fsync updates the properties of the file in addition to the data
Sync: Just put all the modified block buffers into the write queue and then return, it's not waiting for the actual write operation to end
Five, Fcntl function
#include <fcntl.h>
int fcntl (int fd,int cmd,int arg); Return value: If the success is dependent on CMD, if an error returns-1
There are 5 functions of the FCNTL function: 1. Copy an existing descriptor CMD=F_DUPFD
2. Get/Set File descriptor tag cmd=f_getfd or F_SETFD
3. Get/Set file status flag CMD=F_GETFL or F_SETFL
4. Get/Set asynchronous I/O ownership cmd=f_getown or F_setown
5. Get/Set record lock cmd=f_getlk, f_setlk or f_setlkw
Linux Learning Note 11--file I/O II