⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fcntl.2

📁 Unix操作系统minix 2.0源码
💻 2
字号:
.TH FCNTL 2.SH NAMEfcntl \- miscellaneous file descriptor control functions.SH SYNOPSIS.nf.ft B#include <fcntl.h>int fcntl(int \fIfd\fP, int *\fIcmd\fP, \fR[\fP\fIdata\fP\fR]\fP).ft P.fi.SH DESCRIPTION.de SP.if t .sp 0.4.if n .sp...B Fcntl()performs several file descriptor related functions, like duplicating a filedescriptor, setting the "close on exec" attribute, etc.  The.I fdargument is the file descriptor to operate on,.I cmdis the command code of the operation to perform, and.I datais an optional argument to give or receive parameters.  The commandcodes and other symbols and types are declared in <fcntl.h>.  The commandsare:.SP.BI "fcntl(" fd ", F_DUPFD, int " fd2 ")".RSReturns a new file descriptor that is a duplicate of file descriptor.IR fd .It shares the same file pointer and the same file status flags, but hasseparate file descriptor flags that are initially off.  The value of theduplicate file descriptor is the first free file descriptor greater thanor equal to.IR fd2 ..RE.SP.BI "fcntl(" fd ", F_GETFD)".RSReturns the file descriptor flags associated with file descriptor.IR fd .The flags are the "close on exec" flag.B FD_CLOEXECthat, when set, causes the file descriptor to be closed when the processexecutes another program.  The Minix-vmd specific.B FD_ASYNCHIOflag marks a file descriptor for asynchronous I/O operation..RE.SP.BI "fcntl(" fd ", F_SETFD, int " flags ")".RSSet the file descriptor flags of.I fdto.IR flags ..RE.SP.BI "fcntl(" fd ", F_GETFL)".RSReturn the file status flags and file access modes associated with the fileassociated with file descriptor.IR fd .The file status flags are.B O_NONBLOCK(non blocking I/O) and.B O_APPEND(append mode).  The file access modes are.B O_RDONLY(read-only),.B O_WRONLY(write-only) and.B O_RDWR(read-write).  These flags are also used in the second argument of.BR open (2)..RE.SP.BI "fcntl(" fd ", F_SETFL, int " flags ")".RSSet the file status flags of the file referenced by.I fdto.IR flags .Only.B O_NONBLOCKand.B O_APPENDmay be changed.  Access mode flags are ignored..RE.SPThe next four commands use a parameter of type.B struct flockthat is defined in <fcntl.h> as:.SP.RS.nf.ta +4n +8n +12nstruct flock {	short	l_type;	/* F_RDLCK, F_WRLCK, or F_UNLCK */	short	l_whence;	/* SEEK_SET, SEEK_CUR, or SEEK_END */	off_t	l_start;	/* byte offset to start of segment */	off_t	l_len;	/* length of segment */	pid_t	l_pid;	/* process id of the locks' owner */};.fi.RE.SPThis structure describes a segment of a file..B L_typeis the lock operation performed on the file segment:.B F_RDLCKto set a read lock,.B F_WRLCKto set a write lock, and.B F_UNLCKto remove a lock.  Several processes may have a read lock on a segment, butonly one process can have a write lock..B L_whencetells if the.B l_startoffset must be interpreted from the start of the file.RB ( SEEK_SET ),the current file position.RB ( SEEK_CUR ),or the end of the file.RB ( SEEK_END ).This is analogous to the third parameter of.BR lseek (2).These.B SEEK_*symbols are declared in <unistd.h>..B L_startis the starting offset of the segment of the file..B L_endis the length of the segment.  If zero then the segment extends until end offile..B L_pidis the process-id of the process currently holding a lock on the segment.It is returned by.BR F_GETLK ..SP.BI "fcntl(" fd ", F_GETLK, struct flock *" lkp ")".RSFind out if some other process has a lock on a segment of the fileassociated by file descriptor.I fdthat overlaps with the segment described by the.B flockstructure pointed to by.IR lkp .If the segment is not locked then.B l_typeis set to.BR F_UNLCK .Otherwise an.B flockstructure is returned through.I lkpthat describes the lock held by the other process..B L_startis set relative to the start of the file..RE.SP.BI "fcntl(" fd ", F_SETLK, struct flock *" lkp ")".RSRegister a lock on a segment of the file associated with file descriptor.IR fd .The file segment is described by the.B struct flockpointed to by.IR lkp .This call returns an error if any part of the segment is already locked..RE.SP.BI "fcntl(" fd ", F_SETLKW, struct flock *" lkp ")".RSRegister a lock on a segment of the file associated with file descriptor.IR fd .The file segment is described by the.B struct flockpointed to by.IR lkp .This call blocks waiting for the lock to be released if any part of thesegment is already locked..RE.SP.BI "fcntl(" fd ", F_FREESP, struct flock *" lkp ")".RSFree a segment of disk space occupied by the file associated with filedescriptor.IR fd .The segment is described by the.B struct flockpointed to by.IR lkp .The file is truncated in length to the byte position indicated by.B l_startif.B l_lenis zero.  If.B l_lenis nonzero then the file keeps its size, but the freed bytes now read aszeros.  (Other than sharing the flock structure, this call has nothing to dowith locking.).RE.SP.BI "fcntl(" fd ", F_SEEK, u64_t " pos ")".RSThis Minix-vmd specific call sets the file position of the file associatedwith file descriptor.I fdto the byte offset indicated by the 64-bit number.IR pos .This is analogous to the call.SP.RS.BI "lseek(" fd ", " pos ", SEEK_SET)".RE.SPexcept that.B F_SEEKcan be used on devices larger than 4 gigabyte..RE.SH "SEE ALSO".BR open (2),.BR dup (2),.BR lseek (2),.BR ftruncate (3),.BR int64 (3)..SH DIAGNOSTICS.B Fcntlreturns a file descriptor, flags, or.B 0to indicate success.  On error.B \-1is returned, with.B errnoset to the appropriate error code.  The most notable errors are:.TP 5.B EINTRIf a blocked.B F_SETLKWoperation is interrupted by a signal that is caught..TP.B EAGAINBy.B F_SETLKif a segment cannot be locked..TP.B EBADFA bad file descriptor in general, or an attempt to place a write lock on afile that is not open for writing, etc..TP.B ENOLCKNo locks available, the file system code has run out of internal tablespace..SH AUTHORKees J. Bot (kjb@cs.vu.nl)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -