Apply or remove an advisory lock on an open file
#include <fcntl.h> int flock( int filedes, int operation );
You can OR LOCK_NB with LOCK_EX or LOCK_SH to prevent the function from blocking (see below).
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
The flock() function applies or removes an advisory lock on the file associated with the open file descriptor filedes.
Advisory locks allow cooperating processes to perform consistent operations on files, but they don't guarantee consistency.
The locking mechanism allows two types of locks: shared and exclusive. At any time, multiple shared locks may be applied to a file, but at no time are multiple exclusive, or both shared and exclusive, locks allowed simultaneously on a file.
A shared lock may be upgraded to an exclusive lock, and vice versa, by specifying the appropriate lock type. The previous lock is released and a new lock is applied (possibly after other processes have gained and released the lock).
Requesting a lock on an object that's already locked causes the caller to be blocked until the lock may be acquired. If you don't want the caller to be blocked, you can specify LOCK_NB in the operation to make the call fail instead (errno is set to EWOULDBLOCK).
In QNX Neutrino 7.0 or later, if you open the same file multiple times in a process, an flock() on one file descriptor referring to a file locks out an flock() on a different fd.
Processes blocked awaiting a lock may be awakened by signals.
Safety: | |
---|---|
Cancellation point | No |
Interrupt handler | No |
Signal handler | Yes |
Thread | Yes |