This version of this document is no longer maintained. For the latest documentation, see http://www.qnx.com/developers/docs. |
Provide control over an open file
#include <sys/types.h> #include <unistd.h> #include <fcntl.h> int fcntl( int fildes, int cmd, ... );
libc
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:
The file status flags (see open() for more detailed information) are:
The file access modes are:
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.
The only defined file descriptor flag is:
If a lock can't be set, fcntl() returns immediately.
The flock structure contains at least the following members:
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.
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. |
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:
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_GETLK, F_SETLK or F_SETLKW, and the data arg isn't valid, or fildes refers to a file that doesn't support locking.
/* * 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; }
Safety: | |
---|---|
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.
close(), dup(), dup2(), execl(), execle(), execlp(), execlpe(), execv(), execve(), execvp(), execvpe(), open()