<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!--Converted with LaTeX2HTML 98.2 beta6 (August 14th, 1998)
original version by:  Nikos Drakos, CBLU, University of Leeds
* revised and updated by:  Marcus Hennecke, Ross Moore, Herb Swan
* with significant contributions from:
  Jens Lippmann, Marek Rouchal, Martin Wilck and others -->
<HTML>
<HEAD>
<TITLE>8.9 fcntl --
The fcntl() and ioctl() system calls</TITLE>
<META NAME="description" CONTENT="8.9 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">
<LINK REL="next" HREF="module-posixfile.html">
<LINK REL="previous" HREF="module-TERMIOSuppercase.html">
<LINK REL="up" HREF="unix.html">
<LINK REL="next" HREF="module-posixfile.html">
</HEAD>
<BODY bgcolor="#ffffff">
<div class=navigation>
<table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="module-posixfile.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="unix.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="module-TERMIOSuppercase.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Library Reference</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><a
 href="modindex.html"><img width=32 height=32 align=bottom border=0 alt="modules"
 src="../icons/modules.gif"></a></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="module-posixfile.html">8.10 posixfile  </A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="unix.html">8. Unix Specific Services</A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="module-TERMIOSuppercase.html">8.8 TERMIOS  </A></span>
<br><hr></div>
<!--End of Navigation Panel-->

<H1><A NAME="SECTION0010900000000000000000">
8.9 <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>
<a name="l2h-1487"></a>
<a name="l2h-1488"></a>

<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-1483"><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><a name="l2h-1489"></a>. The argument <var>arg</var>
  is optional, and defaults to the integer value <tt>0</tt>.  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.  In
  case the <tt class=cfunction>fcntl()</tt> fails, an <tt class=exception>IOError</tt> is
  raised.
</dl>

<P>
<dl><dt><b><a name="l2h-1484"><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-1485"><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-1486"><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><a name="l2h-1490"></a> or
<tt class=module>IOCTL</tt><a name="l2h-1491"></a> are missing, you can find the
opcodes in the C include files <tt>&lt;sys/fcntl.h&gt;</tt> and
<tt>&lt;sys/ioctl.h&gt;</tt>.  You can create the modules yourself with the
<b class=program>h2py</b> script, found in the <tt class=file>Tools/scripts/</tt> directory.

<P>
Examples (all on a SVR4 compliant system):

<P>
<dl><dd><pre class=verbatim>
import struct, 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(file.fileno(), FCNTL.F_SETLKW, lockdata)
</pre></dl>

<P>
Note that in the first example the return value variable <tt>rv</tt> 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.

<p>
<div class=navigation><hr><table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="module-posixfile.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="unix.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="module-TERMIOSuppercase.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Library Reference</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><a
 href="modindex.html"><img width=32 height=32 align=bottom border=0 alt="modules"
 src="../icons/modules.gif"></a></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="module-posixfile.html">8.10 posixfile  </A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="unix.html">8. Unix Specific Services</A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="module-TERMIOSuppercase.html">8.8 TERMIOS  </A></span>
</div>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>Send comments on this document to <a href="mailto:python-docs@python.org">python-docs@python.org</a>.
</ADDRESS>
</BODY>
</HTML>
