fcntlThe fcntl and ioctl system callsfcntlioctl系统调用


This module performs file control and I/O control on file descriptors. 该模块对文件描述符执行文件控制和I/O控制。It is an interface to the fcntl() and ioctl() Unix routines. 它是fcntl()ioctl()Unix例程的接口。For a complete description of these calls, see fcntl(2) and ioctl(2) Unix manual pages.有关这些调用的完整描述,请参阅fcntl(2)ioctl(2)Unix手册页。

All functions in this module take a file descriptor fd as their first argument. 该模块中的所有函数都将文件描述符fd作为它们的第一个参数。This can be an integer file descriptor, such as returned by sys.stdin.fileno(), or an io.IOBase object, such as sys.stdin itself, which provides a fileno() that returns a genuine file descriptor.这可以是一个整数文件描述符,例如由sys.stdin.fileno()返回,也可以是io.IOBase对象,例如sys.stdin本身,它提供了一个返回真正文件描述符的fileno()

Changed in version 3.3:版本3.3中更改: Operations in this module used to raise an IOError where they now raise an OSError.此模块中的操作过去会引发IOError,现在会引发OSError

Changed in version 3.8:版本3.8中更改: The fcntl module now contains F_ADD_SEALS, F_GET_SEALS, and F_SEAL_* constants for sealing of os.memfd_create() file descriptors.fcntl模块现在包含F_ADD_SEALSF_GET_SELSF_SEAL_*常量,用于密封os.memfd_create()文件描述符。

Changed in version 3.9:版本3.9中更改: On macOS, the fcntl module exposes the F_GETPATH constant, which obtains the path of a file from a file descriptor. 在macOS上,fcntl模块公开F_GETPATH常量,该常量从文件描述符中获取文件的路径。On Linux(>=3.15), the fcntl module exposes the F_OFD_GETLK, F_OFD_SETLK and F_OFD_SETLKW constants, which working with open file description locks.在Linux(>=3.15)上,fcntl模块公开F_OFD_GETLKF_OFD_SETLKF_OFD_SELKW常量,这些常量与打开的文件描述锁一起工作。

Changed in version 3.10:版本3.10中更改: On Linux >= 2.6.11, the fcntl module exposes the F_GETPIPE_SZ and F_SETPIPE_SZ constants, which allow to check and modify a pipe’s size respectively.在Linux>=2.6.11上,fcntl模块公开F_GETPPIPE_SZF_SETPIPE_SZ常量,它们允许分别检查和修改管道的大小。

The module defines the following functions:该模块定义了以下功能:

fcntl.fcntl(fd, cmd, arg=0)

Perform the operation cmd on file descriptor fd (file objects providing a fileno() method are accepted as well). 对文件描述符fd执行cmd操作(也接受提供fileno()方法的文件对象)。The values used for cmd are operating system dependent, and are available as constants in the fcntl module, using the same names as used in the relevant C header files. 用于cmd的值依赖于操作系统,并且在fcntl模块中可用作常量,使用与相关C头文件中使用的名称相同的名称。The argument arg can either be an integer value, or a bytes object. 参数arg可以是整数值,也可以是bytes对象。With an integer value, the return value of this function is the integer return value of the C fcntl() call. 对于整数值,此函数的返回值是C fcntl()调用的整数返回值。When the argument is bytes it represents a binary structure, e.g. created by struct.pack(). 当参数是字节时,它表示一个二进制结构,例如由struct.pack()创建。The binary data is copied to a buffer whose address is passed to the C fcntl() call. 二进制数据被复制到缓冲区,缓冲区的地址被传递给C fcntl()调用。The return value after a successful call is the contents of the buffer, converted to a bytes object. 成功调用后的返回值是缓冲区的内容,转换为bytes对象。The length of the returned object will be the same as the length of the arg argument. 返回的对象的长度将与arg参数的长度相同。This is limited to 1024 bytes. 这被限制为1024个字节。If the information returned in the buffer by the operating system is larger than 1024 bytes, this is most likely to result in a segmentation violation or a more subtle data corruption.如果操作系统在缓冲区中返回的信息大于1024字节,则最有可能导致分段冲突或更微妙的数据损坏。

If the fcntl() fails, an OSError is raised.如果fcntl()失败,则会引发一个OSError

Raises an auditing event fcntl.fcntl with arguments fd, cmd, arg.使用参数fdcmdarg引发审核事件fcntl.fcntl

fcntl.ioctl(fd, request, arg=0, mutate_flag=True)

This function is identical to the fcntl() function, except that the argument handling is even more complicated.此函数与fcntl()函数相同,只是参数处理更加复杂。

The request parameter is limited to values that can fit in 32-bits. request参数被限制为可以适应32位的值。Additional constants of interest for use as the request argument can be found in the termios module, under the same names as used in the relevant C header files.termios模块中可以找到与相关C头文件中使用的名称相同的、用作request参数的其他感兴趣的常量。

The parameter arg can be one of an integer, an object supporting the read-only buffer interface (like bytes) or an object supporting the read-write buffer interface (like bytearray).参数arg可以是整数、支持只读缓冲区接口(如bytes)的对象或支持读写缓冲区接口的对象(如bytearray)中的一个。

In all but the last case, behaviour is as for the fcntl() function.除最后一种情况外,其他所有情况下的行为与fcntl()函数相同。

If a mutable buffer is passed, then the behaviour is determined by the value of the mutate_flag parameter.如果传递了可变缓冲区,那么行为由mutate_flag参数的值决定。

