Fcntl function usage in Linux

Source: Internet
Author: User
Tags flock

Function Description: describes the features of a file based on its description.
File control functions
Fcntl -- file control

Library
Standard C library (libc,-LC)

Synopsis
# Include <fcntl. h>;
Int fcntl (int fd, int cmd );
Int fcntl (int fd, int cmd, long Arg );
Int fcntl (int fd, int cmd, struct flock * Lock );

[Description]
Fcntl () provides control for (File) descriptors. The FD parameter is the descriptor operated by the CMD parameter (as described below.
For the CMD value, fcntl can accept the third parameter int ARG

Fcntl functions have five functions:
1. Copy an existing Descriptor (cmd = f_dupfd ).
2. Get/set the file descriptor mark (cmd = f_getfd or f_setfd ).
3. Get/set the File status mark (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 ).

CMD value:
F_dupfd returns a (File) descriptor as described below:
An available descriptor with the smallest O value greater than or equal to Arg
O reference of an object like the original operator
O if the object is a file, a new descriptor is returned, which shares the same offset with Arg)
O same access mode (read, write or read/write)
O the same file status flag (for example, two file descriptors share the same status flag)
O The close-on-exec flag combined with the new file descriptor is set to cross-type access to execve (2) system calls


F_getfd gets the close-on-exec Mark combined with the file descriptor FD, similar to fd_cloexec. if the return value and fd_cloexec are 0 and the calculation result is 0, the file accesses exec () in a cross-cutting manner. Otherwise, the file will be closed (Arg is ignored) if it runs through exec)


F_setfd: Set the close-on-exec flag. The flag is determined by the fd_cloexec bit of the ARG parameter.

F_getfl indicates the File status of FD, as described below (Arg is ignored)


F_setfl is set to the ARG descriptor status flag. The following flags can be changed: o_append, o_nonblock, o_sync, and o_async.



F_getown obtains the process ID or group ID that is currently receiving sigio or sigurg signals. The process group ID returns a negative value (Arg is ignored)


F_setown indicates the process ID or process group ID that receives sigio and sigurg signals. The process group ID is described by providing negative Arg values. Otherwise, Arg is considered as the process ID.


The command characters (CMD) f_getfl and f_setfl are described as follows:
O_nonblock non-blocking I/O; if the read (2) call does not have readable data, or if the write (2) operation is blocked, the read or write call returns the-1 and eagain errors
The o_append forces each write operation to be added to the end of a large file, which is equivalent to the o_append flag of open (2 ).
O_direct minimizes or removes the impact of reading and writing cache. the system will attempt to avoid caching your read or write data. if the cache cannot be avoided, it will minimize the impact of cached data. if this sign is not used well enough, it will greatly reduce the performance
O_async allows sigio signals to be sent to the process group when I/O is available. For example, when data can be read

Exercise caution when modifying the file descriptor flag or file status flag. first obtain the current flag value, modify it as needed, and then set a new flag value. You cannot just execute the f_setfd or f_setfl command to close the previously set flag.

The return value of fcntl is related to the command. If an error occurs,-1 is returned for all commands. If the result is successful, another value is returned. The following three commands return values: f_dupfd, f_getfd, f_getfl, and f_getown. The first returns the new
File descriptor, the second returns the corresponding flag, and the last returns a positive process ID or a negative process group ID.

The routine for FD control is as follows:

# Include
# Include
# Include
# Include
Using namespace STD;

Int main (INT argc, char * argv [])
{
Int FD, VAR;
// FD = open ("new", o_rdwr );

If (argc! = 2)
{
Perror ("--");
Cout <"enter a parameter, that is, the file name! "<


/** The three accesskeys (o_rdonly, o_wronly, and o_rdwr) do not each occupy one place. (The values of these three flags are respectively 0, 1, and 2 due to historical reasons. These three values are mutually exclusive-one file can only have one of these three values .) Therefore, you must first use the blocked word o_accmode to obtain the access mode bit and then compare the result with the three values.

****/

Switch (VAR & o_accmode)
{
Case o_rdonly: cout <"read only..." <

. Obtain/set the record lock function: (cmd = f_getlk, f_setlk or f_setlkw ).

F_getlk uses the third parameter Arg (a struct pointing to flock) to obtain the first lock that blocks the lock description. the obtained information will overwrite the information transmitted to the fcntl () flock structure. if no lock can be generated to prevent this lock (flock), this structure will not be changed unless the lock type is set to f_unlck.


F_setlk sets or clears a file segment Lock Based on the lock information described by the third parameter Arg pointing to the flock pointer of the struct. f_setlk is used to implement the shared (or read) Lock (f_rdlck) or exclusive (write) Lock (f_wrlck). You can also remove these two locks (f_unlck ). if the shared or exclusive lock cannot be set, fcntl () will return immediately to eagain.


F_setlkw except for shared or exclusive locks that are blocked by other locks, this command is the same as f_setlk. if the shared or exclusive lock is blocked by another lock, the process will wait until the request can be completed. when fcntl () is waiting for a certain area of the file to capture a signal, if this signal is not specified sa_restart, fcntl will be interrupted.


When a shared lock is set to a certain segment of a file, other processes can set shared locks to this segment or a part of this segment. share any part of the protected area where any other process set is blocked from being exclusively locked. if the file descriptor is not opened in read access mode, the request for setting the shared lock will fail.


The exclusive lock prevents any other process from setting a shared lock or exclusive lock anywhere in this protected area. If the file descriptor is not opened in write access mode, the exclusive lock request will fail.

Flock pointer of struct:

Struct flcok
{
Short int l_type;/* locked status */

// These three parameters are used to lock the entire file in segments. If the entire file is locked, l_whence = seek_set, l_start = 0, l_len = 0;
Short int l_whence;/* determines the Rochelle start position */
Off_t l_start;/* start position of the locked area */
Off_t l_len;/* size of the locked area */


Pid_t l_pid;/* Lock action process */
};


Rochelle type has three statuses:
F_rdlck creates a lock for reading
F_wrlck creates a lock for writing
F_unlck Delete the previously created lock


Rochelle whence also has three methods:
Seek_set starts with a file and is the starting position of the lock.
Seek_cur uses the current file read/write location as the starting position of the lock
The start position of seek_end lock is the end of the file.

If the return value is successful, 0 is returned. If an error exists,-1 is returned. The error cause is stored in errno.

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.