📄 module-fcntl.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>8.12 fcntl -- The fcntl() and ioctl() system calls</title>
<META NAME="description" CONTENT="8.12 fcntl -- The fcntl() and ioctl() system calls">
<META NAME="keywords" CONTENT="lib">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="STYLESHEET" href="lib.css" tppabs="http://www.python.org/doc/current/lib/lib.css">
<LINK REL="next" href="module-pipes.html" tppabs="http://www.python.org/doc/current/lib/module-pipes.html">
<LINK REL="previous" href="module-pty.html" tppabs="http://www.python.org/doc/current/lib/module-pty.html">
<LINK REL="up" href="unix.html" tppabs="http://www.python.org/doc/current/lib/unix.html">
<LINK REL="next" href="module-pipes.html" tppabs="http://www.python.org/doc/current/lib/module-pipes.html">
</head>
<body>
<DIV CLASS="navigation"><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="module-pty.html" tppabs="http://www.python.org/doc/current/lib/module-pty.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
alt="Previous Page" width="32"></A></td>
<td><A href="unix.html" tppabs="http://www.python.org/doc/current/lib/unix.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
alt="Up One Level" width="32"></A></td>
<td><A href="module-pipes.html" tppabs="http://www.python.org/doc/current/lib/module-pipes.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="module-pty.html" tppabs="http://www.python.org/doc/current/lib/module-pty.html">8.11 pty </A>
<b class="navlabel">Up:</b> <a class="sectref" href="unix.html" tppabs="http://www.python.org/doc/current/lib/unix.html">8. Unix Specific Services</A>
<b class="navlabel">Next:</b> <a class="sectref" href="module-pipes.html" tppabs="http://www.python.org/doc/current/lib/module-pipes.html">8.13 pipes </A>
<br><hr></DIV>
<!--End of Navigation Panel-->
<H1><A NAME="SECTION00101200000000000000000">
8.12 <tt class="module">fcntl</tt> --
The <tt class="function">fcntl()</tt> and <tt class="function">ioctl()</tt> system calls</A>
</H1>
<P>
<p class='availability'>Availability: <span
class='platform'>Unix</span>.</p>
<P>
<P>
This module performs file control and I/O control on file descriptors.
It is an interface to the <tt class="cfunction">fcntl()</tt> and <tt class="cfunction">ioctl()</tt>
Unix routines. File descriptors can be obtained with the
<tt class="method">fileno()</tt> method of a file or socket object.
<P>
The module defines the following functions:
<P>
<dl><dt><b><a name='l2h-1868'><tt class='function'>fcntl</tt></a></b> (<var>fd, op</var><big>[</big><var>, arg</var><big>]</big>)
<dd>
Perform the requested operation on file descriptor <var>fd</var>.
The operation is defined by <var>op</var> and is operating system
dependent. Typically these codes can be retrieved from the library
module <tt class="module">FCNTL</tt>. The argument <var>arg</var>
is optional, and defaults to the integer value <code>0</code>. 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 <tt class="cfunction">fcntl()</tt>
call. When the argument is a string it represents a binary
structure, e.g. created by <tt class="function">struct.pack()</tt>. The binary
data is copied to a buffer whose address is passed to the C
<tt class="cfunction">fcntl()</tt> 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 <var>arg</var>
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.
<P>
If the <tt class="cfunction">fcntl()</tt> fails, an <tt class="exception">IOError</tt> is
raised.
</dl>
<P>
<dl><dt><b><a name='l2h-1869'><tt class='function'>ioctl</tt></a></b> (<var>fd, op, arg</var>)
<dd>
This function is identical to the <tt class="function">fcntl()</tt> function, except
that the operations are typically defined in the library module
<tt class="module">IOCTL</tt>.
</dl>
<P>
<dl><dt><b><a name='l2h-1870'><tt class='function'>flock</tt></a></b> (<var>fd, op</var>)
<dd>
Perform the lock operation <var>op</var> on file descriptor <var>fd</var>.
See the Unix manual <span class='manpage'><i>flock</i>(3)</span> for details. (On some
systems, this function is emulated using <tt class="cfunction">fcntl()</tt>.)
</dl>
<P>
<dl><dt><b><a name='l2h-1871'><tt class='function'>lockf</tt></a></b> (<var>fd, code, </var><big>[</big><var>len, </var><big>[</big><var>start, </var><big>[</big><var>whence</var><big>]</big><big>]</big><big>]</big>)
<dd>
This is a wrapper around the <tt class="constant">FCNTL.F_SETLK</tt> and
<tt class="constant">FCNTL.F_SETLKW</tt> <tt class="function">fcntl()</tt> calls. See the Unix
manual for details.
</dl>
<P>
If the library modules <tt class="module">FCNTL</tt> or
<tt class="module">IOCTL</tt> are missing, you can find the
opcodes in the C include files <code><sys/fcntl.h></code> and
<code><sys/ioctl.h></code>. You can create the modules yourself with the
<b class="program">h2py</b> script, found in the <span class="file">Tools/scripts/</span> directory.
<P>
Examples (all on a SVR4 compliant system):
<P>
<dl><dd><pre class="verbatim">
import struct, fcntl, FCNTL
file = open(...)
rv = fcntl(file.fileno(), FCNTL.O_NDELAY, 1)
lockdata = struct.pack('hhllhh', FCNTL.F_WRLCK, 0, 0, 0, 0, 0)
rv = fcntl.fcntl(file.fileno(), FCNTL.F_SETLKW, lockdata)
</pre></dl>
<P>
Note that in the first example the return value variable <code>rv</code> will
hold an integer value; in the second example it will hold a string
value. The structure lay-out for the <var>lockdata</var> variable is
system dependent -- therefore using the <tt class="function">flock()</tt> call may be
better.
<DIV CLASS="navigation"><p><hr><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="module-pty.html" tppabs="http://www.python.org/doc/current/lib/module-pty.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
alt="Previous Page" width="32"></A></td>
<td><A href="unix.html" tppabs="http://www.python.org/doc/current/lib/unix.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
alt="Up One Level" width="32"></A></td>
<td><A href="module-pipes.html" tppabs="http://www.python.org/doc/current/lib/module-pipes.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="module-pty.html" tppabs="http://www.python.org/doc/current/lib/module-pty.html">8.11 pty </A>
<b class="navlabel">Up:</b> <a class="sectref" href="unix.html" tppabs="http://www.python.org/doc/current/lib/unix.html">8. Unix Specific Services</A>
<b class="navlabel">Next:</b> <a class="sectref" href="module-pipes.html" tppabs="http://www.python.org/doc/current/lib/module-pipes.html">8.13 pipes </A>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>See <i><a href="about.html" tppabs="http://www.python.org/doc/current/lib/about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -