flockfile(3posix)


NAME

   flockfile, ftrylockfile, funlockfile --- stdio locking functions

SYNOPSIS

   #include <stdio.h>

   void flockfile(FILE *file);
   int ftrylockfile(FILE *file);
   void funlockfile(FILE *file);

DESCRIPTION

   These functions shall provide for explicit application-level locking of
   stdio  (FILE  *)  objects.  These  functions can be used by a thread to
   delineate a sequence of I/O statements that are executed as a unit.

   The flockfile() function shall acquire for  a  thread  ownership  of  a
   (FILE *) object.

   The  ftrylockfile()  function shall acquire for a thread ownership of a
   (FILE *) object if the object is available; ftrylockfile()  is  a  non-
   blocking version of flockfile().

   The  funlockfile()  function  shall relinquish the ownership granted to
   the thread.  The behavior is undefined  if  a  thread  other  than  the
   current owner calls the funlockfile() function.

   The  functions shall behave as if there is a lock count associated with
   each (FILE *) object. This count is implicitly initialized to zero when
   the  (FILE  *)  object is created. The (FILE *) object is unlocked when
   the count is zero. When the count is positive, a single thread owns the
   (FILE  *) object. When the flockfile() function is called, if the count
   is zero or if the count is positive and the caller owns  the  (FILE  *)
   object,  the  count shall be incremented. Otherwise, the calling thread
   shall be suspended, waiting for the count to return to zero. Each  call
   to  funlockfile() shall decrement the count. This allows matching calls
   to  flockfile()   (or   successful   calls   to   ftrylockfile())   and
   funlockfile() to be nested.

   All  functions that reference (FILE *) objects, except those with names
   ending in _unlocked, shall  behave  as  if  they  use  flockfile()  and
   funlockfile() internally to obtain ownership of these (FILE *) objects.

RETURN VALUE

   None for flockfile() and funlockfile().

   The  ftrylockfile() function shall return zero for success and non-zero
   to indicate that the lock cannot be acquired.

ERRORS

   No errors are defined.

   The following sections are informative.

EXAMPLES

   None.

APPLICATION USAGE

   Applications  using  these  functions  may  be  subject   to   priority
   inversion, as discussed in the Base Definitions volume of POSIX.12008,
   Section 3.287, Priority Inversion.

RATIONALE

   The flockfile()  and  funlockfile()  functions  provide  an  orthogonal
   mutual-exclusion  lock  for  each  FILE.   The  ftrylockfile() function
   provides a non-blocking attempt to acquire a file  lock,  analogous  to
   pthread_mutex_trylock().

   These  locks behave as if they are the same as those used internally by
   stdio for thread-safety.  This both  provides  thread-safety  of  these
   functions  without  requiring  a  second  level of internal locking and
   allows functions in stdio to be implemented in  terms  of  other  stdio
   functions.

   Application  developers and implementors should be aware that there are
   potential deadlock problems on FILE objects.  For  example,  the  line-
   buffered  flushing  semantics of stdio (requested via {_IOLBF}) require
   that certain input operations sometimes cause the buffered contents  of
   implementation-defined  line-buffered  output streams to be flushed. If
   two threads each hold the lock on the other's  FILE,  deadlock  ensues.
   This  type  of  deadlock  can  be  avoided by acquiring FILE locks in a
   consistent  order.  In  particular,  the  line-buffered  output  stream
   deadlock  can  typically be avoided by acquiring locks on input streams
   before locks on output streams if a thread would be acquiring both.

   In summary, threads sharing stdio streams with other  threads  can  use
   flockfile()  and funlockfile() to cause sequences of I/O performed by a
   single thread to be kept bundled.  The  only  case  where  the  use  of
   flockfile()  and  funlockfile()  is  required  is  to  provide  a scope
   protecting uses of the  *_unlocked  functions/macros.  This  moves  the
   cost/performance tradeoff to the optimal point.

FUTURE DIRECTIONS

   None.

SEE ALSO

   getc_unlocked()

   The  Base  Definitions  volume of POSIX.12008, Section 3.287, Priority
   Inversion, <stdio.h>

COPYRIGHT

   Portions of this text are reprinted and reproduced in  electronic  form
   from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
   -- Portable Operating System Interface (POSIX),  The  Open  Group  Base
   Specifications  Issue  7,  Copyright  (C)  2013  by  the  Institute  of
   Electrical and Electronics Engineers, Inc and The Open Group.  (This is
   POSIX.1-2008  with  the  2013  Technical Corrigendum 1 applied.) In the
   event of any discrepancy between this version and the original IEEE and
   The  Open Group Standard, the original IEEE and The Open Group Standard
   is the referee document. The original Standard can be  obtained  online
   at http://www.unix.org/online.html .

   Any  typographical  or  formatting  errors that appear in this page are
   most likely to have been introduced during the conversion of the source
   files    to   man   page   format.   To   report   such   errors,   see
   https://www.kernel.org/doc/man-pages/reporting_bugs.html .





Opportunity


Personal Opportunity - Free software gives you access to billions of dollars of software at no cost. Use this software for your business, personal use or to develop a profitable skill. Access to source code provides access to a level of capabilities/information that companies protect though copyrights. Open source is a core component of the Internet and it is available to you. Leverage the billions of dollars in resources and capabilities to build a career, establish a business or change the world. The potential is endless for those who understand the opportunity.

Business Opportunity - Goldman Sachs, IBM and countless large corporations are leveraging open source to reduce costs, develop products and increase their bottom lines. Learn what these companies know about open source and how open source can give you the advantage.





Free Software


Free Software provides computer programs and capabilities at no cost but more importantly, it provides the freedom to run, edit, contribute to, and share the software. The importance of free software is a matter of access, not price. Software at no cost is a benefit but ownership rights to the software and source code is far more significant.


Free Office Software - The Libre Office suite provides top desktop productivity tools for free. This includes, a word processor, spreadsheet, presentation engine, drawing and flowcharting, database and math applications. Libre Office is available for Linux or Windows.





Free Books


The Free Books Library is a collection of thousands of the most popular public domain books in an online readable format. The collection includes great classical literature and more recent works where the U.S. copyright has expired. These books are yours to read and use without restrictions.


Source Code - Want to change a program or know how it works? Open Source provides the source code for its programs so that anyone can use, modify or learn how to write those programs themselves. Visit the GNU source code repositories to download the source.





Education


Study at Harvard, Stanford or MIT - Open edX provides free online courses from Harvard, MIT, Columbia, UC Berkeley and other top Universities. Hundreds of courses for almost all major subjects and course levels. Open edx also offers some paid courses and selected certifications.


Linux Manual Pages - A man or manual page is a form of software documentation found on Linux/Unix operating systems. Topics covered include computer programs (including library and system calls), formal standards and conventions, and even abstract concepts.