flock

Contains information about an advisory lock.

Service:File and Directory

Structure

  #include <fcntl.h>
  
  struct flock 
  {
     pid_t   l_pid;
     pid_t   l_tid;
     short   l_type; 
     short   l_whence; 
     long    ________spare;
     long    ________reserved1; 
     off_t   l_start;
     long    ________reserved2; 
     off_t   l_len;
  };
  

Fields

l_pid

Specifies the process ID of the owner of the lock.

l_tid

Specifies the thread ID of the owner of the lock.

l_type

Specifies the lock type:

Flag

Value

Description

F_RDLCK

1

Indicates a read lock, which is a sharable lock

F_WRLCK

2

Indicates a write lock, which is an exclusive lock

F_UNLCK

3

Indicates unlock, which is used to remove a lock.

l_whence

Specifies the flag for the starting offset of the lock:

Flag

Value

Description

SEEK_SET

0

Indicates the start of the file plus the offset

SEEK_CUR

1

Indicates the current position in the file plus the offset

SEEK_END

2

Indicates the end of the file plus the offset

________spare

Unused.

________reserved1

Provides padding to resemble the 64-bit structure.

l_start

Specifies the relative offset, in bytes, for the lock.

________reserved2

Provides padding to resemble the 64-bit structure.

l_len

Specifies the length for the byte range lock. If zero, specifies until EOF.

Remarks

File locking with the fcntl function is constrained to per-process and is not shared across processes. As long as this remains the case, the value in the l_pid field is a copy of the value in the l_tid field.

When a shared lock (F_RDLCK) is set, other processes can set shared locks on that segment or a portion of it. It prevents other processes from setting an exclusive (F_WRLCK) on any portion of the shared lock area. For a shared lock request to succeed, the file must be opened with read access.

When an exclusive lock (F_WRLCK) is set, other processes cannot set any type of lock on any portion of the exclusive lock area. For an exclusive lock request to succeed, the file must be opened with write access.