现在的位置: 首页 > 综合 > 正文

open(翻译 man 3)

2014年09月05日 ⁄ 综合 ⁄ 共 21429字 ⁄ 字号 评论关闭
OPEN(2)                       Linux Programmer's Manual                       OPEN(2)

NAME

       open, creat - open and possibly create a file or device

//打开或者可能的话创建一个文件或者设备

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); 

DESCRIPTION

       Given a pathname for a file, open() returns a file descriptor, a small,
       nonnegative integer for use in subsequent system calls (read(2), write(2),
       lseek(2), fcntl(2), etc.).  The file descriptor returned by a successful call
       will be the lowest-numbered file descriptor not currently open for the
       process.

//对文件指定一个路径,open()为随后的系统调用(read(2),write(2),lseek(2),fcntl(2)deng )返回一个小的非负的整数作为fd.//成功返回的fd为当前进程未打开的最小的fd值

       By default, the new file descriptor is set to remain open across an execve(2)
       (i.e., the FD_CLOEXEC file descriptor flag described in fcntl(2) is initially
       disabled; the Linux-specific O_CLOEXEC flag, described below, can be used to
       change this default).  The file offset is set to the beginning of the file
       (see lseek(2)).

//对文件指定一个路径,open()为随后的系统调用(read(2),write(2),lseek(2),fcntl(2)deng )返回一个小的非负的整数作为fd.//成功返回的fd为当前进程未打开的最小的fd值

       A call to open() creates a new open file description, an entry in the system-
       wide table of open files.  This entry records the file offset and the file
       status flags (modifiable via the fcntl(2) F_SETFL operation).  A file
       descriptor is a reference to one of these entries; this reference is
       unaffected if pathname is subsequently removed or modified to refer to a
       different file.  The new open file description is initially not shared with
       any other process, but sharing may arise via fork(2).

//对open()的调用创建一个新的打开的fd,为打开文件的系统宽度列表的入口.这个入口记录着文件偏移和//文件的状态位(可以通过fcntl(2)的 F_SETFL 操作修改).一个fd是以下几个入口的引用://如果pathname被随后的涉及对另一个文件的移除或者修改的话,这个fd的引用是不受影响的.//新打开的fd最初是不被别的进程共享的,可以通过fork(2)进行共享

       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.

//参数标志位必须至少包含以下的一种访问模式:O_RDONLY, O_WRONLY, 或 O_RDWR.//这些要求分别将文件打开为:只读,只写,读写.

       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_CREAT, O_EXCL, O_NOCTTY,
       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 using fcntl(2).
       The full list of file creation flags and file status flags is as follows:

//此外,0或者更多的文件创建标志位和文件状态标志位可以被按位与在标志位中.//文件创建标志位为:O_CREAT, O_EXCL, O_NOCTTY, 和O_TRUNC.//文件状态标志位都列在了下面.这2部分标志位的不同之处在于文件状态标志位可以被恢复,//(在某些情况下)可以使用fcntl(2)来修改.完整的文件创建标志位和文件状态标志位如下:

       O_APPEND
              The file is opened in append mode.  Before each write(2), the file
              offset is positioned at the end of the file, as if with lseek(2).
              O_APPEND may lead to corrupted files on NFS file systems if more than
              one process appends data to a file at once.  This is because NFS does
              not support appending to a file, so the client kernel has to simulate
              it, which can't be done without a race condition.

//文件以追加模式打开.在每次使用write(2)之前,文件的偏移都被定位在了文件的末尾,就好像是lseek(2)一样.//如果多于一个进程同时对一个文件添加数据的话,在NFS文件系统中O_APPEND 可能导致损坏.//这是因为NFS并不支持对文件进行追加,所以客户端内核不得不模拟这种操作,但在没有竞争状态下是无法完成的.

       O_ASYNC
              Enable signal-driven I/O: generate a signal (SIGIO by default, but this
              can be changed via fcntl(2)) when input or output becomes possible on
              this file descriptor.  This feature is only available for terminals,
              pseudoterminals, sockets, and (since Linux 2.6) pipes and FIFOs.  See
              fcntl(2) for further details.

