Linux-function-fcntl (i);

Source: Internet
Author: User
Tags flock

Name------------Fcntl

int fcntl (int fd, int cmd, .../*arg*/)----------------The manipulate file descriptor/* action Descriptor */

To include a header file:

#include <unistd.h>

#include <fcntl.h>

The function performs one of the operations described below on the open file descriptor fd,the operation are determined by Cmd

The/* function performs the following described actions on the open file descriptor, which is determined by the parameter cmd (command) */

The function can take an optional third argument. Whether or not the argument is required are determined by CMD.

The/* function accepts the third optional parameter, whether the parameter needs to be determined by the specified parameter cmd.

The required argument types is indicated parentheses all cmd name,or void is specified if the argument are not Requi Red.

/* The type of the required parameter is indicated by the parentheses following the name of each parameter cmd, which is specified if the parameter is not required void*/

/*******************************************************************************************************/

/***cmd***/

--------------------------------------------------------------------------------------------------------------- ---------------------------------------------

Duplicating a file descriptor/* copy the descriptor */

F_DUPFD (Long)

Find the lowest numberd available file descriptor greater than or equal to ARG and make it is a copy of FD.

This was diferent from Dup2 (2), which uses exactly the descriptor specified

/* Find a file descriptor with a minimum number that is greater than or equal to the parameter arg and as a copy of the parameter FD, which differs from the function dup2 () requires an exact descriptor that has been specified */

F_dupfd_cloexec (Long)

As for F_dupfd,but additionally set, the CLOSE-ON-EXEC flag for the duplicate descriptor. Specifying this flag permits a program

To avoid a addtional fcntl () F_SETFD operation to set the FD_CLOEXEC flag. For a explanation of why this flag is useful,see the

Description of O_cloexec in open (2)

/* Same as F_DUPFD, but additional close-on-exec tags are set for the copy descriptor. Specifying this flag allows a program to avoid an additional fcntl () function f_setfd

(parameter cmd) action to set the FD_CLOEXC tag, as to why this tag is available, see the description of the o_cloexec tag in the open () function.

Simple example:

int main ()

{

int fd1 = open ("B.txt", o_creat| o_rdwr| o_excl,0666);

/* At this time generally speaking FD = 3, 0,1,2 for Linux system occupancy */

int newfd1 = Fcntl (FD1, f_dupfd,5);

/* At this time generally NEWFD1 = 5 */

int newfd2 = Fcntl (FD1, f_dupfd,5);

/* At this time NEWFD2 = 6, because the smallest available number is greater than or equal to 5 */

int newfd3 = Fcntl (FD1, f_dupfd_cloexec, 1);

/* Should NEWFD3 = 4, greater than or equal to 1 and the smallest not occupied number */

return 0;

}

--------------------------------------------------------------------------------------------------------------- ----------------------

File Descriptor Flags/* Document DESCRIPTOR tags */

The following commands manipulate the flags associated with a file descriptor. Currently, only one such the flags is defined:

Fd_cloexec,the close-on-exec flag. If the fd_cloexec bit is 0, the file descriptor would remain open across an EXECVE (2), otherwise

It'll be closed

/* The following command manipulates the tag associated with the file descriptor, and currently only one tag is defined: Fd_cloexec. If the FD_CLOEXC bit is 0, the file descriptor is in the EXECVE () series function

is still available in the open, otherwise it will be closed */

F_getfd (void)

Read the file descriptor flags; ARG is ignored;

Read file descriptor tags, parameters are ignored

F_SETFD (Long)

Set The file descriptor flags to the value specified by ARG;

Set the file descriptor token to the value specified by the parameter arg

--------------------------------------------------------------------------------------------------------------- --------------------------------

File status Flags/* files state tags */

Each open file description have certain associated status flags, initialized by open () and possibly modified by fcntl (). Duplicated file

Descriptors refer to the same open file description, and thus share the same file status flags

The file status flags and their semantics is described in open ();

/* Each open File table must have an associated status token, which is initialized by the open function and may be modified by the FCNTL function. The copied file descriptor points to the same open file table

So share the same file status flag */

F_GETFL (void)

Read the file status flags; ARG is ignored;

Read the file status token, ignoring the parameter arg ( Note: The o_creat tag is a one-time token and cannot be obtained by the operation);

F_SETFL (Long)

Set The file status flags to the value specified by arg. File access mode (o_rdonly, o_wronly, O_RDWR) and file Creation

