Linux system programming 13 system call IO-open,close

open / close


man 2 open
NAME
open, openat, creat - open and possibly create a file

SYNOPSIS
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

   int open(const char *pathname, int flags);
   int open(const char *pathname, int flags, mode_t mode);
   int creat(const char *pathname, mode_t mode);

RETURN VALUE
open() and creat() return the new file descriptor, or -1 if an error occurred (in which case, errno is set appropriately).
Return the file descriptor or -1, if it fails, it will return -1, because under the array There are no plurals in the mark (review the essence of the file descriptor in the previous section)

Parameter
flags
The argument flags must include one of the following access modes: O_RDONLY, O_WRONLY, or O_RDWR. These request opening the file read-only, write-only, or read/write, respectively.
Must include: O_RDONLY, O_WRONLY, O_RDWR where One is read-only, write-only, and read-write.

In addition, zero or more file creation flags and file status flags can be bitwise-or’d in flags. The file creation flags are O_CLOEXEC, O_CREAT, O_DIRECTORY, O_EXCL, O_NOCTTY, O_NOFOLLOW, O_TMPFILE, and O_TRUNC. The file status flags are all of the remaining flags listed below. The distinction between these two groups of flags is that the file status flags can be retrieved and (in some cases) modified; see fcntl(2) for details.

In addition, to contain zero or more create an options file and file status options for the form of bits or flags put in.

File creation options include:

O_CLOEXEC

O_CREAT :无则创建

O_DIRECTORY:
如果path 引用的不是目录,则出错。

O_EXCL:
必须打开一个新文件,如果指定了该文件创建为,并且打开的文件已经存在,则报错,该文件创建标志选项可以用来测试一个文件是否存在。

O_NOCTTY

O_NOFOLLOW:
如果文件名是符号链接文件的话,则打开失败。
O_TMPFILE

O_TRUNC:
如果文件存在,并且为只写或读写 方式成功打开,则将其长度截断为0

The file status options include:

O_APPEND
每次写的时候都追加到文件尾端

O_ASYNC
O_DIRECT
O_DSYNC

O_LARGEFILE:
如果打开的文件比较大,可以指定O_LARGEFILE

O_NOATIME:
ATIME是指文件最后读的时间,O_NOATIME 是指不修改文件最后读的时间

O_NONBLOCK or O_NDELAY:
非阻塞,非等待

O_PATH
O_SYNC

Standard IO and file IO mapping

r -> O_RDONLY
r+ -> O_RDWR
w -> O_WRONLY | O_CREAT | O_TRUNC , 只写 有则清空,无则创建
w+ -> O_RDWR | O_TRUNC | O_CREAT, 读写,有则清空,无则创建

cache VS buffer

Buffer: can be understood as a buffer for writing, writing is written to the buffer first, that is, buffer is the acceleration mechanism for writing.
Cache: it can be understood as a buffer for reading, and the read content is first read into the cache, that is, the cache is read Acceleration mechanism

open(2 parameters) VS open(3 parameters)

   int open(const char *pathname, int flags);
   VS
   int open(const char *pathname, int flags, mode_t mode);

If there is O_CREAT in the flags, then use the three-parameter open, if there is no O_CREAT in the flag, then use the two-parameter form. mode refers to the permission to create new files.
Still mode & ~umask


NAME
close - close a file descriptor

SYNOPSIS
#include <unistd.h>

   int close(int fd);

Guess you like

Origin blog.csdn.net/LinuxArmbiggod/article/details/105850767