//使能信号驱动的IO:在fd上当读或写变为可能的时候产生一个信号(默认是SIGIO,但是可以通过fcntl(2)来改变)//这个特性仅仅在终端,伪终端,socket,(在Linux 2.6 之后)管道和FIFO上有效.

       O_CLOEXEC (Since Linux 2.6.23)
              Enable the close-on-exec flag for the new file descriptor.  Specifying
              this flag permits a program to avoid additional fcntl(2) F_SETFD
              operations to set the FD_CLOEXEC flag.  Additionally, use of this flag
              is essential in some multithreaded programs since using a separate
              fcntl(2) F_SETFD operation to set the FD_CLOEXEC flag does not suffice
              to avoid race conditions where one thread opens a file descriptor at
              the same time as another thread does a fork(2) plus execve(2).

//对新的fd使能打开后关闭标志位.指定这个标志位允许程序避免额外的fcntl(2)的 F_SETFD 操作来设置FD_CLOEXEC标识//另外,在一些多线程程序中使用这个标志位是必要的,直到使用独立的fcntl(2)的F_SETFD 操作来设置FD_CLOEXEC标识//也不能在一个线程打开文件而同时另一个线程做fork(2)甚至execve(2)操作的时候避免竞争条件

       O_CREAT
              If the file does not exist it will be created.  The owner (user ID) of
              the file is set to the effective user ID of the process.  The group
              ownership (group ID) is set either to the effective group ID of the
              process or to the group ID of the parent directory (depending on file
              system type and mount options, and the mode of the parent directory,
              see the mount options bsdgroups and sysvgroups described in mount(8)).

//如果文件不存在将会被创建.文件的拥有者(用户ID)设置为进程有效的用户ID.//用户组ID 要么设置这个进程的有效组ID,要么父目录的有效组ID(取决于文件系统类型和挂在选项,//父目录的模式.在mount(8)中查看优选BSD 和 System V 的描述).

              mode specifies the permissions to use in case a new file is created.
              This argument must be supplied when O_CREAT is specified in flags; if
              O_CREAT is not specified, then mode is ignored.  The effective
              permissions are modified by the process's umask in the usual way: The
              permissions of the created file are (mode & ~umask).  Note that this
              mode only applies to future accesses of the newly created file; the
              open() call that creates a read-only file may well return a read/write
              file descriptor.

//万一新文件创建的话模式指定了使用的权限.在flags中指定O_CREAT的话这个参数必须被提供;//如果没有指定O_CREAT,那么mode就会被忽略.在一般情况下,有效的权限被进程的umask修改://创建文件的权限为(mode & ~umask).注意:这种模式仅仅适用于新创建文件的未来访问.//open()的调用可以创建一个只读的文件也可以返回一个读写的fd.

              The following symbolic constants are provided for mode:
//为mode提供下面的符号常量
         S_IRWXU  00700 user (file owner) has read, write and execute permission
//用户(文件的拥有者)有读,写,执行的权限
         S_IRUSR  00400 user has read permission
//用户有读权限
         S_IWUSR  00200 user has write permission
//用户有写权限

         S_IXUSR  00100 user has execute permission
//用户有执行权限
         S_IRWXG  00070 group has read, write and execute permission
//组用户有读,写,执行权限
         S_IRGRP  00040 group has read permission
              S_IWGRP  00020 group has write permission
              S_IXGRP  00010 group has execute permission
              S_IRWXO  00007 others have read, write and execute permission
//其他用户有读,写,执行权限
         S_IROTH  00004 others have read permission
              S_IWOTH  00002 others have write permission
              S_IXOTH  00001 others have execute permission
       O_DIRECT (Since Linux 2.4.10)
              Try to minimize cache effects of the I/O to and from this file.  In
              general this will degrade performance, but it is useful in special
              situations, such as when applications do their own caching.  File I/O
              is done directly to/from user space buffers.  The O_DIRECT flag on its
              own makes at an effort to transfer data synchronously, but does not
              give the guarantees of the O_SYNC that data and necessary metadata are
              transferred.  To guarantee synchronous I/O the O_SYNC must be used in
              addition to O_DIRECT.  See NOTES below for further discussion.