If it is false, the buffer’s mutability is ignored and behaviour is as for a read-only buffer, except that the 1024 byte limit mentioned above is avoided – so long as the buffer you pass is at least as long as what the operating system wants to put there, things should work.如果它是false,那么缓冲区的可变性将被忽略,其行为与只读缓冲区相同,只是避免了上面提到的1024字节限制,只要您传递的缓冲区至少与操作系统想要放置的缓冲区一样长,一切都应该正常。

If mutate_flag is true (the default), then the buffer is (in effect) passed to the underlying ioctl() system call, the latter’s return code is passed back to the calling Python, and the buffer’s new contents reflect the action of the ioctl(). 如果mutate_flagtrue(默认值),则缓冲区(实际上)被传递给底层ioctl()系统调用,后者的返回代码被传递回调用Python,缓冲区的新内容反映ioctl()的操作。This is a slight simplification, because if the supplied buffer is less than 1024 bytes long it is first copied into a static buffer 1024 bytes long which is then passed to ioctl() and copied back into the supplied buffer.这是一个轻微的简化,因为如果提供的缓冲区长度小于1024字节,则首先将其复制到1024字节长的静态缓冲区中,然后将其传递给ioctl()并复制回提供的缓冲区时。

If the ioctl() fails, an OSError exception is raised.如果ioctl()失败,则会引发OSError异常。

An example:例如:

>>> import array, fcntl, struct, termios, os
>>> os.getpgrp()
13341
>>> struct.unpack('h', fcntl.ioctl(0, termios.TIOCGPGRP, " "))[0]
13341
>>> buf = array.array('h', [0])
>>> fcntl.ioctl(0, termios.TIOCGPGRP, buf, 1)
0
>>> buf
array('h', [13341])

Raises an auditing event fcntl.ioctl with arguments fd, request, arg.使用参数fdrequestarg引发审核事件fcntl.ioctl

fcntl.flock(fd, operation)

Perform the lock operation operation on file descriptor fd (file objects providing a fileno() method are accepted as well). 对文件描述符fd执行锁定操作operation(也接受提供fileno()方法的文件对象)。See the Unix manual flock(2) for details. 有关详细信息,请参阅Unix手册flock(2)(On some systems, this function is emulated using fcntl().)(在某些系统上,此函数是使用fcntl()模拟的。)

If the flock() fails, an OSError exception is raised.如果flock()失败,则会引发OSError异常。

Raises an auditing event fcntl.flock with arguments fd, operation.使用参数fdoperation引发审核事件fcntl.flock

fcntl.lockf(fd, cmd, len=0, start=0, whence=0)

This is essentially a wrapper around the fcntl() locking calls. 这本质上是一个围绕fcntl()锁定调用的包装器。fd is the file descriptor (file objects providing a fileno() method are accepted as well) of the file to lock or unlock, and cmd is one of the following values:fd是要锁定或解锁的文件的文件描述符(也接受提供fileno()方法的文件对象),cmd是以下值之一:

  • LOCK_UN – unlock

  • LOCK_SHacquire a shared lock获取共享锁

  • LOCK_EXacquire an exclusive lock获取独占锁

When cmd is LOCK_SH or LOCK_EX, it can also be bitwise ORed with LOCK_NB to avoid blocking on lock acquisition. cmdLOCK_SHLOCK_EX时,它也可以与LOCK_NB进行位“或”运算,以避免锁定获取时的阻塞。If LOCK_NB is used and the lock cannot be acquired, an OSError will be raised and the exception will have an errno attribute set to EACCES or EAGAIN (depending on the operating system; for portability, check for both values). 如果使用了LOCK_NB并且无法获取锁,则将引发一个OSError,并且异常的errno属性将设置为EACCESEAGAIN(取决于操作系统;为了便于移植,请检查这两个值)。On at least some systems, LOCK_EX can only be used if the file descriptor refers to a file opened for writing.至少在某些系统上,只有当文件描述符引用为写入而打开的文件时,才能使用LOCK_EX

len is the number of bytes to lock, start is the byte offset at which the lock starts, relative to whence, and whence is as with io.IOBase.seek(), specifically:len是要锁定的字节数,start是锁定开始的字节偏移量,相对于wherewhereio.IOBase.seek()相同,具体而言:

The default for start is 0, which means to start at the beginning of the file. start的默认值为0,这意味着从文件的开头开始。The default for len is 0 which means to lock to the end of the file. len的默认值是0,这意味着锁定到文件的末尾。The default for whence is also 0.where的默认值也是0。

Raises an auditing event fcntl.lockf with arguments fd, cmd, len, start, whence.使用参数fdcmdlenstartwhere引发审核事件fcntllockf。

Examples (all on a SVR4 compliant system):示例(均在SVR4兼容系统上):

import struct, fcntl, os
f = open(...)
rv = fcntl.fcntl(f, fcntl.F_SETFL, os.O_NDELAY)

lockdata = struct.pack('hhllhh', fcntl.F_WRLCK, 0, 0, 0, 0, 0)
rv = fcntl.fcntl(f, fcntl.F_SETLKW, lockdata)

Note that in the first example the return value variable rv will hold an integer value; in the second example it will hold a bytes object. 请注意,在第一个示例中,返回值变量rv将包含一个整数值;在第二个示例中,它将保存一个bytes对象。The structure lay-out for the lockdata variable is system dependent — therefore using the flock() call may be better.lockdata变量的结构布局取决于系统,因此使用flock()调用可能会更好。

See also

Module os

If the locking flags O_SHLOCK and O_EXLOCK are present in the os module (on BSD only), the os.open() function provides an alternative to the lockf() and flock() functions.如果锁定标志O_SHLOCKO_EXLOCK存在于os模块中(仅在BSD上),那么os.open()函数提供了lockf()flock()函数的替代方法。