CHips L MINI SHELL

CHips L pro

Current Path : /proc/2/root/usr/share/doc/python-docs-2.6.6/html/library/
Upload File :
Current File : //proc/2/root/usr/share/doc/python-docs-2.6.6/html/library/fcntl.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>35.10. fcntl — The fcntl() and ioctl() system calls &mdash; Python v2.6.6 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '2.6.6',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python v2.6.6 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python v2.6.6 documentation" href="../index.html" />
    <link rel="up" title="35. Unix Specific Services" href="unix.html" />
    <link rel="next" title="35.11. pipes — Interface to shell pipelines" href="pipes.html" />
    <link rel="prev" title="35.9. pty — Pseudo-terminal utilities" href="pty.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
 

  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             accesskey="M">modules</a> |</li>
        <li class="right" >
          <a href="pipes.html" title="35.11. pipes — Interface to shell pipelines"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="pty.html" title="35.9. pty — Pseudo-terminal utilities"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v2.6.6 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="unix.html" accesskey="U">35. Unix Specific Services</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-fcntl">
<h1>35.10. <tt class="xref docutils literal"><span class="pre">fcntl</span></tt> &#8212; The <tt class="xref docutils literal"><span class="pre">fcntl()</span></tt> and <tt class="xref docutils literal"><span class="pre">ioctl()</span></tt> system calls<a class="headerlink" href="#module-fcntl" title="Permalink to this headline">¶</a></h1>
<p><em>Platforms: </em>Unix</p>
<p id="index-333">This module performs file control and I/O control on file descriptors. It is an
interface to the <tt class="xref docutils literal"><span class="pre">fcntl()</span></tt> and <tt class="xref docutils literal"><span class="pre">ioctl()</span></tt> Unix routines.</p>
<p>All functions in this module take a file descriptor <em>fd</em> as their first
argument.  This can be an integer file descriptor, such as returned by
<tt class="docutils literal"><span class="pre">sys.stdin.fileno()</span></tt>, or a file object, such as <tt class="docutils literal"><span class="pre">sys.stdin</span></tt> itself, which
provides a <tt class="xref docutils literal"><span class="pre">fileno()</span></tt> which returns a genuine file descriptor.</p>
<p>The module defines the following functions:</p>
<dl class="function">
<dt id="fcntl.fcntl">
<tt class="descclassname">fcntl.</tt><tt class="descname">fcntl</tt><big>(</big><em>fd</em>, <em>op</em><span class="optional">[</span>, <em>arg</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#fcntl.fcntl" title="Permalink to this definition">¶</a></dt>
<dd><p>Perform the requested operation on file descriptor <em>fd</em> (file objects providing
a <tt class="xref docutils literal"><span class="pre">fileno()</span></tt> method are accepted as well). The operation is defined by <em>op</em>
and is operating system dependent.  These codes are also found in the
<tt class="xref docutils literal"><span class="pre">fcntl</span></tt> module. The argument <em>arg</em> is optional, and defaults to the integer
value <tt class="docutils literal"><span class="pre">0</span></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="xref docutils literal"><span class="pre">fcntl()</span></tt> call.  When the argument is
a string it represents a binary structure, e.g. created by <a title="struct.pack" class="reference external" href="struct.html#struct.pack"><tt class="xref docutils literal"><span class="pre">struct.pack()</span></tt></a>.
The binary data is copied to a buffer whose address is passed to the C
<tt class="xref docutils literal"><span class="pre">fcntl()</span></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 <em>arg</em> 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>
<p>If the <tt class="xref docutils literal"><span class="pre">fcntl()</span></tt> fails, an <a title="exceptions.IOError" class="reference external" href="exceptions.html#exceptions.IOError"><tt class="xref docutils literal"><span class="pre">IOError</span></tt></a> is raised.</p>
</dd></dl>

<dl class="function">
<dt id="fcntl.ioctl">
<tt class="descclassname">fcntl.</tt><tt class="descname">ioctl</tt><big>(</big><em>fd</em>, <em>op</em><span class="optional">[</span>, <em>arg</em><span class="optional">[</span>, <em>mutate_flag</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#fcntl.ioctl" title="Permalink to this definition">¶</a></dt>
<dd><p>This function is identical to the <a title="fcntl.fcntl" class="reference internal" href="#fcntl.fcntl"><tt class="xref docutils literal"><span class="pre">fcntl()</span></tt></a> function, except that the
operations are typically defined in the library module <a title="(Unix) POSIX style tty control." class="reference external" href="termios.html#module-termios"><tt class="xref docutils literal"><span class="pre">termios</span></tt></a> and the
argument handling is even more complicated.</p>
<p>The op parameter is limited to values that can fit in 32-bits.</p>
<p>The parameter <em>arg</em> can be one of an integer, absent (treated identically to the
integer <tt class="docutils literal"><span class="pre">0</span></tt>), an object supporting the read-only buffer interface (most likely
a plain Python string) or an object supporting the read-write buffer interface.</p>
<p>In all but the last case, behaviour is as for the <a title="fcntl.fcntl" class="reference internal" href="#fcntl.fcntl"><tt class="xref docutils literal"><span class="pre">fcntl()</span></tt></a> function.</p>
<p>If a mutable buffer is passed, then the behaviour is determined by the value of
the <em>mutate_flag</em> parameter.</p>
<p>If it is false, the buffer&#8217;s mutability is ignored and behaviour is as for a
read-only buffer, except that the 1024 byte limit mentioned above is avoided &#8211;
so long as the buffer you pass is as least as long as what the operating system
wants to put there, things should work.</p>
<p>If <em>mutate_flag</em> is true, then the buffer is (in effect) passed to the
underlying <a title="fcntl.ioctl" class="reference internal" href="#fcntl.ioctl"><tt class="xref docutils literal"><span class="pre">ioctl()</span></tt></a> system call, the latter&#8217;s return code is passed back to
the calling Python, and the buffer&#8217;s new contents reflect the action of the
<a title="fcntl.ioctl" class="reference internal" href="#fcntl.ioctl"><tt class="xref docutils literal"><span class="pre">ioctl()</span></tt></a>.  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 <a title="fcntl.ioctl" class="reference internal" href="#fcntl.ioctl"><tt class="xref docutils literal"><span class="pre">ioctl()</span></tt></a> and copied back into the supplied
buffer.</p>
<p>If <em>mutate_flag</em> is not supplied, then from Python 2.5 it defaults to true,
which is a change from versions 2.3 and 2.4. Supply the argument explicitly if
version portability is a priority.</p>
<p>An example:</p>
<div class="highlight-python"><div class="highlight"><pre><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>
<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>
<span class="go">13341</span>
<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="s">&#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="s">&quot;  &quot;</span><span class="p">))[</span><span class="mi">0</span><span class="p">]</span>
<span class="go">13341</span>
<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="s">&#39;h&#39;</span><span class="p">,</span> <span class="p">[</span><span class="mi">0</span><span class="p">])</span>
<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>
<span class="go">0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">buf</span>
<span class="go">array(&#39;h&#39;, [13341])</span>
</pre></div>
</div>
</dd></dl>

<dl class="function">
<dt id="fcntl.flock">
<tt class="descclassname">fcntl.</tt><tt class="descname">flock</tt><big>(</big><em>fd</em>, <em>op</em><big>)</big><a class="headerlink" href="#fcntl.flock" title="Permalink to this definition">¶</a></dt>
<dd>Perform the lock operation <em>op</em> on file descriptor <em>fd</em> (file objects providing
a <tt class="xref docutils literal"><span class="pre">fileno()</span></tt> method are accepted as well). See the Unix manual
<em>flock(2)</em> for details.  (On some systems, this function is emulated
using <tt class="xref docutils literal"><span class="pre">fcntl()</span></tt>.)</dd></dl>

<dl class="function">
<dt id="fcntl.lockf">
<tt class="descclassname">fcntl.</tt><tt class="descname">lockf</tt><big>(</big><em>fd</em>, <em>operation</em><span class="optional">[</span>, <em>length</em><span class="optional">[</span>, <em>start</em><span class="optional">[</span>, <em>whence</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#fcntl.lockf" title="Permalink to this definition">¶</a></dt>
<dd><p>This is essentially a wrapper around the <a title="fcntl.fcntl" class="reference internal" href="#fcntl.fcntl"><tt class="xref docutils literal"><span class="pre">fcntl()</span></tt></a> locking calls.  <em>fd</em> is
the file descriptor of the file to lock or unlock, and <em>operation</em> is one of the
following values:</p>
<ul class="simple">
<li><tt class="xref docutils literal"><span class="pre">LOCK_UN</span></tt> &#8211; unlock</li>
<li><tt class="xref docutils literal"><span class="pre">LOCK_SH</span></tt> &#8211; acquire a shared lock</li>
<li><tt class="xref docutils literal"><span class="pre">LOCK_EX</span></tt> &#8211; acquire an exclusive lock</li>
</ul>
<p>When <em>operation</em> is <tt class="xref docutils literal"><span class="pre">LOCK_SH</span></tt> or <tt class="xref docutils literal"><span class="pre">LOCK_EX</span></tt>, it can also be
bitwise ORed with <tt class="xref docutils literal"><span class="pre">LOCK_NB</span></tt> to avoid blocking on lock acquisition.
If <tt class="xref docutils literal"><span class="pre">LOCK_NB</span></tt> is used and the lock cannot be acquired, an
<a title="exceptions.IOError" class="reference external" href="exceptions.html#exceptions.IOError"><tt class="xref docutils literal"><span class="pre">IOError</span></tt></a> will be raised and the exception will have an <em>errno</em>
attribute set to <tt class="xref docutils literal"><span class="pre">EACCES</span></tt> or <tt class="xref docutils literal"><span class="pre">EAGAIN</span></tt> (depending on the
operating system; for portability, check for both values).  On at least some
systems, <tt class="xref docutils literal"><span class="pre">LOCK_EX</span></tt> can only be used if the file descriptor refers to a
file opened for writing.</p>
<p><em>length</em> is the number of bytes to lock, <em>start</em> is the byte offset at which the
lock starts, relative to <em>whence</em>, and <em>whence</em> is as with <tt class="xref docutils literal"><span class="pre">fileobj.seek()</span></tt>,
specifically:</p>
<ul class="simple">
<li><tt class="xref docutils literal"><span class="pre">0</span></tt> &#8211; relative to the start of the file (<tt class="xref docutils literal"><span class="pre">SEEK_SET</span></tt>)</li>
<li><tt class="xref docutils literal"><span class="pre">1</span></tt> &#8211; relative to the current buffer position (<tt class="xref docutils literal"><span class="pre">SEEK_CUR</span></tt>)</li>
<li><tt class="xref docutils literal"><span class="pre">2</span></tt> &#8211; relative to the end of the file (<tt class="xref docutils literal"><span class="pre">SEEK_END</span></tt>)</li>
</ul>
<p>The default for <em>start</em> is 0, which means to start at the beginning of the file.
The default for <em>length</em> is 0 which means to lock to the end of the file.  The
default for <em>whence</em> is also 0.</p>
</dd></dl>

<p>Examples (all on a SVR4 compliant system):</p>
<div class="highlight-python"><div class="highlight"><pre><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>

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

<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="s">&#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>
<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>
</pre></div>
</div>
<p>Note that in the first example the return value variable <em>rv</em> will hold an
integer value; in the second example it will hold a string value.  The structure
lay-out for the <em>lockdata</em> variable is system dependent &#8212; therefore using the
<a title="fcntl.flock" class="reference internal" href="#fcntl.flock"><tt class="xref docutils literal"><span class="pre">flock()</span></tt></a> call may be better.</p>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt>Module <a title="Miscellaneous operating system interfaces." class="reference external" href="os.html#module-os"><tt class="xref docutils literal"><span class="pre">os</span></tt></a></dt>
<dd>If the locking flags <tt class="xref docutils literal"><span class="pre">O_SHLOCK</span></tt> and <tt class="xref docutils literal"><span class="pre">O_EXLOCK</span></tt> are present
in the <a title="Miscellaneous operating system interfaces." class="reference external" href="os.html#module-os"><tt class="xref docutils literal"><span class="pre">os</span></tt></a> module (on BSD only), the <a title="os.open" class="reference external" href="os.html#os.open"><tt class="xref docutils literal"><span class="pre">os.open()</span></tt></a> function
provides an alternative to the <a title="fcntl.lockf" class="reference internal" href="#fcntl.lockf"><tt class="xref docutils literal"><span class="pre">lockf()</span></tt></a> and <a title="fcntl.flock" class="reference internal" href="#fcntl.flock"><tt class="xref docutils literal"><span class="pre">flock()</span></tt></a> functions.</dd>
</dl>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h4>Previous topic</h4>
            <p class="topless"><a href="pty.html"
                                  title="previous chapter">35.9. <tt class="docutils literal"><span class="pre">pty</span></tt> &#8212; Pseudo-terminal utilities</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="pipes.html"
                                  title="next chapter">35.11. <tt class="docutils literal"><span class="pre">pipes</span></tt> &#8212; Interface to shell pipelines</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
  <li><a href="../bugs.html">Report a Bug</a></li>
  <li><a href="../_sources/library/fcntl.txt"
         rel="nofollow">Show Source</a></li>
</ul>

          <div id="searchbox" style="display: none">
            <h3>Quick search</h3>
              <form class="search" action="../search.html" method="get">
                <input type="text" name="q" size="18" />
                <input type="submit" value="Go" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
              <p class="searchtip" style="font-size: 90%">
              Enter search terms or a module, class or function name.
              </p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="pipes.html" title="35.11. pipes — Interface to shell pipelines"
             >next</a> |</li>
        <li class="right" >
          <a href="pty.html" title="35.9. pty — Pseudo-terminal utilities"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v2.6.6 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="unix.html" >35. Unix Specific Services</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2011, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.  
    <a href="http://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on Jul 20, 2011.
    <a href="../bugs.html">Found a bug</a>?
    <br />
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.6.
    </div>

  </body>
</html>

Copyright 2K16 - 2K18 Indonesian Hacker Rulez