上一话题

pty — 伪终端实用程序

下一话题
就业培训     下载中心     Wiki     联络
登录   注册

Log
  1. 首页
  2. Python 3.12.4
  3. 索引
  4. 模块
  5. 下一
  6. 上一
  7. Python 标准库
  8. Unix 特定服务
  9. fcntl — fcntl and ioctl 系统调用

fcntl — fcntl and ioctl 系统调用 ¶


This module performs file and I/O control on file descriptors. It is an interface to the fcntl() and ioctl() Unix routines. See the fcntl(2) and ioctl(2) Unix manual pages for full details.

可用性 :Unix,非 Emscripten,非 WASI。

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

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

3.8 版改变: fcntl 模块现在包含 F_ADD_SEALS , F_GET_SEALS ,和 F_SEAL_* 常量为密封 os.memfd_create() 文件描述符。

3.9 版改变: 在 macOS,fcntl 模块暴露 F_GETPATH constant, which obtains the path of a file from a file descriptor. On Linux(>=3.15), the fcntl module exposes the F_OFD_GETLK , F_OFD_SETLK and F_OFD_SETLKW constants, which are used when working with open file description locks.

3.10 版改变: 在 Linux >= 2.6.11,fcntl 模块暴露 F_GETPIPE_SZ and F_SETPIPE_SZ constants, which allow to check and modify a pipe’s size respectively.

3.11 版改变: 在 FreeBSD,fcntl 模块暴露 F_DUP2FD and F_DUP2FD_CLOEXEC constants, which allow to duplicate a file descriptor, the latter setting FD_CLOEXEC flag in addition.

Changed in version 3.12: On Linux >= 4.5, the fcntl module exposes the FICLONE and FICLONERANGE constants, which allow to share some data of one file with another file by reflinking on some filesystems (e.g., btrfs, OCFS2, and XFS). This behavior is commonly referred to as “copy-on-write”.

模块定义了下列函数:

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() call fails, an OSError 被引发。

引发 审计事件 fcntl.fcntl 采用自变量 fd , cmd , arg .

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() call fails, an 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.ioctl 采用自变量 fd , request , arg .

fcntl. flock ( fd , operation ) ¶

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

若 flock() call fails, an OSError 异常被引发。

引发 审计事件 fcntl.flock 采用自变量 fd , operation .

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

这本质上是包裹器围绕 fcntl() 锁定调用。 fd 是文件描述符 (文件对象提供 fileno() 方法也是可以接受的) 对于要锁定 (或解锁) 的文件,和 cmd 是下列值之一:

fcntl. LOCK_UN ¶

Release an existing lock.

fcntl. LOCK_SH ¶

Acquire a shared lock.

fcntl. LOCK_EX ¶

Acquire an exclusive lock.

fcntl. LOCK_NB ¶

Bitwise OR with any of the other three LOCK_* constants to make the request non-blocking.

若 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:

  • 0 – 相对于文件起始 ( os.SEEK_SET )

  • 1 – 相对于当前缓冲位置 ( os.SEEK_CUR )

  • 2 – 相对于文件末尾 ( os.SEEK_END )

默认为 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.

引发 审计事件 fcntl.lockf 采用自变量 fd , cmd , len , start , whence .

范例 (所有在 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 存在于 os 模块 (仅在 BSD), os.open() 函数会提供替代为 lockf() and flock() 函数。

上一话题

pty — 伪终端实用程序

下一话题

resource — 资源使用信息

本页

  • 报告 Bug
  • 展示源

快速搜索

键入搜索术语或模块、类、函数名称。

  1. 首页
  2. Python 3.12.4
  3. 索引
  4. 模块
  5. 下一
  6. 上一
  7. Python 标准库
  8. Unix 特定服务
  9. fcntl — fcntl and ioctl 系统调用

版权所有  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1