Fcntl function learning in Linux

Source: Internet
Author: User
Tags flock
Linux fcntl function learning for Linux first, understand the function of fcntl function: Operate on file features based on file descriptions. 1. usage: intfcntl (intfd, intcmd); intfcntl (intfd, intcmd, longar... linux fcntl function learning for Linux first, understand the function of fcntl function: Operate on file features based on file descriptions. 1. usage: int fcntl (int fd, int cmd); int fcntl (int fd, int cmd, long arg); int fcntl (int fd, int cmd, struct flock * lock ); 2. parameter: fd: File description. Cmd: Operation Command. Arg: parameters used by commands. Lock: Same as above. You can run the following command to copy the file description. 2. FD_CLOEXEC: Set the close-on-exec flag. If the FD_CLOEXEC bit is 0, the file remains open during execve. Otherwise, it is disabled. III. F_GETFD: Mark of the description word used to read the file. IV. F_SETFD: Set the description word mark of the file. V. F_GETFL: indicates the file reading status. VI. F_SETFL: Set the file status flag. O_RDONLY, O_WRONLY, random, O_CREAT, O_EXCL, O_NOCTTY, and O_TRUNC are not affected. you can change the O_APPEND, O_ASYNC, O_DIRECT, O_NOATIME, and O_NONBLOCK. 7. f_GETLK, F_SETLK and F_SETLKW: Get, release or test record lock. the following struct pointer is used: F_SETLK: get the lock within the specified byte range (F_RDLCK, F_WRLCK) or release the lock (F_UNLCK ). If a lock operation conflicts with another process,-1 is returned and errno is set to EACCES or EAGAIN. F_SETLKW: behavior is like F_SETLK, except for sleeping and waiting when the lock cannot be obtained. If a signal is received while waiting, the system returns immediately and sets errno to EINTR. F_GETLK: get the file lock information. F_UNLCK: Release the file lock. To set the read lock, the file must be opened as read. To set the write lock, the file must be opened as a write. To set the read/write lock, the file must be opened in read/write mode. 8. signal management F_GETOWN, F_SETOWN, F_GETSIG, and F_SETSIG are used to obtain IO signals. F_GETOWN: gets the process or group ID that receives the SIGIO or SIGURG event signal on the file description fd. F_SETOWN: specifies the process or process Group ID that will receive SIGIO or SIGURG event signals on the file description fd. F_GETSIG: obtains signals that can be performed on the input and output of an identifier. F_SETSIG: Set the signal that can be performed by the input and output. Most of the time, the program does not need to use select () or poll () to implement complete asynchronous I/O. 9. The lease (Leases) F_SETLEASE and F_GETLEASE are used for the lease of the current process on the file. A file lease provides a mechanism to notify a process that owns a file lease when a process tries to open or break the file content. F_SETLEASE: set or delete a file lease based on the following symbol values. 1. f_RDLCK sets the read lease. when the file is opened or broken by another process, the current process with the lease will be announced. 2. F_WRLCK sets the write lease. when the file is opened or broken by another process in read or write mode, the current process with the lease will be announced. 3. F_UNLCK delete the file lease. F_GETLEASE: Gets the lease type. 10. file or directory change notice (more than linux 2.4) when a file in the directory or directory of the fd index changes, the process will be notified. The advertised events specified by the arg parameter are as follows. two or more values can be combined by the OR operation. 1. DN_ACCESS file accessed (read, pread, readv) 2. the DN_MODIFY file is modified (write, pwrite, writev, truncate, ftruncate) 3. the DN_CREATE file is created (open, creat, mknod, mkdir, link, symlink, rename) 4. DN_DELETE file deleted (unlink, rmdir) 5. the DN_RENAME file is renamed (rename) 6. if the attributes of the DN_ATTRIB file are changed (chown, chmod, utime [s]), the returned results indicate that different operations have different return values: F_DUPFD: New File description F_GETFD: flag value F_GETFL: flag value F_GETOWN: file descriptive word owner F_GETSIG: the notification signal to be sent when read and write become available, or 0 for traditional SIGIO Action returns 0 for other commands. -1 is returned for failure, and errno is set to an EACCES/EAGAIN value below: The operation is not allowed. it is not feasible yet. EBADF: the file description word is invalid. EDEADLK: deadlock may occur. EFAULT: lock operation occurs outside the accessible address space EINTR: The operation is interrupted by signals EINVAL: the parameter is invalid EMFILE: The process has exceeded the maximum available range of the file ENOLCK: the lock has been exhausted EPERM: struct flock {short l_type;/* lock type: F_RDLCK, F_WRLCK, F_UNLCK */short l_whence;/* l_start field reference point: SEEK_SET (file header ), SEEK_CUR (current position of the file), SEEK_END (end of the file) */off_t l_start;/* offset from the l_whence field */off_t l_len;/* length to be locked */pid_t l_pid; /* process ID (F_GETLK) for which the file lock is currently obtained )*/};
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.