Fcntl () Functional explanation

Source: Internet
Author: User

The Fcntl () function can change the nature of an open file
<pre lang= "C" escaped= "true" >
#include <unistd.h>
#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);

</pre>

Fcntl () is used to manipulate some of the characteristics of a file description word. The parameter fd represents the file descriptor to be set, and the parameter cmd represents the instruction to be manipulated.

There are 5 functions of the CNTL function:
1. Copy an existing descriptor (CMD=F_DUPFD).
2. Get/Set the file descriptor tag (CMD=F_GETFD or F_SETFD).
3. Get/Set the file status tag (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).

There are several situations:
<ol>
The <LI>F_DUPFD is used to find the smallest and still unused file descriptor that is greater than or equal to the parameter arg, and copies the file descriptor of the parameter FD. Successful execution returns the newly copied file descriptor. Please refer to dup2 (). F_GETFD obtained close-on-exec flag. If the fd_cloexec bit of this flag is 0, the file will not be closed when the exec () related function is called. </li>
<LI>F_SETFD set close-on-exec flag. The flag is determined by the fd_cloexec bit of the parameter arg. </li>
<LI>F_GETFL Gets the file descriptor status flag, which is the parameter of the open () flags. </li>
<LI>F_SETFL sets the file descriptor status flag, the parameter arg is the new flag, but only the o_append, O_nonblock, and o_async bits are allowed to change, and the other bit changes will not be affected. </li>
<li>f_getlk gets the status of the file lock. </li>
<LI>F_SETLK sets the state of the file lock. At this point the L_type value of the FLCOK structure must be f_rdlck, F_wrlck, or F_unlck. If the lock cannot be established, 1 is returned, and the error code is eacces or Eagain. <li>
<LI>F_SETLKW the F_SETLK function is the same, but the lock cannot be established, the call waits until the lock action succeeds. If the signal is interrupted while waiting for the lock, it returns 1 immediately and the error code is EINTR. The parameter lock pointer is a flock structure pointer, defined as follows <li>

</ol>

<pre lang= "C" escaped= "true" >
struct FLCOK
{
short int l_type;
short int l_whence;
off_t L_start;
off_t L_len;
pid_t L_pid;
};
</pre>


There are three states of L_type:

<ol>
<li>f_rdlck creating a lock for reading </li>
<li>f_wrlck creating a lock for write-in </li>
<li>f_unlck Delete a previously established lock </li>
</ol>

There are three ways of L_whence:
<ol>
<li>seek_set begins with the start of the file as the starting position of the lock. </li>
<li>seek_cur starting position with the current file read/write location locked </li>
<li>seek_end the starting position of the lock with the end of the file. </li>
</ol>

Returns 0 if the return value succeeds, or 1 if there is an error, and the reason for the error is stored in errno.

Fcntl () Functional explanation

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.