[Linux] File system call interfaces such as open/read/write/close and the comparison between fd and FILE

In Linux, everything is a file. Therefore, the file is a very important concept. Next, we will introduce its related file interface functions.

1. Description of file system call interfaces such as open/read/write/close


1. open function

write picture description here
Notes:
(1) Function description : used to open an existing file or create a normal file
(2) Parameter explanation :
pathname : The target file to be opened or created
flags : When opening a file, multiple parameter options can be passed in, Use one or more of the following to perform an "OR" operation to form flags;
parameters : O_RDONLY - read-only open, Q_WRONLY - write-only open, O_RDWR - read, write open; these three variables can only specify one
O_CREAT - if the file does not exists, create it. The mode (file permission flag) option needs to be used to indicate the access permission of the new file.
O_APPEND—append write
(3) Return value : successfully returns the descriptor of the newly opened file, if it fails, it returns -1

file permission flag
write picture description here

File permission flags can also be represented by weighted numbers. This set of numbers is called the umask variable, which is of type mode_t and is an unsigned octal number. The definition method of the umask variable is as follows:
write picture description here


2. read function

//函数原型
ssize_t read(int fd,void *buf,size_t count);

Notes:
(1) Function description : It reads n bytes in the buffer pointed to by buf from the open file described by fd.
(2) Parameter description
fd : file descriptor, used to point to the file structure of the file to be operated
buf : a piece of memory space
count : the number of bytes requested to be read, the read data is stored in the buffer buf, and the file The current read and write position is moved backward.
ssize_t : signed integer
(3) Return value : Returns the number of bytes read on success, -1 on error and set errno, if the end of the file has been reached before calling read, this read returns 0.

Note : The read operation starts at the current displacement of the file, which is incremented by the number of words actually read before returning successfully.


3. write function

//函数原型
ssize_t write(int fd,const void *buf,size_t count);

Notes:
(1) Function description : Write n bytes of the buffer pointed to by buf into the open file described by fd
(2) Parameter description :
fd : file pointer
buf : The written data is stored in the buffer buf , at the same time, the current read and write position of the file is moved backward by
count : the number of bytes requested to be written
(3) Return value : the number of bytes written is returned successfully, -1 is returned on error and errno is set when writing a regular file, the return of write The value is usually equal to the number of bytes requested to be written, count, but not necessarily to the terminal or network.


4. close function

write picture description here
Notes:
(1) Function description : close the specified file
(2) Parameter explanation : fd—file descriptor, which is used to point to the file structure of the file to be operated
(3) Return value : Return 0 if successful, -1 if error occurs;

Note : When a file is closed, all record locks added by the process to the file are released. When a process terminates, all its open files are automatically closed by the kernel.


Second, compare fd and FILE structure

1. File descriptor fd

Concept : A file descriptor is formally a non-negative integer. In fact, it is an index value that points to the record table of the process's open files maintained by the kernel for each process. When a program opens an existing file or creates a new file, the kernel returns a file descriptor to the process. In programming, some low-level programming often revolves around file descriptors.

By convention, the file descriptor for standard input is 0, standard output is 1, and standard error is 2.
The physical devices corresponding to 0, 1, and 2 are generally: keyboard, monitor, monitor.

View the current maximum number of open file descriptors:
write picture description here
Modify the maximum number of open file descriptors in the current user environment (temporary change):write picture description here

2. FILE structure

struct FILE
{
    char *_ptr;//文件输入的下一个位置
    int _cnt;//当前缓冲区的相对位置
    char *_base;//指基础位置(文件的起始位置)
    int _flag;//文件标志
    int _file;//文件的有效性验证
    int _charbuf;//检查缓冲区状况,如果缓冲区则不读取
    int _bufsiz;//文件的大小
    char *_tmpfname;//临时文件名
};

3. (FILE*) file pointer

The file pointer points to a data structure called a FILE structure in the process user area.
The FILE structure includes a buffer and a file descriptor. And a file descriptor is an index into the file descriptor table;
so in a sense a file pointer is a handle to a handle (on Windows, a file descriptor is called a file handle).

4. The difference between a file descriptor and a file pointer

(1) The file descriptor fd is just a non-negative integer, which is generated when open and acts as an index. The process finds the file pointer file pointed to by the fd through the file descriptor table in the PCB.
(2) The open function returns a file descriptor, the kernel will maintain a file descriptor table in each process space, and all open files will be referenced through the file descriptors in this table;
(3) fopen function What is returned is a file pointer (FILE*), the FILE structure contains file descriptors, fopen can be regarded as the encapsulation of open (fd direct operation system call), its advantage is that it has an I/O cache write picture description here
function fopen () is to return a pointer to the open file; if the operation fails, it returns a null pointer null.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324573380&siteId=291194637