Caution: This version of this document is no longer maintained. For the latest documentation, see


Provide control over an open file


#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>

int fcntl( int fildes, 
           int cmd,
           ... );


The descriptor for the file you want to control.
The command to execute; see below.



Use the -l c option to qcc to link against this library. This library is usually included automatically.


The fcntl() function provides control over the open file referenced by file descriptor fildes. To establish a lock with this function, open with write-only permission (O_WRONLY) or with read/write permission (O_RDWR).

The type of control is specified by the cmd argument, which may require a third data argument (arg). The cmd argument is defined in <fcntl.h>, and includes at least the following values:

If the size of the file is less than the number of bytes specified by the extra arg argument, extend the file with NUL characters.
Allocate and return a new file descriptor that's the lowest numbered available (i.e. not already open) file descriptor greater than or equal to the third argument, arg, taken as an int. The new file descriptor refers to the same file as fildes, and shares any locks.
Truncate the file to the size (in bytes) specified by the extra argument, arg.
Get the file descriptor flags associated with the file descriptor fildes. File descriptor flags are associated with a single file descriptor, and don't affect other file descriptors referring to the same file.
Get the file status flags and the file access modes associated with fildes. The flags and modes are defined in <fcntl.h>.

The file status flags (see open() for more detailed information) are:

The file access modes are:

Get the first lock that blocks the lock description pointed to by the third argument, arg, taken as a pointer to type struct flock (defined in <fcntl.h>). For more information, see the flock structure section below. The information returned overwrites the information passed to fcntl() in the structure pointed to by arg.

If no lock is found that prevents this lock from being created, the structure is left unchanged, except for the lock type, which is set to F_UNLCK. If a lock is found, the l_pid member of the structure pointed to by arg is set to the process ID of the process holding the blocking lock and l_whence is set to SEEK_SET.

Set the file descriptor flags associated with fildes to the third argument, arg, taken as type int. See the above discussion for more details.

The only defined file descriptor flag is:

When this flag is clear, the file remains open across spawn*() or exec*() calls; else the file is closed.
Set the file status flags, as shown above, for the open file description associated with fildes from the corresponding bits in the third argument, arg, taken as type int. You can't use this function to change the file access mode. All bits set in arg, other than the file status bits, are ignored.
Set or clear a file segment lock, according to the lock description pointed to by the third argument, arg, taken as a pointer to type struct flock, as defined in the header file <fcntl.h>, and documented below. This command is used to create the following locks (defined in <fcntl.h>):
Shared or read locks.
Remove either type of lock.
Exclusive or write locks.

If a lock can't be set, fcntl() returns immediately.

This command is the same as F_SETLK, except that when a lock is blocked by other locks, the process waits until the request can be satisfied. If a signal that's to be caught is received while fcntl() is waiting for a region, the call is interrupted without performing the lock operation, and fcntl() returns -1 with errno set to EINTR.

flock structure

The flock structure contains at least the following members:

short l_type
short l_whence
One of the following flags that specify where the relative offset, l_start, is measured from:
Current seek position.
End of file.
Start of file.
off_t l_start
Relative offset in bytes.
off_t l_len
Consecutive bytes to lock; if 0, then until EOF; if negative, the preceding bytes up to, but not including, the start byte.
pid_t l_pid
Process ID of the process holding the lock, returned when cmd is F_GETLK.

When a shared lock is set on a segment of a file, other processes can set shared locks on the same segment, or a portion of it. A shared lock prevents other processes from setting exclusive locks on any portion of the protected area. A request for a shared lock fails if the file was opened write-only.

An exclusive lock prevents any other process from setting a shared or an exclusive lock on a portion of the protected area. A request for an exclusive lock fails if the file was opened read-only.

Locks may start and extend beyond the current end of file, but may not start or extend before the beginning of the file; to attempt to do so is an error. A lock extends to “infinity” (the largest possible value for the file offset) if l_len is set to zero. If l_whence and l_start point to the beginning of the file, and l_len is zero, the entire file is locked.

The calling process may have only one type of lock set for each byte of a file. Before successfully returning from an F_SETLK or F_SETLKW request, the previous lock type (if any) for each byte in the specified lock region is replaced by the new lock type. All locks associated with a file for a given process are removed when a file descriptor for that file is closed by the process, or the process holding the file descriptor terminates. Locks aren't inherited by a child process using the fork() function. However, locks are inherited across exec*() or spawn*() calls.

Note: A potential for deadlock occurs if a process controlling a locked region is put to sleep by attempting to lock another process's locked region. If the system detects that sleeping until a locked region is unlocked would cause a deadlock, fcntl() fails with EDEADLK. However, the system can't always detect deadlocks in the network case, and care should be exercised in the design of your application for this possibility.

Note: Locking is a protocol designed for updating a file shared among concurrently running applications. Locks are only advisory, that is, they don't prevent an errant or poorly-designed application from overwriting a locked region of a shared file. An application should use locks to indicate regions of a file that are to be updated by the application, and it should respect the locks of other applications.

The following functions ignore locks:


-1 if an error occurred (errno is set). The successful return value(s) depend on the request type specified by arg, as shown in the following table:

A new file descriptor.
Value of the file descriptor flags (never a negative value).
Value of the file status flags and access modes as shown above (never a negative value).
Value other than -1.
Value other than -1.
Value other than -1.
Value other than -1.
Value other than -1.


The argument cmd is F_SETLK, the type of lock (l_type) is a shared lock (F_RDLCK), and the segment of a file to be locked is already exclusive-locked by another process, or the type is an exclusive lock and some portion of the segment of a file to be locked is already shared-locked or exclusive-locked by another process.
The fildes argument isn't a valid file descriptor.

The argument cmd is F_SETLK or F_SETLKW, the type of lock (l_type) is a shared lock (F_RDLCK), and fildes isn't a valid file descriptor open for reading.

The argument cmd is F_SETLK or F_SETLKW, the type of lock (l_type) is an exclusive lock (F_WRLCK), and fildes isn't a valid file descriptor open for writing.

The argument cmd is F_SETLKW, and a deadlock condition was detected.
The argument cmd is F_SETLKW, and the function was interrupted by a signal.
The argument cmd is F_DUPFD, and the third argument is negative, or greater than the configured number of maximum open file descriptors per process.

The argument cmd is F_GETLK, F_SETLK or F_SETLKW, and the data arg isn't valid, or fildes refers to a file that doesn't support locking.

The argument cmd is F_DUPFD, and the process has no unused file descriptors, or no file descriptors greater than or equal to arg are available.
The argument cmd is F_SETLK or F_SETLKW, and satisfying the lock or unlock request causes the number of lock regions in the system to exceed the system-imposed limit.
One of the values to be returned can't be represented correctly.


 * This program makes "stdout" synchronous
 * to guarantee the data is recoverable
 * (if it's redirected to a file).
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>

int main( void )
    int flags, retval;

    flags = fcntl( STDOUT_FILENO, F_GETFL );

    flags |= O_DSYNC;

    retval = fcntl( STDOUT_FILENO, F_SETFL, flags );
    if( retval == -1 ) {
        printf( "error setting stdout flags\n" );
        return EXIT_FAILURE;

    printf( "hello QNX world\n" );

    return EXIT_SUCCESS;


POSIX 1003.1

Cancellation point Read the Caveats
Interrupt handler No
Signal handler Yes
Thread Yes


The fcntl() function may be a cancellation point in the case of F_DUPFD (when dupping across the network), F_GETFD, and F_SETFD.

See also:

close(), dup(), dup2(), execl(), execle(), execlp(), execlpe(), execv(), execve(), execvp(), execvpe(), ioctl(), open()