A detailed explanation of FCUTL function cluster

Source: Internet
Author: User

Function Description: According to the file description to manipulate the characteristics of the file.

#include <unistd.h>

#include <fcntl.h>

int fcntl (int fd, int cmd);

int fcntl (int fd, int cmd, long arg);

int fcntl (int fd, int cmd, struct flock *lock);

Description

FCNTL () provides control for (file) descriptors. Parameter FD is a descriptor of the parameter cmd operation (described below). For the value of CMD, Fcntl can accept the third parameter int arg.

[Return value]

The return value of Fcntl () is related to the command. If an error occurs, all commands return-1, and a different value is returned if successful. The following three commands have specific return values: F_DUPFD, F_GETFD, F_GETFL, and F_getown.

F_DUPFD returns a new file descriptor

F_GETFD return the corresponding flag

F_GETFL, F_getown returns a positive process ID or negative process group ID

There are 5 functions of the FCNTL function:

1. Copy an existing descriptor (CMD=F_DUPFD).

2. Get/Set File descriptor tags (cmd=f_getfd or f_setfd).

3. Get/Set File status tag (CMD=F_GETFL or F_SETFL).

4. Get/Set up asynchronous I/O ownership (Cmd=f_getown or F_setown).

5. Get/Set record lock (CMD=F_GETLK, f_setlk or f_setlkw).

1. F_DUPFD of cmd value:

F_DUPFD returns a (file) descriptor that is described below:

• A minimum of one available descriptor greater than or equal to ARG

• Reference to an object like the original operator

• Returns a new descriptor that shares the same offset (offset) as Arg if the object is a file.

• Same access mode (read, write or read/write)

• Same file status flags (for example: Two file descriptors share the same status flag)

• Close-on-exec flags combined with new file descriptors are set to cross access Execve (2) system calls

Actually call DUP (OLDFD);

is equivalent to

Fcntl (OLDFD, F_DUPFD, 0);

While calling Dup2 (OLDFD, NEWFD);

is equivalent to

Close (OLDFD);

Fcntl (OLDFD, F_DUPFD, NEWFD);

2. F_getfd and f_setfd of the CMD value:

The F_GETFD obtains the close-on-exec symbol associated with the file descriptor FD, similar to the fd_cloexec. If the return value and Fd_cloexec are performed with the result of 0, the file remains interleaved to exec (), otherwise the file will be closed if run through EXEC (Arg ignored)

F_SETFD sets the CLOSE-ON-EXEC flag, which is determined by the FD_CLOEXEC bit of parameter arg, and it should be understood that many existing programs involving file descriptor flags do not use constant fd_cloexec, but instead set this flag to 0 (system default, Do not close at exec) or 1 (closed at exec)

You must be careful when modifying the file descriptor flag or file status flag, first get the current flag value, and then modify it as you want, and finally set the new flag value. You cannot simply execute the F_SETFD or F_SETFL command, which closes the previously set flag bit.

3. F_GETFL and F_SETFL of the CMD value:

F_GETFL gets the file status flag for FD, as described below (ARG is ignored), when the open function is explained

The file status flag. Unfortunately, three access-mode flags (O_rdonly, o_wronly, and O_RDWR) do not account for 1 digits. (The values for these three flags are 0, 1, and 2, and for historical reasons, these three values are mutually exclusive-a file can have only one of these three values.) Therefore, we must first use the o_accmode phase of the shield character and obtain the access mode bit, and then compare the result with these three kinds of values.

F_SETFL set to the ARG descriptor status flag, several flags that can be changed are: O_append,o_nonblock,o_sync and O_async. The FCNTL has a total of 7 file status flags: O_rdonly, O_wronly, O_rdwr, O_append, O_nonblock, O_sync and O_async

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.