Embedded Linux system Programming (ii)--File descriptor control function fcntl

Source: Internet
Author: User

embedded Linux system Programming (ii)--File Descriptor control function fcntl

Because the fcntl function is too flexible and complex, the fcntl function is listed separately from the file IO , which is easy to read in detail. The function prototypes are as follows:

#include <unistd.h>

#include <fcntl.h>

int fcntl (int fd, int cmd, ... */arg */);

    fcntl fd, control of the file descriptor is performed by the cmd control command to control, arg parameter is optional, do you need arg parameter depends on control command cmd fcntl () The return value of control command related. If an error occurs, all commands are returned-1

Common control commands are as follows:

A, F_DUPFD

find a greater than or equal the smallest available file descriptor for Arg as a copy of FD , the difference between this usage and the dup2 function is dup2 The function can specify the file descriptor precisely. Success returns a new file descriptor.

   // equivalent to dup (OLDFD)

 
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP, &NBSP; fcntl (OLDFD, F_DUPFD, NEWFD) // new returned file descriptor greater than or equal to NEWFD

// equivalent to dup2 (OLDFD, NEWFD); // The newly returned file descriptor equals newfd

B, f_dupfd_cloexec

Same as f_dupfd , but adds a set close-on-exec identity. that is, when the call to the exec() function succeeds, the file descriptor is automatically closed .

C, F_GETFD

Gets the file descriptor identifier, ARG can be ignored. If the obtained file descriptor identifies the fd_cloexec phase with a file descriptorthat is equal to 0, the file remains cross-access exec ( ), or the file is closed.

D, f_setfd

Set file descriptor identifies a value of arg

     settings close-on-exec flag with parameter arg fd_cloexec bit determines that many existing programs involving file descriptor flags do not use constants fd_cloexec 0 ( exec ) 1 ( in exec ) .

caution must be taken when modifying the file descriptor flag or file status flag to obtain the current flag value, then modify it as desired, and finally set the new flag value. You cannot just execute the f_setfd or f_setfl command, which turns off the previously set flag bit.

E, F_GETFL

gets the file status ID, arg ignores

F, F_SETFL

sets the value of the file status ID to arg, file access mode (o_rdonly,o_wronly, O_rdwr) and file creation identification (o_creat, O_excl, O_noctty, O_trunc ) is ignored and can only change o_append, O_async, O_direct, O_noat-ime, and o_nonblock identification.

G,f_getown

get current in file description character the process of receiving a SIGIO or Sigurg event signal on FD ID or process group , ARG is ignored.

H, F_setown

set in file description character process of receiving Sigio or Sigurg event signals on FD ID or process group ID , and the value is arg.

I,f_getsig

get the input and output signals that can be made , returning 0 indicates that SIGIO has been sent, andarg is ignored.

J,f_setsig

Set input and output signals to be made , the value is arg, and if arg is 0 , the SIGIO signal is sent by default .

K,f_getlk

Get file lock information , Arg is a struct flock *lock, which is a pointer to the file lock structure.

L, F_setlk

Set file lock

M, f_getlease

Gets the lease,Arg ignores

N,f_setlease

Sets and deletes a file lease based on the value of ARG.

In general, thearg parameter is ignored when the state information is obtained, and arg is the parameter value when the modification information is set .

The Fcntl function is too profound, this article only explains the Man Handbook, the concrete application examples will be presented in the following.


This article from "Endless life, Struggle not only" blog, reprint please contact the author!

Embedded Linux system Programming (ii)--File descriptor control function fcntl

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.