35.9. fcntl fcntl and ioctl 系统调用


本模块对文件描述符,履行文件控制和 I/O 控制。它接口到 fcntl() and ioctl() Unix 例程。这些调用的完整描述,见 fcntl(2) and ioctl(2) Unix 手册页。

本模块中的所有函数接受文件描述符 fd 作为其第 1 自变量。这可以是整数文件描述符,譬如返回通过 sys.stdin.fileno() ,或 io.IOBase 对象,譬如 sys.stdin 自身,其提供 fileno() 返回真正的文件描述符。

3.3 版改变: 使用此模块中的操作引发 IOError 它们现在引发 OSError .

模块定义了下列函数:

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

履行操作 cmd 对于文件描述符 fd (文件对象提供 fileno() method are accepted as well). 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. The argument arg can either be an integer value, or a bytes object. With an integer value, the return value of this function is the integer return value of the C fcntl() call. When the argument is bytes it represents a binary structure, e.g. created by struct.pack() . The binary data is copied to a buffer whose address is passed to the C fcntl() call. The return value after a successful call is the contents of the buffer, converted to a bytes object. The length of the returned object will be the same as the length of the arg argument. This is limited to 1024 bytes. 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.

fcntl() 失败, OSError 被引发。

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

此函数等同于 fcntl() 函数,除自变量处理甚至更复杂外。

The request parameter is limited to values that can fit in 32-bits. 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.

参数 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 ).

In all but the last case, behaviour is as for the fcntl() 函数。

If a mutable buffer is passed, then the behaviour is determined by the value of the 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.

mutate_flag 为 True (默认),那么 (实际上) 会将缓冲传递给底层 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() . 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.

ioctl() 失败, OSError 异常被引发。

范例:

>>> 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])
						
fcntl. flock ( fd , operation )

履行锁定操作 operation 对于文件描述符 fd (文件对象提供 fileno() 方法也是可以接受的)。见 Unix 手册 flock(2) 了解细节。(在某些系统,此函数的模拟是使用 fcntl() )。

flock() 失败, OSError 异常被引发。

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

这本质上是包裹器围绕 fcntl() 锁定调用。 fd is the file descriptor of the file to lock or unlock, and cmd 是下列值之一:

  • LOCK_UN – 解锁
  • LOCK_SH – 获得共享锁
  • LOCK_EX – 获得独占锁

cmd is LOCK_SH or LOCK_EX ,它还可以是按位 OR (或) 采用 LOCK_NB 以避免阻塞,当获得锁时。若 LOCK_NB 有使用但无法获得锁, OSError 会被引发且异常将拥有 errno 属性设置为 EACCES or EAGAIN (depending on the operating system; for portability, check for both values). On at least some systems, LOCK_EX can only be used if the file descriptor refers to a file opened for writing.

len 是要锁定的字节数, start is the byte offset at which the lock starts, relative to whence ,和 whence is as with io.IOBase.seek() , specifically:

默认为 start is 0, which means to start at the beginning of the file. The default for len is 0 which means to lock to the end of the file. The default for whence is also 0.

范例 (所有在 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. The structure lay-out for the lockdata variable is system dependent — therefore using the flock() 调用可能更好。

另请参阅

模块 os
若锁定标志 O_SHLOCK and O_EXLOCK are present in the os 模块 (仅在 BSD), os.open() 函数会提供替代为 lockf() and flock() 函数。