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
For the Open function O_cloexec mode, the FCNTL function fd_cloexec option is summarized as follows:
Calling the Open function O_cloexec mode opens a file descriptor that is closed in a new program that executes the exec call, and is an
Use the following method to set the socket to non-blocking mode:Int flags = fcntl (socket, F_GETFL, 0 );Fcntl (socket, F_SETFL, flags | O_NONBLOCK );Use the following method to set the socket to non-blocking mode:Int flags = fcntl (socket, F_GETFL, 0
Fcntl. h and unistd. h2008-08-22 22: 15fcntl. h define a lot of macro and open, fcntl function prototypeUnistd. h defines more function prototypes
Close (close the file)Header file # include Defines the int close (int fd) function );Function
Transfer from http://www.cnblogs.com/xuyh/p/3273082.htmlSet file flags with command F_GETFL and F_SETFL, such as blocking and non-blockingF_SETFL set to the ARG descriptor status flag, several flags that can be changed are: O_append,
How to operate a file that has been shared, that is, how to ensure the correctness of file data when multiple processes operate on the same file at the same time. Linux usually uses File Locking to avoid competition for shared resources.
File locks
There are two types of fcntl File locks:
Create locksAnd
Mandatory lock
The locks are defined as follows: every process that uses a lock file must check whether a lock exists. Of course, you must respect the existing locks. In general, the kernel
The Fcntl () function can change the nature of an open file#include #include int fcntl (int fd, int cmd);int fcntl (int fd, int cmd, long arg);int fcntl (int fd, int cmd, struct flock * lock);Fcntl () is used to manipulate some of the
FCNTL system CallExample 1: Status flags for a fileMyfcntl_access.c#include #include#include#include#include#include#include#defineERROR (flag)if(flag) {printf ("%d:", __line__); Fflush (stdout); Perror ("Error");
Use fcntl () to set the blocking nature of the blocking function. The fcntl Function
#include #include #include #include #include #include #include #define ERR_EXIT(m) \ do { \ perror(m);\ exit(EXIT_FAILURE);\ }while(0)void
1. DUP functionThe 1.1 DUP function is used to copy a file descriptor , and the copied file descriptor can be used normally (see Example 1). The DUP function returns one of the smallest available file descriptors in the current File descriptor table
Records lock record Locking
Function: When a process is reading or modifying a part of a file, it can prevent other processes from modifying the same file area.
BYTE range lock Byte-range locking
Second, history
Flock function, you can lock the
Fcntl system call]Function Description: describes the features of a file based on its description.Usage:Int fcntl (int fd, int cmd );Int fcntl (int fd, int cmd, long Arg );Int fcntl (int fd, int cmd, struct flock * Lock );Parameters:FD: file
int fcntl (int fd,int cmd,...)The function Fcntl provides a very rich feature. The main dependencies are the various parameters of CMD:
Copy an existing file descriptor F_dupfd,f_dupfd_cloexec
Gets the flag for the set file descriptor F_
The fcntl function can change the nature of opened files.
#include 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
In Linux, the functions used to implement File locks include lock and fcntl, where lock is used to apply a commit lock to the file, while fcntl can not only apply a mandatory lock, but also apply a commit lock. At the same time, fcntl can lock a
/DUP function: copy an existing handle to generate a new handle that is exactly the same as the "source handle feature" (that is, generate a new handle number, and associated with the same device)// Functions of the dup2 function: copy an existing
Fcntl (file lock)Header file # include # Include Function Definition int fcntl (int fd, int cmd, struct flock * Lock );Function Description FD: file descriptorThe configured file descriptor. The cmd parameter indicates the command to
getsockopt ()
Briefly:Gets a set of interface options.#include int PASCAL FAR getsockopt (SOCKET s, int level, int optname,Char far* optval, int far* optlen);S: A descriptive word that identifies the socket interface.Level: The
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,
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