//试图最小化文件和IO之间的缓存效果.一般而言,这会降低性能,但在特殊情况下是有用的,如//当程序操作它们的共享缓存的时候.文件的IO直接从用户空间的缓存中进行操作.//在自身的O_DIRECT标志位努力同步传输数据,但是并不像O_SYNC一样保证数据和必要的元数据发送.//为了保证同步的IO,在使用O_DIRECT的时候必须使用O_SYNC.进一步的讨论参见下面的Note.

              A semantically similar (but deprecated) interface for block devices is
              described in raw(8).
//对块设备的接口,语义的相似(但是反对,弃用)使用raw(8)来描述.
       O_DIRECTORY
              If pathname is not a directory, cause the open to fail.  This flag is
              Linux-specific, and was added in kernel version 2.1.126, to avoid
              denial-of-service problems if opendir(3) is called on a FIFO or tape
              device, but should not be used outside of the implementation of
              opendir(3).

//如果pathname不是目录,就会导致打开失败.这个标志位是Linux专用的,在Linux 2.1.126之后被加进来//以解决如果在一个FIFO或tape类型的设备上调用opendir(3)的服务器拒绝问题.但是不要在opendir(3)//的程序之外使用

       O_EXCL Ensure that this call creates the file: if this flag is specified in
              conjunction with O_CREAT, and pathname already exists, then open() will
              fail.  The behavior of O_EXCL is undefined if O_CREAT is not specified.

//确认调用创建的文件:如果这个标志位和O_CREAT联合使用,而且pathname已经存在, open()会失败.//如果O_CREAT没有指定的话O_EXCL的行为是未定义的.

              When these two flags are specified, symbolic links are not followed: if
              pathname is a symbolic link, then open() fails regardless of where the
              symbolic link points to.

//如果2个标志位都指定的话,符号链接就不允许:如果pathname是一个符号链接,不管符号链接//指向哪里open()都会失败.

              On NFS, O_EXCL is only supported when using NFSv3 or later on kernel
              2.6 or later.  In NFS environments where O_EXCL support is not
              provided, programs that rely on it for performing locking tasks will
              contain a race condition.  Portable programs that want to perform
              atomic file locking using a lockfile, and need to avoid reliance on NFS
              support for O_EXCL, can create a unique file on the same file system
              (e.g., incorporating hostname and PID), and use link(2) to make a link
              to the lockfile.  If link(2) returns 0, the lock is successful.
              Otherwise, use stat(2) on the unique file to check if its link count
              has increased to 2, in which case the lock is also successful.

//在NFS上,O_EXCL仅仅被NFS v3 即以上的版本在Linux kernel 2.6及以上支持.在NFS环境中,//在不提供O_EXCL支持的地方,依靠于此的程序会表现为锁定任务将会包含一个竞争条件.//轻量的程序想使用锁定文件来实现原子文件锁定,就需要解决在NFS上的O_EXCL支持问题,//可以在相同的文件系统中创建一个唯一的文件(例如,一体化的主机名和PID),然后使用link(2)来//使一个链接连接到锁定文件上.如果link(2)返回0,锁定就是成功的.否则,使用stat(2)在唯一的//文件上来检查文件的链接数是否增加到了2,这种情况下锁定也是成功的.

       O_LARGEFILE
              (LFS) Allow files whose sizes cannot be represented in an off_t (but
              can be represented in an off64_t) to be opened.  The
              _LARGEFILE64_SOURCE macro must be defined (before including any header
              files) in order to obtain this definition.  Setting the
              _FILE_OFFSET_BITS feature test macro to 64 (rather than using
              O_LARGEFILE) is the preferred method of obtaining method of accessing
              large files on 32-bit systems (see feature_test_macros(7)).

//允许文件的大小不能用off_t(但是可以用off64_t表示)表示的文件被打开._LARGEFILE64_SOURCE宏//必须定义(在包含任何头文件之前)以获得这些定义.设置_FILE_OFFSET_BITS 测试宏的值为64(而//不是使用O_LARGEFILE)来在32位的系统上或者访问大文件的方法.

       O_NOATIME (Since Linux 2.6.8)
              Do not update the file last access time (st_atime in the inode) when
              the file is read(2).  This flag is intended for use by indexing or
              backup programs, where its use can significantly reduce the amount of
              disk activity.  This flag may not be effective on all file systems.
              One example is NFS, where the server maintains the access time.
//当文件被读的时候不要更新文件的最后访问时间(inode的st_atime元素).这个标志位是打算给索引或者
//备份程序的时候使用的,它的使用会显著的减少磁盘的操作数目.这个标志位不是在所有的文件系统上都有效.
//一个例子就是NFS,当服务器保持访问时间的时候.
       O_NOCTTY
              If pathname refers to a terminal device -- see tty(4) -- it will not
              become the process's controlling terminal even if the process does not
              have one.
//当pathname 是一个终端设备的引用时(参见(tty(4)),终端不会变为进程的控制终端甚至进程没有终端(写的蛋疼,感觉不对)
       O_NOFOLLOW
              If pathname is a symbolic link, then the open fails.  This is a FreeBSD
              extension, which was added to Linux in version 2.1.126.  Symbolic links
              in earlier components of the pathname will still be followed.
//当pathname 是一个符号链接,打开就会失败.这是一个FreeBSD的扩展,在Linux 2.1.126之后加了进来.
//符号链接在早期的盘符组成部分中会被遵守.
       O_NONBLOCK or O_NDELAY
              When possible, the file is opened in nonblocking mode.  Neither the
              open() nor any subsequent operations on the file descriptor which is
              returned will cause the calling process to wait.  For the handling of
              FIFOs (named pipes), see also fifo(7).  For a discussion of the effect
              of O_NONBLOCK in conjunction with mandatory file locks and with file
              leases, see fcntl(2).
//当可能的时候,文件被打开为非阻塞模式.在fd上的不管是open()还是随后的操作都不会造成调用线程等待.
//对FIFO(命名管道)的处理,参见fifo(7).对O_NONBLOCK 强制文件锁和文件租期的讨论参见fcntl(2)
       O_SYNC The file is opened for synchronous I/O.  Any write(2)s on the resulting
              file descriptor will block the calling process until the data has been
              physically written to the underlying hardware.  But see NOTES below.
//文件为了同步IO而打开,任何在设置的fd上的write(2)都将阻塞调用线程直到数据被物理的写到下面的硬件中.但是参见下面的NOTES
       O_TRUNC
              If the file already exists and is a regular file and the open mode
              allows writing (i.e., is O_RDWR or O_WRONLY) it will be truncated to
              length 0.  If the file is a FIFO or terminal device file, the O_TRUNC
              flag is ignored.  Otherwise the effect of O_TRUNC is unspecified.

//如果文件已经存在并且是常规的文件同时打开模式允许写(如O_RDWR 或 O_WRONLY),那么文件将会截断长度为0.//如果文件是FIFO或者终端,O_TRUNC 会被忽略.否则O_TRUNC 的效果是未定义的.


       Some of these optional flags can be altered using fcntl(2) after the file has
       been opened.

       creat() is equivalent to open() with flags equal to O_CREAT|O_WRONLY|O_TRUNC.

//在文件打开后一些可选的标识位可以通过fcntl(2)来改变
//creat() 和 open()是相等的当使用O_CREAT|O_WRONLY|O_TRUNC标识位的时候

RETURN VALUE

       open() and creat() return the new file descriptor, or -1 if an error occurred
       (in which case, errno is set appropriately).

//成功的话open() 和 creat() 都会返回fd
//失败的话返回-1(errno也会设置)

