Linux environment Programming file I/O (v): fcntl function

Source: Internet
Author: User

Introduction:

For an ordinary file, we can think of the operation of it, read the contents of the file, write the data to the file, these are mentioned in the previous read, write function. In addition, you can also get other properties of the file, and modify these properties, such as the file descriptor, file descriptor tags, file status flags and so on. These modifications to the nature of the file are done by the FCNTL function.


Function Description:

#include <unistd.h>
#include <fcntl.h>
int fcntl (int fd, int cmd, .../* arg */);

Parameters:

FD: The file descriptor that you want to set.

CMD: An instruction intended to operate on a file.

ARG: In general, this parameter is an integer that points to a pointer to a structure when a record lock is used.

Function: According to the different cmd, the FCNTL function has the following 5 functions:

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

2. Get/Set File descriptor tag (cmd = F_GETFD or F_SETFD).

3. Get/Set File status flag (cmd = F_GETFL or F_SETFL).

4. Get/Set asynchronous I/o ownership (cmd = F_getown or F_setown).

5. Get/Set record lock (cmd = f_getlk, f_setlk or f_setlkw).


Detailed Description:

1, F_DUPFD

Copy the file descriptor FD. The new file descriptor is returned as a function value. It is the smallest value in a descriptor that has not been opened that is greater than or equal to the third parameter (taken as an integer value). The new descriptor shares the same file table entry as the FD. However, the new descriptor has its own set of file descriptor flags, whose FD_CLOEXEC file descriptor flags are cleared. In fact, at this point the function of the FCNTL function is similar to the function of dup2.

/* * File NAME:FUPFD.C * Author    : libing * Mail      : [email protected] * Function  : Just for a test */#include <stdio.h> #include <stdlib.h> #include <fcntl.h>intmain (void) {int FD, NEWFD;FD = open ("Test.txt", O_ RDWR); if (fd = =-1) {printf ("can ' t open test.txt.\n"); exit (1);}        Use the FCNTL function to copy the file descriptor NEWFD = Fcntl (FD, F_DUPFD, 5);p rintf ("NEWFD =%d.\n", NEWFD); return 0;}
Compile test:

Compiler: GCC FUPFD.C Execute program:./a.out test Result: NEWFD = 5.
2. F_GETFD: The file descriptor flag corresponding to FD is returned as a function. Only one file descriptor flag Fd_cloexec is currently defined.

F_SETFD: Set file descriptor flags for FD. The new flag is set by the third parameter.

3. F_GETFL: The file status flag for FD is returned as a function value. File status flags are O_rdonly, o_wronly, O_RDWR, and so on. Because three access way flags do not account for 1 bits. Therefore, you must first obtain the access mode bit with the masked word o_accmode, and then compare the result with any of these three values.

F_SETFL: Sets the file status flag to the value of the third parameter. Several flags that can be changed are: O_append, O_nonblock, O_sync, O_dsync, O_rsync, and so on.

#include <fcntl.h> #include <stdio.h> #include <stdlib.h>intmain (void) {int Val;int fd;fd = open (" Test.txt ", O_RDWR);        The FCNTL function obtains the file status flag if (val = fcntl (fd, F_GETFL, 0)) < 0) printf ("Fcntl failed.\n"); switch (val & o_accmode) {case O_ rdonly:printf ("Read Only"), Break;case o_wronly:printf ("Write only"), Break;case o_rdwr:printf ("Read and write"); ;d efault:printf ("Unknown access Mode"); if (Val & o_append) printf ("APPEND"), if (Val & o_nonblock) printf ("Nonblock");p Utchar (' \ n '); exit (0);}
Compile test results:

Compiler: GCC fcntl.c Execute program:./a.out results display: Read and write
4. F_getown: Take the process ID or process group ID that currently receives the Sigio and Sigurg signals.

F_getown: Sets the process ID or process group ID that receives the Sigio and Sigurg signals.






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.