Unix programming learning notes (5) -- The nature of opened files accessed by the fcntl function of file I/O

Source: Internet
Author: User

Lien000034
2014-08-29

The fcntl function can change the nature of opened files.

# Include <fcntl. h>

Int fcntl (INT filedes, int cmd,.../* int Arg */);

Fcntl functions have five functions:

1. Copy an existing Descriptor (cmd = f_dupfd ).

2. Get/set the file descriptor flag (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 ).

The first five cmd values are described below.

(1) f_dupfd

Copy the file descriptor filedes. The new file descriptor is returned as the function value. The difference with DUP functions is:The new file descriptor returned by the DUP function must be the smallest among unopened descriptors, while the new file descriptor returned by the fcntl function is the minimum value greater than or equal to the values of the third parameter. That is, DUP (filedes) is equivalent to fcntl (filedes, f_dupfd, 0).

(2) f_getfd

Obtain the file descriptor flag of filedes and return it as a function value. Currently, only one file descriptor flag fd_cloexec (fd_cloexec is the "close at execution" flag. This flag is set to automatically disable this descriptor when an exec is executed. This flag is automatically cleared when a file descriptor is copied, whether DUP function or fcntl function is used .)

(3) f_setfd

Set the filedes file descriptor flag.

(4) f_getfl

Obtain the File status flag of the file descriptor filedes and return it as a function value. For the File status mark, see table 1,

File status flag Description
O_rdonly Read-only access
O_wronly Write-only open
O_rdwr Open for read and write
O_append Append at each write
O_nonblock Non-Blocking Mode
O_sync Wait until the write is completed (data and attributes)
O_dsync Wait until write is completed (data only)
O_rsync Synchronous read and write

The three access method signs (o_rdonly, o_wronly, and o_rdwr) do not each occupy one place. The values of these three access methods are 0, 1, and 2, respectively. Therefore, you must use the blocked word o_accmode to obtain the access mode bit, and then compare the result with any of the three values.

(5) f_setfl

You can set the File status flag to the value of the third parameter (take the value as an integer) with the following labels: o_append, o_nonblock, o_sync, o_dsync, and o_rsync.

Example:

The first parameter of the program below specifies the file descriptor and prints the description of the selected file status flag for this descriptor.

#include <stdlih.h>#include <stdio.h>#include <fcntl.h>intmain(int argc, char *argv[]){    int val;    if (argc != 2) {        printf("usage: a.out <descriptor#>");        exit(-1);    }    if ((val = fcntl(atoi(argv[1]), F_GETFL, 0)) < 0) {        printf("fcntl error for fd %d", atoi(argv[1]));        exit(-1);    }    switch (val & O_ACCMODE) {    case O_RDONLY:        printf("read only");        break;    case O_WRONLY:        printf("write only");        break;    case O_RDWR:        printf("read write");        break;    default:        err_dump("unknown access mode");        break;    }    if (val & O_APPEND) {        printf(", append");    }    putchar(‘\n‘);    exit(0);}

Compile the program, generate the flprinter, and then run the file,

lienhua34:examples$ ./flprinter 0 < /dev/ttyread onlylienhua34:examples$ ./flprinter 1 > temp.foolienhua34:examples$ cat temp.foowrite onlylienhua34:examples$ ./flprinter 2 2>>temp.foowrite only, appendlienhua34:examples$ ./flprinter 5 5<>temp.fooread write

(Done)

Unix programming learning notes (5) -- The nature of opened files accessed by the fcntl function of file I/O

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.