ERRORS

       EACCES The requested access to the file is not allowed, or search permission
              is denied for one of the directories in the path prefix of pathname, or
              the file did not exist yet and write access to the parent directory is
              not allowed.  (See also path_resolution(7).)
//对文件的访问是不允许的,或者是在路径名的前缀里的目录是不允许搜索的,或者是文件现在不存在,或者是
//对父目录的访问是不允许的.
       EEXIST pathname already exists and O_CREAT and O_EXCL were used.
//pathname 已经存在并且O_CREATO_EXCL 使用的时候
       EFAULT pathname points outside your accessible address space.
//pathname 指向你能够访问的地址空间之外
       EFBIG  See EOVERFLOW.
//参见EOVERFLOW
       EINTR  While blocked waiting to complete an open of a slow device (e.g., a
              FIFO; see fifo(7)), the call was interrupted by a signal handler; see
              signal(7).
//对一个低速设备对其进行阻塞直到写入完成(如FIFO,参见fifo()),调用就会被信号处理程序中断,参见signal(7)
       EISDIR pathname refers to a directory and the access requested involved
              writing (that is, O_WRONLY or O_RDWR is set).
//pathname 是对目录的引用同时访问需要复杂的写(也就是说,O_WRONLYO_RDWR 设置了)
       ELOOP  Too many symbolic links were encountered in resolving pathname, or
              O_NOFOLLOW was specified but pathname was a symbolic link.
//在解析pathname的时候有太多的符号链接相遇,或者是O_NOFOLLOW 指定了但是pathname 是符号链接.
       EMFILE The process already has the maximum number of files open.
//进程已经达到了文件打开的最大数目
       ENAMETOOLONG
              pathname was too long.
//pathname 太长了
       ENFILE The system limit on the total number of open files has been reached.
//已经达到了系统打开文件的最大值
       ENODEV pathname refers to a device special file and no corresponding device
              exists.  (This is a Linux kernel bug; in this situation ENXIO must be
              returned.)
//pathname 引用的是设备特殊文件但是没有对应的设备存在.(这是Linux 内核的bug;在这种情况下必须返回ENXIO)
       ENOENT O_CREAT is not set and the named file does not exist.  Or, a directory
              component in pathname does not exist or is a dangling symbolic link.
//没有设置O_CREAT 而且命名的文件不存在.或者是在pathname 中的目录名称不存在或为悬浮的符号链接(意思是符号链接没有指向,为无效的)

       ENOMEM Insufficient kernel memory was available.
//kernel没有足够的内存
       ENOSPC pathname was to be created but the device containing pathname has no
              room for the new file.
//包含pathname 的设备上没有要创建的pathname 的足够的空间
       ENOTDIR
              A component used as a directory in pathname is not, in fact, a
              directory, or O_DIRECTORY was specified and pathname was not a
              directory.
//在pathname 中作为目录使用的元素而实际上并不是目录,或者指定了O_DIRECTORYpathname 不是目录.
       ENXIO  O_NONBLOCK | O_WRONLY is set, the named file is a FIFO and no process
              has the file open for reading.  Or, the file is a device special file
              and no corresponding device exists.
//设置了O_NONBLOCK | O_WRONLY ,命名文件是FIFO同时没有进程来对文件进行读取,或者,文件是设备指定的文件而没有相应的设备存在

       EOVERFLOW
              pathname refers to a regular file that is too large to be opened.  The
              usual scenario here is that an application compiled on a 32-bit
              platform without -D_FILE_OFFSET_BITS=64 tried to open a file whose size
              exceeds (2<<31)-1 bits; see also O_LARGEFILE above.  This is the error
              specified by POSIX.1-2001; in kernels before 2.6.24, Linux gave the
              error EFBIG for this case.
//pathname 指定一个常规的文件但是对打开来说太大了.这里一般的情节是程序在32bit平台编译没有使用-D_FILE_OFFSET_BITS=64 
//而试图打开一个文件大小超过(2<<31)-1 bits 的文件.参见下面的O_LARGEFILE .这是由POSIX.1-2001指定的错误.
//在2.6.24之后,Linux在这种情况下给出错误号EFBIG 
       EPERM  The O_NOATIME flag was specified, but the effective user ID of the
              caller did not match the owner of the file and the caller was not
              privileged (CAP_FOWNER).
