gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

510 lines
41KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="fcntl — The fcntl and ioctl system calls" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/fcntl.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="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. Availab..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="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. Availab..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>fcntl — The fcntl and ioctl system calls &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="resource — Resource usage information" href="resource.html" />
  33. <link rel="prev" title="pty — Pseudo-terminal utilities" href="pty.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/fcntl.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h4>Previous topic</h4>
  84. <p class="topless"><a href="pty.html"
  85. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">pty</span></code> — Pseudo-terminal utilities</a></p>
  86. </div>
  87. <div>
  88. <h4>Next topic</h4>
  89. <p class="topless"><a href="resource.html"
  90. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">resource</span></code> — Resource usage information</a></p>
  91. </div>
  92. <div role="note" aria-label="source link">
  93. <h3>This Page</h3>
  94. <ul class="this-page-menu">
  95. <li><a href="../bugs.html">Report a Bug</a></li>
  96. <li>
  97. <a href="https://github.com/python/cpython/blob/main/Doc/library/fcntl.rst"
  98. rel="nofollow">Show Source
  99. </a>
  100. </li>
  101. </ul>
  102. </div>
  103. </nav>
  104. </div>
  105. </div>
  106. <div class="related" role="navigation" aria-label="related navigation">
  107. <h3>Navigation</h3>
  108. <ul>
  109. <li class="right" style="margin-right: 10px">
  110. <a href="../genindex.html" title="General Index"
  111. accesskey="I">index</a></li>
  112. <li class="right" >
  113. <a href="../py-modindex.html" title="Python Module Index"
  114. >modules</a> |</li>
  115. <li class="right" >
  116. <a href="resource.html" title="resource — Resource usage information"
  117. accesskey="N">next</a> |</li>
  118. <li class="right" >
  119. <a href="pty.html" title="pty — Pseudo-terminal utilities"
  120. accesskey="P">previous</a> |</li>
  121. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  122. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  123. <li class="switchers">
  124. <div class="language_switcher_placeholder"></div>
  125. <div class="version_switcher_placeholder"></div>
  126. </li>
  127. <li>
  128. </li>
  129. <li id="cpython-language-and-version">
  130. <a href="../index.html">3.12.3 Documentation</a> &#187;
  131. </li>
  132. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  133. <li class="nav-item nav-item-2"><a href="unix.html" accesskey="U">Unix Specific Services</a> &#187;</li>
  134. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">fcntl</span></code> — The <code class="docutils literal notranslate"><span class="pre">fcntl</span></code> and <code class="docutils literal notranslate"><span class="pre">ioctl</span></code> system calls</a></li>
  135. <li class="right">
  136. <div class="inline-search" role="search">
  137. <form class="inline-search" action="../search.html" method="get">
  138. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  139. <input type="submit" value="Go" />
  140. </form>
  141. </div>
  142. |
  143. </li>
  144. <li class="right">
  145. <label class="theme-selector-label">
  146. Theme
  147. <select class="theme-selector" oninput="activateTheme(this.value)">
  148. <option value="auto" selected>Auto</option>
  149. <option value="light">Light</option>
  150. <option value="dark">Dark</option>
  151. </select>
  152. </label> |</li>
  153. </ul>
  154. </div>
  155. <div class="document">
  156. <div class="documentwrapper">
  157. <div class="bodywrapper">
  158. <div class="body" role="main">
  159. <section id="module-fcntl">
  160. <span id="fcntl-the-fcntl-and-ioctl-system-calls"></span><h1><a class="reference internal" href="#module-fcntl" title="fcntl: The fcntl() and ioctl() system calls. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">fcntl</span></code></a> — The <code class="docutils literal notranslate"><span class="pre">fcntl</span></code> and <code class="docutils literal notranslate"><span class="pre">ioctl</span></code> system calls<a class="headerlink" href="#module-fcntl" title="Link to this heading">¶</a></h1>
  161. <hr class="docutils" id="index-0" />
  162. <p>This module performs file and I/O control on file descriptors. It is an
  163. interface to the <code class="xref c c-func docutils literal notranslate"><span class="pre">fcntl()</span></code> and <code class="xref c c-func docutils literal notranslate"><span class="pre">ioctl()</span></code> Unix routines.
  164. See the <em class="manpage"><a class="manpage reference external" href="https://manpages.debian.org/fcntl(2)">fcntl(2)</a></em> and <em class="manpage"><a class="manpage reference external" href="https://manpages.debian.org/ioctl(2)">ioctl(2)</a></em> Unix manual pages
  165. for full details.</p>
  166. <div class="availability docutils container">
  167. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p>
  168. </div>
  169. <p>All functions in this module take a file descriptor <em>fd</em> as their first
  170. argument. This can be an integer file descriptor, such as returned by
  171. <code class="docutils literal notranslate"><span class="pre">sys.stdin.fileno()</span></code>, or an <a class="reference internal" href="io.html#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.IOBase</span></code></a> object, such as <code class="docutils literal notranslate"><span class="pre">sys.stdin</span></code>
  172. itself, which provides a <a class="reference internal" href="io.html#io.IOBase.fileno" title="io.IOBase.fileno"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fileno()</span></code></a> that returns a genuine file
  173. descriptor.</p>
  174. <div class="versionchanged">
  175. <p><span class="versionmodified changed">Changed in version 3.3: </span>Operations in this module used to raise an <a class="reference internal" href="exceptions.html#IOError" title="IOError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">IOError</span></code></a> where they now
  176. raise an <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a>.</p>
  177. </div>
  178. <div class="versionchanged">
  179. <p><span class="versionmodified changed">Changed in version 3.8: </span>The fcntl module now contains <code class="docutils literal notranslate"><span class="pre">F_ADD_SEALS</span></code>, <code class="docutils literal notranslate"><span class="pre">F_GET_SEALS</span></code>, and
  180. <code class="docutils literal notranslate"><span class="pre">F_SEAL_*</span></code> constants for sealing of <a class="reference internal" href="os.html#os.memfd_create" title="os.memfd_create"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.memfd_create()</span></code></a> file
  181. descriptors.</p>
  182. </div>
  183. <div class="versionchanged">
  184. <p><span class="versionmodified changed">Changed in version 3.9: </span>On macOS, the fcntl module exposes the <code class="docutils literal notranslate"><span class="pre">F_GETPATH</span></code> constant, which obtains
  185. the path of a file from a file descriptor.
  186. On Linux(&gt;=3.15), the fcntl module exposes the <code class="docutils literal notranslate"><span class="pre">F_OFD_GETLK</span></code>, <code class="docutils literal notranslate"><span class="pre">F_OFD_SETLK</span></code>
  187. and <code class="docutils literal notranslate"><span class="pre">F_OFD_SETLKW</span></code> constants, which are used when working with open file
  188. description locks.</p>
  189. </div>
  190. <div class="versionchanged">
  191. <p><span class="versionmodified changed">Changed in version 3.10: </span>On Linux &gt;= 2.6.11, the fcntl module exposes the <code class="docutils literal notranslate"><span class="pre">F_GETPIPE_SZ</span></code> and
  192. <code class="docutils literal notranslate"><span class="pre">F_SETPIPE_SZ</span></code> constants, which allow to check and modify a pipe’s size
  193. respectively.</p>
  194. </div>
  195. <div class="versionchanged">
  196. <p><span class="versionmodified changed">Changed in version 3.11: </span>On FreeBSD, the fcntl module exposes the <code class="docutils literal notranslate"><span class="pre">F_DUP2FD</span></code> and <code class="docutils literal notranslate"><span class="pre">F_DUP2FD_CLOEXEC</span></code>
  197. constants, which allow to duplicate a file descriptor, the latter setting
  198. <code class="docutils literal notranslate"><span class="pre">FD_CLOEXEC</span></code> flag in addition.</p>
  199. </div>
  200. <div class="versionchanged">
  201. <p><span class="versionmodified changed">Changed in version 3.12: </span>On Linux &gt;= 4.5, the <a class="reference internal" href="#module-fcntl" title="fcntl: The fcntl() and ioctl() system calls. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">fcntl</span></code></a> module exposes the <code class="docutils literal notranslate"><span class="pre">FICLONE</span></code> and
  202. <code class="docutils literal notranslate"><span class="pre">FICLONERANGE</span></code> constants, which allow to share some data of one file with
  203. another file by reflinking on some filesystems (e.g., btrfs, OCFS2, and
  204. XFS). This behavior is commonly referred to as “copy-on-write”.</p>
  205. </div>
  206. <p>The module defines the following functions:</p>
  207. <dl class="py function">
  208. <dt class="sig sig-object py" id="fcntl.fcntl">
  209. <span class="sig-prename descclassname"><span class="pre">fcntl.</span></span><span class="sig-name descname"><span class="pre">fcntl</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">cmd</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">arg</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#fcntl.fcntl" title="Link to this definition">¶</a></dt>
  210. <dd><p>Perform the operation <em>cmd</em> on file descriptor <em>fd</em> (file objects providing
  211. a <a class="reference internal" href="io.html#io.IOBase.fileno" title="io.IOBase.fileno"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fileno()</span></code></a> method are accepted as well). The values used
  212. for <em>cmd</em> are operating system dependent, and are available as constants
  213. in the <a class="reference internal" href="#module-fcntl" title="fcntl: The fcntl() and ioctl() system calls. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">fcntl</span></code></a> module, using the same names as used in the relevant C
  214. header files. The argument <em>arg</em> can either be an integer value, or a
  215. <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> object. With an integer value, the return value of this
  216. function is the integer return value of the C <code class="xref c c-func docutils literal notranslate"><span class="pre">fcntl()</span></code> call. When
  217. the argument is bytes it represents a binary structure, e.g. created by
  218. <a class="reference internal" href="struct.html#struct.pack" title="struct.pack"><code class="xref py py-func docutils literal notranslate"><span class="pre">struct.pack()</span></code></a>. The binary data is copied to a buffer whose address is
  219. passed to the C <code class="xref c c-func docutils literal notranslate"><span class="pre">fcntl()</span></code> call. The return value after a successful
  220. call is the contents of the buffer, converted to a <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> object.
  221. The length of the returned object will be the same as the length of the
  222. <em>arg</em> argument. This is limited to 1024 bytes. If the information returned
  223. in the buffer by the operating system is larger than 1024 bytes, this is
  224. most likely to result in a segmentation violation or a more subtle data
  225. corruption.</p>
  226. <p>If the <code class="xref c c-func docutils literal notranslate"><span class="pre">fcntl()</span></code> call fails, an <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> is raised.</p>
  227. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">fcntl.fcntl</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">fd</span></code>, <code class="docutils literal notranslate"><span class="pre">cmd</span></code>, <code class="docutils literal notranslate"><span class="pre">arg</span></code>.</p>
  228. </dd></dl>
  229. <dl class="py function">
  230. <dt class="sig sig-object py" id="fcntl.ioctl">
  231. <span class="sig-prename descclassname"><span class="pre">fcntl.</span></span><span class="sig-name descname"><span class="pre">ioctl</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">request</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">arg</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">mutate_flag</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#fcntl.ioctl" title="Link to this definition">¶</a></dt>
  232. <dd><p>This function is identical to the <a class="reference internal" href="#fcntl.fcntl" title="fcntl.fcntl"><code class="xref py py-func docutils literal notranslate"><span class="pre">fcntl()</span></code></a> function, except
  233. that the argument handling is even more complicated.</p>
  234. <p>The <em>request</em> parameter is limited to values that can fit in 32-bits.
  235. Additional constants of interest for use as the <em>request</em> argument can be
  236. found in the <a class="reference internal" href="termios.html#module-termios" title="termios: POSIX style tty control. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">termios</span></code></a> module, under the same names as used in
  237. the relevant C header files.</p>
  238. <p>The parameter <em>arg</em> can be one of an integer, an object supporting the
  239. read-only buffer interface (like <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>) or an object supporting
  240. the read-write buffer interface (like <a class="reference internal" href="stdtypes.html#bytearray" title="bytearray"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytearray</span></code></a>).</p>
  241. <p>In all but the last case, behaviour is as for the <a class="reference internal" href="#fcntl.fcntl" title="fcntl.fcntl"><code class="xref py py-func docutils literal notranslate"><span class="pre">fcntl()</span></code></a>
  242. function.</p>
  243. <p>If a mutable buffer is passed, then the behaviour is determined by the value of
  244. the <em>mutate_flag</em> parameter.</p>
  245. <p>If it is false, the buffer’s mutability is ignored and behaviour is as for a
  246. read-only buffer, except that the 1024 byte limit mentioned above is avoided –
  247. so long as the buffer you pass is at least as long as what the operating system
  248. wants to put there, things should work.</p>
  249. <p>If <em>mutate_flag</em> is true (the default), then the buffer is (in effect) passed
  250. to the underlying <a class="reference internal" href="#fcntl.ioctl" title="fcntl.ioctl"><code class="xref py py-func docutils literal notranslate"><span class="pre">ioctl()</span></code></a> system call, the latter’s return code is
  251. passed back to the calling Python, and the buffer’s new contents reflect the
  252. action of the <a class="reference internal" href="#fcntl.ioctl" title="fcntl.ioctl"><code class="xref py py-func docutils literal notranslate"><span class="pre">ioctl()</span></code></a>. This is a slight simplification, because if the
  253. supplied buffer is less than 1024 bytes long it is first copied into a static
  254. buffer 1024 bytes long which is then passed to <a class="reference internal" href="#fcntl.ioctl" title="fcntl.ioctl"><code class="xref py py-func docutils literal notranslate"><span class="pre">ioctl()</span></code></a> and copied back
  255. into the supplied buffer.</p>
  256. <p>If the <code class="xref c c-func docutils literal notranslate"><span class="pre">ioctl()</span></code> call fails, an <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> exception is raised.</p>
  257. <p>An example:</p>
  258. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">array</span><span class="o">,</span> <span class="nn">fcntl</span><span class="o">,</span> <span class="nn">struct</span><span class="o">,</span> <span class="nn">termios</span><span class="o">,</span> <span class="nn">os</span>
  259. <span class="gp">&gt;&gt;&gt; </span><span class="n">os</span><span class="o">.</span><span class="n">getpgrp</span><span class="p">()</span>
  260. <span class="go">13341</span>
  261. <span class="gp">&gt;&gt;&gt; </span><span class="n">struct</span><span class="o">.</span><span class="n">unpack</span><span class="p">(</span><span class="s1">&#39;h&#39;</span><span class="p">,</span> <span class="n">fcntl</span><span class="o">.</span><span class="n">ioctl</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">termios</span><span class="o">.</span><span class="n">TIOCGPGRP</span><span class="p">,</span> <span class="s2">&quot; &quot;</span><span class="p">))[</span><span class="mi">0</span><span class="p">]</span>
  262. <span class="go">13341</span>
  263. <span class="gp">&gt;&gt;&gt; </span><span class="n">buf</span> <span class="o">=</span> <span class="n">array</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="s1">&#39;h&#39;</span><span class="p">,</span> <span class="p">[</span><span class="mi">0</span><span class="p">])</span>
  264. <span class="gp">&gt;&gt;&gt; </span><span class="n">fcntl</span><span class="o">.</span><span class="n">ioctl</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">termios</span><span class="o">.</span><span class="n">TIOCGPGRP</span><span class="p">,</span> <span class="n">buf</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
  265. <span class="go">0</span>
  266. <span class="gp">&gt;&gt;&gt; </span><span class="n">buf</span>
  267. <span class="go">array(&#39;h&#39;, [13341])</span>
  268. </pre></div>
  269. </div>
  270. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">fcntl.ioctl</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">fd</span></code>, <code class="docutils literal notranslate"><span class="pre">request</span></code>, <code class="docutils literal notranslate"><span class="pre">arg</span></code>.</p>
  271. </dd></dl>
  272. <dl class="py function">
  273. <dt class="sig sig-object py" id="fcntl.flock">
  274. <span class="sig-prename descclassname"><span class="pre">fcntl.</span></span><span class="sig-name descname"><span class="pre">flock</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">operation</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#fcntl.flock" title="Link to this definition">¶</a></dt>
  275. <dd><p>Perform the lock operation <em>operation</em> on file descriptor <em>fd</em> (file objects providing
  276. a <a class="reference internal" href="io.html#io.IOBase.fileno" title="io.IOBase.fileno"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fileno()</span></code></a> method are accepted as well). See the Unix manual
  277. <em class="manpage"><a class="manpage reference external" href="https://manpages.debian.org/flock(2)">flock(2)</a></em> for details. (On some systems, this function is emulated
  278. using <code class="xref c c-func docutils literal notranslate"><span class="pre">fcntl()</span></code>.)</p>
  279. <p>If the <code class="xref c c-func docutils literal notranslate"><span class="pre">flock()</span></code> call fails, an <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> exception is raised.</p>
  280. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">fcntl.flock</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">fd</span></code>, <code class="docutils literal notranslate"><span class="pre">operation</span></code>.</p>
  281. </dd></dl>
  282. <dl class="py function">
  283. <dt class="sig sig-object py" id="fcntl.lockf">
  284. <span class="sig-prename descclassname"><span class="pre">fcntl.</span></span><span class="sig-name descname"><span class="pre">lockf</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fd</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">cmd</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">len</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">start</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">whence</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#fcntl.lockf" title="Link to this definition">¶</a></dt>
  285. <dd><p>This is essentially a wrapper around the <a class="reference internal" href="#fcntl.fcntl" title="fcntl.fcntl"><code class="xref py py-func docutils literal notranslate"><span class="pre">fcntl()</span></code></a> locking calls.
  286. <em>fd</em> is the file descriptor (file objects providing a <a class="reference internal" href="io.html#io.IOBase.fileno" title="io.IOBase.fileno"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fileno()</span></code></a>
  287. method are accepted as well) of the file to lock or unlock, and <em>cmd</em>
  288. is one of the following values:</p>
  289. <dl class="py data">
  290. <dt class="sig sig-object py" id="fcntl.LOCK_UN">
  291. <span class="sig-prename descclassname"><span class="pre">fcntl.</span></span><span class="sig-name descname"><span class="pre">LOCK_UN</span></span><a class="headerlink" href="#fcntl.LOCK_UN" title="Link to this definition">¶</a></dt>
  292. <dd><p>Release an existing lock.</p>
  293. </dd></dl>
  294. <dl class="py data">
  295. <dt class="sig sig-object py" id="fcntl.LOCK_SH">
  296. <span class="sig-prename descclassname"><span class="pre">fcntl.</span></span><span class="sig-name descname"><span class="pre">LOCK_SH</span></span><a class="headerlink" href="#fcntl.LOCK_SH" title="Link to this definition">¶</a></dt>
  297. <dd><p>Acquire a shared lock.</p>
  298. </dd></dl>
  299. <dl class="py data">
  300. <dt class="sig sig-object py" id="fcntl.LOCK_EX">
  301. <span class="sig-prename descclassname"><span class="pre">fcntl.</span></span><span class="sig-name descname"><span class="pre">LOCK_EX</span></span><a class="headerlink" href="#fcntl.LOCK_EX" title="Link to this definition">¶</a></dt>
  302. <dd><p>Acquire an exclusive lock.</p>
  303. </dd></dl>
  304. <dl class="py data">
  305. <dt class="sig sig-object py" id="fcntl.LOCK_NB">
  306. <span class="sig-prename descclassname"><span class="pre">fcntl.</span></span><span class="sig-name descname"><span class="pre">LOCK_NB</span></span><a class="headerlink" href="#fcntl.LOCK_NB" title="Link to this definition">¶</a></dt>
  307. <dd><p>Bitwise OR with any of the other three <code class="docutils literal notranslate"><span class="pre">LOCK_*</span></code> constants to make
  308. the request non-blocking.</p>
  309. </dd></dl>
  310. <p>If <code class="xref py py-const docutils literal notranslate"><span class="pre">LOCK_NB</span></code> is used and the lock cannot be acquired, an
  311. <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> will be raised and the exception will have an <em>errno</em>
  312. attribute set to <a class="reference internal" href="errno.html#errno.EACCES" title="errno.EACCES"><code class="xref py py-const docutils literal notranslate"><span class="pre">EACCES</span></code></a> or <a class="reference internal" href="errno.html#errno.EAGAIN" title="errno.EAGAIN"><code class="xref py py-const docutils literal notranslate"><span class="pre">EAGAIN</span></code></a> (depending on the
  313. operating system; for portability, check for both values). On at least some
  314. systems, <code class="xref py py-const docutils literal notranslate"><span class="pre">LOCK_EX</span></code> can only be used if the file descriptor refers to a
  315. file opened for writing.</p>
  316. <p><em>len</em> is the number of bytes to lock, <em>start</em> is the byte offset at
  317. which the lock starts, relative to <em>whence</em>, and <em>whence</em> is as with
  318. <a class="reference internal" href="io.html#io.IOBase.seek" title="io.IOBase.seek"><code class="xref py py-func docutils literal notranslate"><span class="pre">io.IOBase.seek()</span></code></a>, specifically:</p>
  319. <ul class="simple">
  320. <li><p><code class="docutils literal notranslate"><span class="pre">0</span></code> – relative to the start of the file (<a class="reference internal" href="os.html#os.SEEK_SET" title="os.SEEK_SET"><code class="xref py py-const docutils literal notranslate"><span class="pre">os.SEEK_SET</span></code></a>)</p></li>
  321. <li><p><code class="docutils literal notranslate"><span class="pre">1</span></code> – relative to the current buffer position (<a class="reference internal" href="os.html#os.SEEK_CUR" title="os.SEEK_CUR"><code class="xref py py-const docutils literal notranslate"><span class="pre">os.SEEK_CUR</span></code></a>)</p></li>
  322. <li><p><code class="docutils literal notranslate"><span class="pre">2</span></code> – relative to the end of the file (<a class="reference internal" href="os.html#os.SEEK_END" title="os.SEEK_END"><code class="xref py py-const docutils literal notranslate"><span class="pre">os.SEEK_END</span></code></a>)</p></li>
  323. </ul>
  324. <p>The default for <em>start</em> is 0, which means to start at the beginning of the file.
  325. The default for <em>len</em> is 0 which means to lock to the end of the file. The
  326. default for <em>whence</em> is also 0.</p>
  327. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">fcntl.lockf</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">fd</span></code>, <code class="docutils literal notranslate"><span class="pre">cmd</span></code>, <code class="docutils literal notranslate"><span class="pre">len</span></code>, <code class="docutils literal notranslate"><span class="pre">start</span></code>, <code class="docutils literal notranslate"><span class="pre">whence</span></code>.</p>
  328. </dd></dl>
  329. <p>Examples (all on a SVR4 compliant system):</p>
  330. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">struct</span><span class="o">,</span> <span class="nn">fcntl</span><span class="o">,</span> <span class="nn">os</span>
  331. <span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="o">...</span><span class="p">)</span>
  332. <span class="n">rv</span> <span class="o">=</span> <span class="n">fcntl</span><span class="o">.</span><span class="n">fcntl</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">fcntl</span><span class="o">.</span><span class="n">F_SETFL</span><span class="p">,</span> <span class="n">os</span><span class="o">.</span><span class="n">O_NDELAY</span><span class="p">)</span>
  333. <span class="n">lockdata</span> <span class="o">=</span> <span class="n">struct</span><span class="o">.</span><span class="n">pack</span><span class="p">(</span><span class="s1">&#39;hhllhh&#39;</span><span class="p">,</span> <span class="n">fcntl</span><span class="o">.</span><span class="n">F_WRLCK</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
  334. <span class="n">rv</span> <span class="o">=</span> <span class="n">fcntl</span><span class="o">.</span><span class="n">fcntl</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">fcntl</span><span class="o">.</span><span class="n">F_SETLKW</span><span class="p">,</span> <span class="n">lockdata</span><span class="p">)</span>
  335. </pre></div>
  336. </div>
  337. <p>Note that in the first example the return value variable <em>rv</em> will hold an
  338. integer value; in the second example it will hold a <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> object. The
  339. structure lay-out for the <em>lockdata</em> variable is system dependent — therefore
  340. using the <a class="reference internal" href="#fcntl.flock" title="fcntl.flock"><code class="xref py py-func docutils literal notranslate"><span class="pre">flock()</span></code></a> call may be better.</p>
  341. <div class="admonition seealso">
  342. <p class="admonition-title">See also</p>
  343. <dl class="simple">
  344. <dt>Module <a class="reference internal" href="os.html#module-os" title="os: Miscellaneous operating system interfaces."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os</span></code></a></dt><dd><p>If the locking flags <a class="reference internal" href="os.html#os.O_SHLOCK" title="os.O_SHLOCK"><code class="xref py py-const docutils literal notranslate"><span class="pre">O_SHLOCK</span></code></a> and <a class="reference internal" href="os.html#os.O_EXLOCK" title="os.O_EXLOCK"><code class="xref py py-const docutils literal notranslate"><span class="pre">O_EXLOCK</span></code></a> are
  345. present in the <a class="reference internal" href="os.html#module-os" title="os: Miscellaneous operating system interfaces."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os</span></code></a> module (on BSD only), the <a class="reference internal" href="os.html#os.open" title="os.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.open()</span></code></a>
  346. function provides an alternative to the <a class="reference internal" href="#fcntl.lockf" title="fcntl.lockf"><code class="xref py py-func docutils literal notranslate"><span class="pre">lockf()</span></code></a> and <a class="reference internal" href="#fcntl.flock" title="fcntl.flock"><code class="xref py py-func docutils literal notranslate"><span class="pre">flock()</span></code></a>
  347. functions.</p>
  348. </dd>
  349. </dl>
  350. </div>
  351. </section>
  352. <div class="clearer"></div>
  353. </div>
  354. </div>
  355. </div>
  356. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  357. <div class="sphinxsidebarwrapper">
  358. <div>
  359. <h4>Previous topic</h4>
  360. <p class="topless"><a href="pty.html"
  361. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">pty</span></code> — Pseudo-terminal utilities</a></p>
  362. </div>
  363. <div>
  364. <h4>Next topic</h4>
  365. <p class="topless"><a href="resource.html"
  366. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">resource</span></code> — Resource usage information</a></p>
  367. </div>
  368. <div role="note" aria-label="source link">
  369. <h3>This Page</h3>
  370. <ul class="this-page-menu">
  371. <li><a href="../bugs.html">Report a Bug</a></li>
  372. <li>
  373. <a href="https://github.com/python/cpython/blob/main/Doc/library/fcntl.rst"
  374. rel="nofollow">Show Source
  375. </a>
  376. </li>
  377. </ul>
  378. </div>
  379. </div>
  380. <div id="sidebarbutton" title="Collapse sidebar">
  381. <span>«</span>
  382. </div>
  383. </div>
  384. <div class="clearer"></div>
  385. </div>
  386. <div class="related" role="navigation" aria-label="related navigation">
  387. <h3>Navigation</h3>
  388. <ul>
  389. <li class="right" style="margin-right: 10px">
  390. <a href="../genindex.html" title="General Index"
  391. >index</a></li>
  392. <li class="right" >
  393. <a href="../py-modindex.html" title="Python Module Index"
  394. >modules</a> |</li>
  395. <li class="right" >
  396. <a href="resource.html" title="resource — Resource usage information"
  397. >next</a> |</li>
  398. <li class="right" >
  399. <a href="pty.html" title="pty — Pseudo-terminal utilities"
  400. >previous</a> |</li>
  401. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  402. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  403. <li class="switchers">
  404. <div class="language_switcher_placeholder"></div>
  405. <div class="version_switcher_placeholder"></div>
  406. </li>
  407. <li>
  408. </li>
  409. <li id="cpython-language-and-version">
  410. <a href="../index.html">3.12.3 Documentation</a> &#187;
  411. </li>
  412. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  413. <li class="nav-item nav-item-2"><a href="unix.html" >Unix Specific Services</a> &#187;</li>
  414. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">fcntl</span></code> — The <code class="docutils literal notranslate"><span class="pre">fcntl</span></code> and <code class="docutils literal notranslate"><span class="pre">ioctl</span></code> system calls</a></li>
  415. <li class="right">
  416. <div class="inline-search" role="search">
  417. <form class="inline-search" action="../search.html" method="get">
  418. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  419. <input type="submit" value="Go" />
  420. </form>
  421. </div>
  422. |
  423. </li>
  424. <li class="right">
  425. <label class="theme-selector-label">
  426. Theme
  427. <select class="theme-selector" oninput="activateTheme(this.value)">
  428. <option value="auto" selected>Auto</option>
  429. <option value="light">Light</option>
  430. <option value="dark">Dark</option>
  431. </select>
  432. </label> |</li>
  433. </ul>
  434. </div>
  435. <div class="footer">
  436. &copy;
  437. <a href="../copyright.html">
  438. Copyright
  439. </a>
  440. 2001-2024, Python Software Foundation.
  441. <br />
  442. This page is licensed under the Python Software Foundation License Version 2.
  443. <br />
  444. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  445. <br />
  446. See <a href="/license.html">History and License</a> for more information.<br />
  447. <br />
  448. The Python Software Foundation is a non-profit corporation.
  449. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  450. <br />
  451. <br />
  452. Last updated on Apr 09, 2024 (13:47 UTC).
  453. <a href="/bugs.html">Found a bug</a>?
  454. <br />
  455. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  456. </div>
  457. </body>
  458. </html>
上海开阖软件有限公司 沪ICP备12045867号-1