3 fcntl \- miscellaneous file descriptor control functions
9 int fcntl(int \fIfd\fP, int \fIcmd\fP, \fR[\fP\fIdata\fP\fR]\fP)
18 performs several file descriptor related functions, like duplicating a file
19 descriptor, setting the "close on exec" attribute, etc. The
21 argument is the file descriptor to operate on,
23 is the command code of the operation to perform, and
25 is an optional argument to give or receive parameters. The command
26 codes and other symbols and types are declared in <fcntl.h>. The commands
29 .BI "fcntl(" fd ", F_DUPFD, int " fd2 ")"
31 Returns a new file descriptor that is a duplicate of file descriptor
33 It shares the same file pointer and the same file status flags, but has
34 separate file descriptor flags that are initially off. The value of the
35 duplicate file descriptor is the first free file descriptor greater than
40 .BI "fcntl(" fd ", F_GETFD)"
42 Returns the file descriptor flags associated with file descriptor
44 The flags are the "close on exec" flag
46 that, when set, causes the file descriptor to be closed when the process
47 executes another program. The Minix-vmd specific
49 flag marks a file descriptor for asynchronous I/O operation.
52 .BI "fcntl(" fd ", F_SETFD, int " flags ")"
54 Set the file descriptor flags of
60 .BI "fcntl(" fd ", F_GETFL)"
62 Return the file status flags and file access modes associated with the file
63 associated with file descriptor
65 The file status flags are
67 (non blocking I/O) and
69 (append mode). The file access modes are
75 (read-write). These flags are also used in the second argument of
79 .BI "fcntl(" fd ", F_SETFL, int " flags ")"
81 Set the file status flags of the file referenced by
89 may be changed. Access mode flags are ignored.
92 The next four commands use a parameter of type
94 that is defined in <fcntl.h> as:
100 short l_type; /* F_RDLCK, F_WRLCK, or F_UNLCK */
101 short l_whence; /* SEEK_SET, SEEK_CUR, or SEEK_END */
102 off_t l_start; /* byte offset to start of segment */
103 off_t l_len; /* length of segment */
104 pid_t l_pid; /* process id of the locks' owner */
109 This structure describes a segment of a file.
111 is the lock operation performed on the file segment:
115 to set a write lock, and
117 to remove a lock. Several processes may have a read lock on a segment, but
118 only one process can have a write lock.
122 offset must be interpreted from the start of the file
124 the current file position
126 or the end of the file
128 This is analogous to the third parameter of
132 symbols are declared in <unistd.h>.
134 is the starting offset of the segment of the file.
136 is the length of the segment. If zero then the segment extends until end of
139 is the process-id of the process currently holding a lock on the segment.
143 .BI "fcntl(" fd ", F_GETLK, struct flock *" lkp ")"
145 Find out if some other process has a lock on a segment of the file
146 associated by file descriptor
148 that overlaps with the segment described by the
150 structure pointed to by
152 If the segment is not locked then
158 structure is returned through
160 that describes the lock held by the other process.
162 is set relative to the start of the file.
165 .BI "fcntl(" fd ", F_SETLK, struct flock *" lkp ")"
167 Register a lock on a segment of the file associated with file descriptor
169 The file segment is described by the
173 This call returns an error if any part of the segment is already locked.
176 .BI "fcntl(" fd ", F_SETLKW, struct flock *" lkp ")"
178 Register a lock on a segment of the file associated with file descriptor
180 The file segment is described by the
184 This call blocks waiting for the lock to be released if any part of the
185 segment is already locked.
188 .BI "fcntl(" fd ", F_FREESP, struct flock *" lkp ")"
190 This call frees a segment of disk space occupied by the
191 file associated with file descriptor
193 The segment is described by the
197 The file is truncated in length to the byte position indicated by
203 is nonzero then the file keeps its size, but the freed bytes now read as
204 zeros. (Other than sharing the flock structure, this call has nothing to do
205 with locking.) (This call is common among UNIX(-like) systems.)
208 .BI "fcntl(" fd ", F_SEEK, u64_t " pos ")"
210 This Minix-vmd specific call sets the file position of the file associated
213 to the byte offset indicated by the 64-bit number
215 This is analogous to the call
218 .BI "lseek(" fd ", " pos ", SEEK_SET)"
223 can be used on devices larger than 4 gigabyte.
233 returns a file descriptor, flags, or
235 to indicate success. On error
239 set to the appropriate error code. The most notable errors are:
244 operation is interrupted by a signal that is caught.
249 if a segment cannot be locked.
252 A bad file descriptor in general, or an attempt to place a write lock on a
253 file that is not open for writing, etc.
256 No locks available, the file system code has run out of internal table
259 Kees J. Bot <kjb@cs.vu.nl>
262 .\" $PchId: fcntl.2,v 1.2 2000/08/11 19:39:51 philip Exp $