fcntl.2
来自「<B>Digital的Unix操作系统VAX 4.2源码</B>」· 2 代码 · 共 584 行 · 第 1/2 页
2
584 行
.\" SCCSID: @(#)fcntl.2 2.1 3/10/87.TH fcntl 2.SH Namefcntl \- file control.SH Syntax#include <fcntl.h>.PPres = fcntl (\fIfd, request, arg\fP).brint res;.brint \fIfd, request, arg\fP.SH ArgumentsThe following arguments can be used with .PN fcntl :.IP "\fIfd\fR" 10Descriptor to be operated on. Depending on thefunction selected by the \fIrequest\fR argument,the \fIfd\fR argument can bea file descriptor returned by an .PN open system call, or a socket descriptor returned by a .PN socket system call..IP "\fIrequest\fR" 10Defines what you want done. The possible values are defined in.PN <fcntl.h> . See the Description section for more information..IP "\fIarg\fR" 10Varies according to the \fIrequest\fR argument. See the Descriptionsection for more information..SH Description.NXR "fcntl system call" .NXR "file descriptor" "controlling".NXR "fcntl system call" "dup2 system call".NXR "fcntl system call" "close system call"The .PN fcntlsystem call provides for control over descriptors. Thedescriptors can be either file descriptors returned by the .PN open system call orsocket descriptors returned by the .PN socket system call..PPPossible .I request arguments are the following:.NXR "fcntl system call" "request definitions".SS F_DUPFD \- Return New DescriptorThe shell provides an example of when a newdescriptor is useful. Suppose the shell receivesa command such as:.EXcat > myfile.EEThe shell needs to redirect the output of the .PN catcommand from the file descriptor 1 (standard output)to a new file named.PN myfile . The shell issues the.PN fcntlcall, using the old file descriptor of 1, to obtaina new file descriptor for the file.PN myfile ..IP F_DUPFD 13When \fIrequest\fR is set for F_DUPFD:.spThe .PN fcntl call returns a new descriptor. The new file descriptor returned has thefollowing characteristics:.RS.IP \(bu 3The file descriptor returned is the lowest numbered availabledescriptor that is greater than or equal tothe argument.I arg..IP \(bu 3The descriptor has the same object references as the original descriptor. That is, if the originalfile descriptor referred to a file, the new filedescriptor refers to a file. If the original descriptorreferred to a socket, the new file descriptor refers toa socket..IP \(buThe new descriptor shares the same file pointer if the objectwas a file. (A file pointer points to an inode, which inturn points to a file. Thus, the new descriptor refers tothe same file as the old descriptor.).IP \(buThe new descriptor has the same access mode as the olddescriptor (read, write, or read/write)..IP \(buThe new descriptor shares the same file status flags as the old file descriptor. (See the discussion ofF_GETFL and F_SETFL for a description offile status flags.).IP \(buThe close-on-exec flag associated with the new file descriptoris set to remain open across .PN execve system calls. (See the discussion of F_GETFD andF_SETFD for a description of the close-on-execflag.).RE.SS F_GETFD and F_SETFD \- Close-on-exec FlagEach file descriptor points to an entry in anarray of file pointersthat, among other things,define certain characteristics for the file. Onesuch characteristic is the close-on-exec flag. This flag defines whether or not a file remainsopen across calls to .PN execve . If cleared, the file descriptor remains open in the new image loaded by the call to .PN execve .If set, the file descriptor is closed in the newimage loaded by the call to .PN execve . .IP F_GETFD 13When \fIrequest\fR is set to F_GETFD:.spThe .PN fcntl call returnsthe close-on-exec flag associated with the file descriptor\fIfd\fR. If the low-order bit of the value returned by.PN fcntl is 0,the file remains open across calls to .PN execve . If the low-order bit of the value returnedby .PN fcntlis 1, the file descriptor is closed across calls to .PN execve ..IP F_SETFDWhen \fIrequest\fR is set to F_SETFD:.spThe.PN fcntlcall sets the close-on-exec flag associated with.I fdto the low-order bit of.I arg(0 or 1)..SS "F_GETFL and F_SETFL \- Descriptor Status Flags"Each file descriptor points to an entry in an array of filepointers that, among other things, define the file's currentstatus. One such item of status, for example, iswhether or not input/outputoperations to a file are currently blocked..PPYou might want to program your process to allow blocking so thata user who runs your process in the background, while doing other work in theforeground, need not see output from the background jobdisplayed on the screen..PP These and other status indicators are discussed in the list thatfollows. Some status indicators do not apply to all types ofdescriptors. The O_APPEND status, for example, is meaninglessfor sockets..IP F_GETFL 13When \fIrequest\fR is set to F_GETFL:.spThe .PN fcntlcall returns the file's descriptor status flags.The following names have been defined in.PN <fcntl.h>for these status flags:.RS.IP O_NDELAY 13Nonblocking I/O. If no data is available to a.PN readcall, or if awrite operation would block, the call returns \-1 withthe error [EWOULDBLOCK]. The flag FNDELAY is an obsolete synonym forO_NDELAY..IP O_FSYNC(O_SYNC) Synchronous write flag. Forces subsequent file writes to be donesynchronously. For further information, see.MS write 2 .The flag OFSYNCRON is an obsolete synonym for FSYNCRON. .IP O_APPEND Force each write to append at the end of file. This correspondsto the action taken with the O_APPEND flag of .PN open .The flag FAPPEND is an obsolete synonym for O_APPEND..IP FASYNCEnable the SIGIO signal to be sent to the process group whenI/O is possible. For example, send SIGIO when data is available to beread..IP O_NONBLOCKPOSIX environment, nonblocking I/O flag. See O_NDELAY request for descriptionof operation. The flag FNBLOCK is an obsolete synonym forO_NONBLOCK..RE.IP F_SETFL 13When \fIrequest\fR is set to F_SETFL:.spThe.PN fcntl call sets descriptor status flags specified in .I arg(see F_GETFL). Refer to the F_SETOWN section for more information. .SS F_GETOWN and F_SETOWN \- Get Or\0Set\0OwnerWith these requests, your process can recognize the software interrupts SIGIOor SIGURG.As described in .PN sigvec ,SIGIO is a signal indicating that I/O is possible on a descriptor.SIGURG indicates an urgent condition present on a socket. .IP F_GETOWN 13When \fIrequest\fR is set to F_GETOWN:.sp .5The.PN fcntlcall returns the process ID or process groupcurrently receiving SIGIO and SIGURG signals.Process groups are returnedas negative values..IP F_SETOWN 13When \fIrequest\fR is set to F_SETOWN:.sp .5The.PN fcntlcall sets the process or process groupto receive SIGIO and SIGURG signals;process groups are specified by supplying.I argas negative. Otherwise, .I argis interpreted as a process ID. See also the F_SETFL request,which sets descriptor status flags, and the description of theFASYNC flag in the F_GETFL section..SS F_GETLK, F_SETLK, and F_SETLKW \- Locking\0File\0RegionsWith these requests, your process can:.IP \(bu 3Test a file fora region that might have been read-locked or write-locked byanother process..IP \(buSet or clear a file region read or write lock..IP \(buSet a file region read or write lock, sleeping, ifnecessary, until locks previously set by other processesare unlocked..PPWhen a read lock has been set on a segment of a file,other processes can also set read locks on that filesegment or portions thereof..PPA read lock prevents any other process from write lockingthe protected area. More than one read lock can exist for agiven region of a file at a given time. The file descriptoron which a read lock is being placed must have been openedwith read access..PPA write lock prevents any other process from read lockingor write locking the protected region. Only one write lockcan exist for a given region of a file at a given time. The filedescriptor on which a write lock is being placed must havebeen opened with write access..PPLocks can start and extend beyond the current end of a file,but cannot be negative relative to the beginning of thefile..PPChanging or unlocking a region from the middle of a larger locked regionleaves two smaller regions with the old setting at either end. Locking a region that is already locked by the calling processcauses the old lock to be removed and the new lock type to takeeffect..PPAll locks associated with a file for a given process are removedwhen a file descriptor for that file is closed by that processor the process holding that file descriptor terminates. Locks arenot inherited by a child process in a .PN fork (a)2system call.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?