//O_NOATIME 标志位指定了,但是调用者的有效的用户ID和文件的拥有者不匹配,调用者又是没有特权的(CAP_FOWNER)

       EROFS  pathname refers to a file on a read-only file system and write access
              was requested.
//pathname 指定一个在只读文件系统上的文件并要求写的权利
       ETXTBSY
              pathname refers to an executable image which is currently being
              executed and write access was requested.
//pathname 指向一个可执行的镜像,而镜像本身正在被执行而进行写要求的话
       EWOULDBLOCK
              The O_NONBLOCK flag was specified, and an incompatible lease was held
              on the file (see fcntl(2)).

//O_NONBLOCK 被指定,文件保持这一个矛盾的租期(参见fcntl(2)).

CONFORMING TO(一致性)

       SVr4, 4.3BSD, POSIX.1-2001.  The O_DIRECTORY, O_NOATIME, and O_NOFOLLOW flags
       are Linux-specific, and one may need to define _GNU_SOURCE (before including
       any header files) to obtain their definitions.

       The O_CLOEXEC flag is not specified in POSIX.1-2001, but is specified in
       POSIX.1-2008.

       O_DIRECT is not specified in POSIX; one has to define _GNU_SOURCE (before
       including any header files) to get its definition.

NOTES

       Under Linux, the O_NONBLOCK flag indicates that one wants to open but does not
       necessarily have the intention to read or write.  This is typically used to
       open devices in order to get a file descriptor for use with ioctl(2).
//在Linux下,O_NONBLOCK 标识着标志着有人想打开但是并不必须有读和写的意图.这种情况典型的就是打开一个设备
//以便于获得fd使用ioctl(2)进行操作.
       Unlike the other values that can be specified in flags, the access mode values
       O_RDONLY, O_WRONLY, and O_RDWR, do not specify individual bits.  Rather, they
       define the low order two bits of flags, and are defined respectively as 0, 1,
       and 2.  In other words, the combination O_RDONLY | O_WRONLY is a logical
       error, and certainly does not have the same meaning as O_RDWR.  Linux reserves
       the special, nonstandard access mode 3 (binary 11) in flags to mean: check for
       read and write permission on the file and return a descriptor that can't be
       used for reading or writing.  This nonstandard access mode is used by some
       Linux drivers to return a descriptor that is only to be used for device-
       specific ioctl(2) operations.
//不像其他可以通过标志位指定的值,访问模式的值O_RDONLY, O_WRONLY, 和 O_RDWR并不指定单独的位.
//宁愿,定义标志位的低2位,分别定义为0,1,2.换句话说,O_RDONLY | O_WRONLY 的组合是逻辑错误的,
//当然和O_RDWR也不是相同的意思.Liuux保留着这种特性,非标准的访问模式3(二进制11)在标志位中意味着:
//在文件上检查独和写的权限并返回fd如果不能读或写的话.这种非标准的访问模式使用在一些Linux驱动中来返回fd
//当fd仅仅使用在设备指定的ioctl(2)操作中.
       The (undefined) effect of O_RDONLY | O_TRUNC varies among implementations.  On
       many systems the file is actually truncated.
//O_RDONLY | O_TRUNC 的未定义作用在不同的程序间是改变的,在很多系统下文件实际上被截断.
       There are many infelicities in the protocol underlying NFS, affecting amongst
       others O_SYNC and O_NDELAY.
//在NFS协议下有很多的不恰当的地方,在O_SYNCO_NDELAY的作用中
       POSIX provides for three different variants of synchronized I/O, corresponding
       to the flags O_SYNC, O_DSYNC, and O_RSYNC.  Currently (2.6.31), Linux only
       implements O_SYNC, but glibc maps O_DSYNC and O_RSYNC to the same numerical
       value as O_SYNC.  Most Linux file systems don't actually implement the POSIX
       O_SYNC semantics, which require all metadata updates of a write to be on disk
       on returning to userspace, but only the O_DSYNC semantics, which require only
       actual file data and metadata necessary to retrieve it to be on disk by the
       time the system call returns.
//POSIX 提供三种不同的同步IO的变量,和O_SYNC, O_DSYNC对应的变量
       Note that open() can open device special files, but creat() cannot create
       them; use mknod(2) instead.
//注意 open() 可以打开设备特定文件,但是 creat() 不能够创建它们,而是使用 mknod().
       On NFS file systems with UID mapping enabled, open() may return a file
       descriptor but, for example, read(2) requests are denied with EACCES.  This is
       because the client performs open() by checking the permissions, but UID
       mapping is performed by the server upon read and write requests.
//在NFS文件系统上使能UID映射,open()可以返回文件描述符,但是例如,read()请求被 EACCES阻止.
//这是因为客户端通过检查权限来运行 open(),但是UID 映射通过服务器的读写请求来运行的.
       If the file is newly created, its st_atime, st_ctime, st_mtime fields
       (respectively, time of last access, time of last status change, and time of
       last modification; see stat(2)) are set to the current time, and so are the
       st_ctime and st_mtime fields of the parent directory.  Otherwise, if the file
       is modified because of the O_TRUNC flag, its st_ctime and st_mtime fields are
       set to the current time.

//如果文件是新创建的,它的 st_atime, st_ctime,st_mtime 域(各自为:最后访问时间, 最后的状态改变时间,最后修改时间,参见 stat(2))
//就被设置为当前时间,父目录的st_ctime,st_mtime也是一样的.另外的,如果文件因为 O_TRUNC 标志位修改,文件的st_ctime,st_mtime就被设置为当前时间

O_DIRECT
       The O_DIRECT flag may impose alignment restrictions on the length and address
       of userspace buffers and the file offset of I/Os.  In Linux alignment
       restrictions vary by file system and kernel version and might be absent
       entirely.  However there is currently no file system-independent interface for
       an application to discover these restrictions for a given file or file system.
       Some file systems provide their own interfaces for doing so, for example the
       XFS_IOC_DIOINFO operation in xfsctl(3).
// O_DIRECT 标志可以强行修改对其的限制和用户空间缓存的地址和I/O的文件偏移.文件系统和内核版本在linux对其限制上的不同可能完全不出现.
//然而对于跟定的文件或者文件系统,当前没有对应的文件或者文件系统的接口来发现这些限制.一些文件系统提供它们自己的接口来做这些,
//例如 在 xfsctl(3) 操作的 XFS_IOC_DIOINFO
       Under Linux 2.4, transfer sizes, and the alignment of the user buffer and the
       file offset must all be multiples of the logical block size of the file
       system.  Under Linux 2.6, alignment to 512-byte boundaries suffices.

//在 Linux 2.4下,发送大小,用户缓存的对其,文件偏移必须是文件系统逻辑块大小的整数倍.
//在 Linux 2.6下,对齐是以 512 字节为界限的.
       The O_DIRECT flag was introduced in SGI IRIX, where it has alignment
       restrictions similar to those of Linux 2.4.  IRIX has also a fcntl(2) call to
       query appropriate alignments, and sizes.  FreeBSD 4.x introduced a flag of the
       same name, but without alignment restrictions.
//O_DIRECT 标志位在 SGI IRIX中介绍,它的这些对其界限比Linux 2.4的要小.IRIX也有 fcntl(2)调用来询问对应的对齐和大小.
//FreeBSD 4.X 介绍了同名的标志位,但是没有对齐限制.
       O_DIRECT support was added under Linux in kernel version 2.4.10.  Older Linux
       kernels simply ignore this flag.  Some file systems may not implement the flag
       and open() will fail with EINVAL if it is used.