Flag (O_creat, O_EXCL, O_noctty,o_trunc) in Arg is ignored, on Linux This command can only change the O_append,o_async,

O_direct,o_noatime,and O_nonblock flags.

/* Set the file status tag to the value specified by the parameter, the file access mode and the file creation tag are ignored, in the Linux system this command only changes ... Mark */

Simple example:

int main ()

{

int fd = open ("B.txt", o_creat| o_excl| o_rdwr,0666);

Long flags = FCNTL (FD, F_GETFL);

/* At this time flags = O_EXCL | O_rdwr, the o_creat tag bit becomes 0,*/after the open function is executed

int res = FCNTL (FD, F_SETFL, O_append | o_wronly | O_TRUNC);

Flags = FCNTL (FD, F_GETFL);

/* At this time the flags are only more o_append bit o_wronly,o_trunc are ignored and cannot be modified */

}

--------------------------------------------------------------------------------------------------------------- --------------------------------------------

Advisory locking (emphasis)/* access Lock (emphasis) */

F_getlk, F_setlk and f_setlkw is used to acquire, release, and test for the existence of record locks (also know as file -segment

or file-region Lock), the third argument, lock, is a pointer to a structure the have at least the following fileds (in U Nspecified order)

/* f_getlk, F_SETLK and f_setlkw are used to apply, release, test an existing record lock (such as known file segment lock, file domain lock), and the third parameter lock is to point to a at least

Structures with the following areas */

struct flock{

...

Short L_type; Type of Lock:f_rdlck,f_wrlck,f_unlck;

Short l_whence; How to Interpert l_start:seek_set,seek_cur,seek_end file SEEK location

off_t L_start; Offset of starting offset for lock lock

off_t L_len; Number of bytes to lock bytes to lock

pid_t L_pid;               PID of Process blocking our lock (f_getlk only); The process of blocking our locks

...};

The L_start,l_start,,and l_len fields of this structure specify the range of bytes we wish to lock, bytes past the end of File my be locked

, but not bytes before the start of the file.

/* The L_start,l_whence,l_len area of the struct constructs the range we want to lock, the byte that crosses the end of the file may be lock, but the byte before the beginning of the file does not */

In the final cases, L_start can is a negative number provided the offset does not lie before the start of the file

/* In the last second case, L_start may be a negative number that provides a position offset from the beginning of the file */

Specifying 0 for L_len have the special Meaning:lock all bytes staring at the location specified by l_whence and L_start th Rough to

To the end of file, no matter how large the file grows.

/* Specifying the parameter L_len to 0 has special meaning: lock all positions from L_whence and L_start to the end of the file, regardless of the file selection area */

F_setlk (struct flock*)

Aquire a lock (when L_type was f_rdlck ro f_wrlck) or release a lock (when L_type was F_unlck) on the bytes specified by the L _whence,l_start,and l_len fields of lock. If a conflicting lock is held by another process,this call return-1 and sets errno to Eacces or

EGAIN

/* Request a lock (when L_type is F_rdlck or F_wrlck), or release a lock (L_type is F_unlck), when lock is conflicting with another process occupancy, this call returns-1

Also set error errno to Eacces or egain*/

F_SETLKW (struct flcok*)

As for F_setlk,but if a conflicting lock was held on the file and then wait for that lock to be release. If a signal is caught while waiting.

Then the call is interrupted and returns immediately

/* with F_SETLK, also in the file lock conflict generated, this time will wait for lock to be released, if the wait to receive a signal processing, the call will be interrupted, and immediately return */

F_getlk (struct flock*)

On input to this call, lock describes a-lock we would like-to-place on the file. If the lock could is placed, fcntl () does not actually

Place it, but returns F_unlck in the L_type field of lock and leaves the other fields of the structure unchanged. If one or more incompatible

Locks would prevent this lock being placed, then FCNTL () returns details on one of the these locks in the L_type, L_whence, L_start,andl_len fields

of lock and set L_pid to is PID of the process holding that lock

/* In this invocation input, the parameter lock describes a lock information we want to get in the file, and if the lock information can be obtained, the FCNTL () function does not actually get it, but instead returns the F_UNCLK in the l_type of the parameter lock without changing the other values in the struct. If one or more conflicting lock presence will prevent lock information from being fetched, then the FCNTL () function returns the details of these locks and sets the pid*/of the process that owns the lock

Linux-function-fcntl (i);

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.