35.9. fcntl fcntl and ioctl 系统调用

本模块对文件描述符,履行文件控制和 I/O 控制。它接口到 fcntl() and ioctl() Unix routines.

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

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

模块定义了下列函数:

fcntl. fcntl ( fd , op [ , arg ] )

履行操作 op 对于文件描述符 fd (文件对象提供 fileno() method are accepted as well). The values used for op 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 is optional, and defaults to the integer value 0 . When present, it can either be an integer value, or a string. With the argument missing or an integer value, the return value of this function is the integer return value of the C fcntl() call. When the argument is a string 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 string object. The length of the returned string 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 , op [ , arg [ , mutate_flag ] ] )

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

The op parameter is limited to values that can fit in 32-bits. Additional constants of interest for use as the op 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, absent (treated identically to the integer 0 ), an object supporting the read-only buffer interface (most likely a plain Python string) or an object supporting the read-write buffer interface.

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 as 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() 失败, IOError 异常被引发。

范例:

>>> 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 , op )

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

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

fcntl. lockf ( fd , operation [ , length [ , start [ , whence ] ] ] )

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

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

operation 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.

length 是要锁定的字节数, 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 length 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 string value. 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() 函数。

上一话题

35.8. pty — 伪终端实用程序

下一话题

35.10. pipes — Shell 管道接口

本页