//O_DIRECT 的支持在Linux 内核版本 2.4.10之后 下加了进来.更老的版本忽略这个标志位.一些文件系统可能有这个标志位的实现
//如果 open()使用了这个标志位的话可能会失败,错误号是 EINVAL.
       Applications should avoid mixing O_DIRECT and normal I/O to the same file, and
       especially to overlapping byte regions in the same file.  Even when the file
       system correctly handles the coherency issues in this situation, overall I/O
       throughput is likely to be slower than using either mode alone.  Likewise,
       applications should avoid mixing mmap(2) of files with direct I/O to the same
       files.
//程序应该避免在相同的文件上 O_DIRECT 和一般的I/O 标志混合使用,尤其是在同一个文件上重叠字节区域.
//甚至当文件系统正确的处理了在这种情况下争议问题的一致性的时候.总之 I/O吞吐量好像比使用单独任何一个模式要慢.
//同样的,程序应该避免在同一个文件上混合使用 mmap(2)和直接I/O.
       The behaviour of O_DIRECT with NFS will differ from local file systems.  Older
       kernels, or kernels configured in certain ways, may not support this
       combination.  The NFS protocol does not support passing the flag to the
       server, so O_DIRECT I/O will only bypass the page cache on the client; the
       server may still cache the I/O.  The client asks the server to make the I/O
       synchronous to preserve the synchronous semantics of O_DIRECT.  Some servers
       will perform poorly under these circumstances, especially if the I/O size is
       small.  Some servers may also be configured to lie to clients about the I/O
       having reached stable storage; this will avoid the performance penalty at some
       risk to data integrity in the event of server power failure.  The Linux NFS
       client places no alignment restrictions on O_DIRECT I/O.
// O_DIRECT 在NFS文件系统上的行为和本地文件系统是不同的.早些的内核,或者通过某些途径设置的内核,
//可能不支持这些联合.NFS协议不支持向服务器传送这个标志位,所以O_DIRECT  I/O 仅仅会传递页缓存到客户端;
//服务器仍然对I/O进行缓存.客户端要求服务器使 I/O 同步来保护O_DIRECT的语义.在这些细节上,一些服务器会表现很差,
//尤其是一些I/O大小很小的话.一些服务器也被配置为对客户端谎称I/O已经到达了稳定的存储;这将避免一些性能惩罚在服务器断电的情况下,
//数据完整性的风险.LInux NFS 客户端在 O_DIRECT I/O 上没有放置对齐限制.
       In summary, O_DIRECT is a potentially powerful tool that should be used with
       caution.  It is recommended that applications treat use of O_DIRECT as a
       performance option which is disabled by default.
//在说明中,O_DIRECT是一个潜在的有力的工具,应该小心使用,推荐程序对待O_DIRECT的使用像一个性能选项,默认是不使能的.
              "The thing that has always disturbed me about O_DIRECT is that the
              whole interface is just stupid, and was probably designed by a deranged
              monkey on some serious mind-controlling substances." -- Linus

//"一直困扰我的事情是 O_DIRECT 作为一个整个的接口看起来是愚蠢的,在一些认真的思维控制事物上很可能被疯狂的猴子设计" -- Linus
//译者注: Linus的意思就是这个接口很烂,根本就不应该设计出来.

BUGS

       Currently, it is not possible to enable signal-driven I/O by specifying
       O_ASYNC when calling open(); use fcntl(2) to enable this flag.

//当前,在调用opne()的时候不能通过指定O_ASYNC来使能一个信号驱动的IO,使用fcntl(2)来使能这个标志位

SEE ALSO

       chmod(2), chown(2), close(2), dup(2), fcntl(2), link(2), lseek(2), mknod(2),
       mmap(2), mount(2), openat(2), read(2), socket(2), stat(2), umask(2),
       unlink(2), write(2), fopen(3), feature_test_macros(7), fifo(7),
       path_resolution(7), symlink(7)

COLOPHON

       This page is part of release 3.29 of the Linux man-pages project.  A
       description of the project, and information about reporting bugs, can be found
       at http://www.kernel.org/doc/man-pages/.
Linux                                     2010-09-10                                OPEN(2)

抱歉!评论已关闭.