Fcntl function for file I/O

Source: Internet
Author: User

The fcntl function can change the nature of opened files.

#include <fcntl.h> fcntl(  filedes,  cmd, ... -

In each instance in this section, the third parameter is always an integer, which corresponds to the annotation section in the function prototype shown above. However, when the record lock is described, the third parameter is a pointer to a structure.

Fcntl functions have five functions:

(1) copy an existing Descriptor (cmd = F_DUPFD ).

(2) GET/set the file descriptor mark (cmd = F_GETFD or F_SETFD ).

(3) GET/set the File status flag (cmd = F_GETFL or F_SETFL ).

(4) Obtain/set the asynchronous I/O ownership (cmd = F_GETOWN or F_SETOWN ).

(5) Obtain/set the record lock (cmd = F_GETLK, F_SETLK, or F_SETLKW ).

First, we should explain that these 10 kinds of cmd are worth the first seven (the last three are described when talking about record locks ). We will involve the file descriptor flag associated with each file descriptor in the progress table item, and the file status flag in each file table item.

F_DUPFD: copy the file descriptor filedes. The new file descriptor is returned as the function value. It is the minimum value of each value in the unopened descriptor that is greater than or equal to the value of the third parameter value (take as an integer value. The new descriptor shares the same file table item with filedes. However, the new descriptor has its own set of file descriptor flag, and its FD_CLOEXEC file descriptor flag is cleared (this indicates that the descriptor remains valid when it passes through an exec ).

F_GETFD corresponds to the file descriptor flag of filedes and is returned as a function value. Currently, only one file descriptor flag FD_CLOEXEC is defined.

F_SETFD sets the file descriptor flag for filedes. The new flag value is set based on the third parameter (taking it as an integer value.

Note: It should be noted that many existing programs involving the file descriptor flag do not use the constant FD_CLOEXEC, but set this flag to 0 (by default, it is not disabled during exec) or 1 (disabled during exec ).

F_GETFL corresponds to the file status flag of filedes and is returned as a function value. The file status flag is shown in Table 3-3: (in Table 3-3, each flag occupies only one of the three access methods (O_RDONLY, O_WRONLY, and O_RDWR, other flags occupy a bit of the file status flag respectively)

<Fcntl. h> argc, * (argc! = (Val = fcntl (atoi (argv []), F_GETFL,) <, atoi (argv [(val & defined (O_SYNC) (val &! Defined (_ POSIX_C_SOURCE) & defined (O_FSYNC) (val &

Note that we have used the function to test the macro _ POSIX_C_SOURCE and compiled the file access flag not defined in POSIX.1.

Exercise caution when modifying the file descriptor flag or file status flag. first obtain the existing flag value, modify it as needed, and then set a new flag value. You cannot just execute the F_SETFD or F_SETFL command to close the previously set flag.

Program listing 3-5 open one or more File status signs for a file descriptor

[root@localhost apue]# cat prog3-<fcntl.h> fd,  flags) ((val = fcntl(fd, F_GETFL, )) < |= flags;           (fcntl(fd, F_SETFL, val) < 

If you change the intermediate statement:

val &= ~flags;    

It constitutes another function, which we call clr_fl.

Compare the fsync, fdatasync, and O_SYNC flags. fsync and fdatasync update the file content as needed. The O_SYNC flags update the file content each time we write to the file.

Fcntl necessity: Our program operates on a descriptor (standard output), but does not know the corresponding file name opened by shell. Because this is opened by shell, you cannot set the O_SYNC flag as required. Fcntl allows you to modify its nature only when the file descriptor is opened.

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.