ftrylockfile

Acquires an application-level lock of a file object without blocking upon failure.

Library:LibC
Classification:POSIX
Service:File and Directory I/O

Syntax

  #include <stdio.h> 
   
  int ftrylockfile (
     FILE   *fp);
  

Parameters

fp

(IN) Points to the file object to lock.

Return Values

If the lock can be obtained, returns 0. Otherwise, returns a nonzero value.

Remarks

The ftrylockfile function is a non-blocking version of flockfile.

The ftrylockfile function behaves as if there is a lock count associated with each file object or stream. This count is 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 ftrylockfile function is called, the count is incremented on the following conditions:

  • The count is zero

  • The count is positive and the caller owns the file object. This allows matching calls to ftrylockfile and funlockfile to be nested.

If the lock cannot be obtained, ftrylockfile immediately returns with a nonzero value. Each call to the funlockfile function decrements the count.

All functions that reference file objects behave as if they use flockfile and funlockfile internally to obtain ownership of these file objects.

See Also