Use the following method to set the socket to non-blocking intflagsfcntl (socket, F_GETFL, 0); fcntl (socket, F_SETFL, flags | O_NONBLOCK ); set the socket to non-blocking intflagsfcntl (socket, F_GETFL ,...
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 (socket, F_SETFL, flags | O_NONBLOCK );
You can use the non-blocking setting back to block
Int flags = fcntl (socket, F_GETFL, 0 );
Fcntl (socket, F_SETFL, flags &~ O_NONBLOCK );
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 description.
Cmd: Operation Command.
Arg: parameters used by commands.
Lock: Same as above.
The following commands are available:
1. F_DUPFD: copy the file description.
2. FD_CLOEXEC: Set the close-on-exec flag. If the FD_CLOEXEC bit is 0, the file remains open during execve. Otherwise, it is disabled.
III. F_GETFD: Mark of the description word used to read the file.
IV. F_SETFD: Set the description word mark of the file.
V. F_GETFL: indicates the file reading status.
VI. F_SETFL: Set the file status flag.
O_RDONLY, O_WRONLY, O_RDWR, O_CREAT, O_EXCL, O_NOCTTY, and O_TRUNC are not affected,
The possible flags include O_APPEND, O_ASYNC, O_DIRECT, O_NOATIME, and O_NONBLOCK.
VII. F_GETLK, F_SETLK, and F_SETLKW: Obtain, release, or test record locks. the following struct pointers are used:
F_SETLK: get the lock (F_RDLCK, F_WRLCK) or release the lock (F_UNLCK) within the specified byte range ). If a lock operation conflicts with another process,-1 is returned and errno is set to EACCES or EAGAIN.
F_SETLKW: behavior is like F_SETLK, except for sleeping and waiting when the lock cannot be obtained. If a signal is received while waiting, the system returns immediately and sets errno to EINTR.
F_GETLK: get the file lock information.
F_UNLCK: Release the file lock.
To set the read lock, the file must be opened as read. To set the write lock, the file must be opened as a write. To set the read/write lock, the file must be opened in read/write mode.
8. signal management
F_GETOWN, F_SETOWN, F_GETSIG, and F_SETSIG are used to obtain IO signals.
F_GETOWN: gets the process or group ID that receives the SIGIO or SIGURG event signal on the file description fd.
F_SETOWN: specifies the process or process Group ID that will receive SIGIO or SIGURG event signals on the file description fd.
F_GETSIG: obtains signals that can be performed on the input and output of an identifier.
F_SETSIG: Set the signal that can be performed by the input and output.
Most of the time, the program does not need to use select () or poll () to implement complete asynchronous I/O.
9. Lease (Leases)
F_SETLEASE and F_GETLEASE are used for the lease of the current process on the file. A file lease provides a mechanism to notify a process that owns a file lease when a process tries to open or break the file content.
F_SETLEASE: set or delete a file lease based on the following symbol values
1. F_RDLCK sets the read lease. when the file is opened or broken by another process, the current process with the lease will be announced.
2. F_WRLCK sets the write lease. when the file is opened or broken by another process in read or write mode, the current process with the lease will be announced.
3. F_UNLCK delete the file lease.
F_GETLEASE: Gets the lease type.
10. file or directory change notice
(Linux 2.4 or above) when a file in the directory or directory of the fd index changes, a notice will be issued to the process. The advertised events specified by the arg parameter are as follows. two or more values can be combined by the OR operation.
1. access the DN_ACCESS File (read, pread, readv)
2. the DN_MODIFY file is modified (write, pwrite, writev, truncate, ftruncate)
3. the DN_CREATE file is created (open, creat, mknod, mkdir, link, symlink, rename)
4. delete the DN_DELETE File (unlink, rmdir)
5. rename the DN_RENAME file)
6. the attributes of the DN_ATTRIB file are changed (chown, chmod, utime [s]).
Return description:
When a successful execution is performed, different operations return different values.
F_DUPFD: New File description
F_GETFD: flag value
F_GETFL: flag value
F_GETOWN: owner of the file description word
F_GETSIG: indicates the notification signal to be sent when the read/write function becomes available, or 0 indicates the traditional SIGIO behavior.
Returns 0 for other commands.
-1 is returned for failure, and errno is set to one of the following values
EACCES/EAGAIN: The operation is not allowed and is not feasible yet.
EBADF: invalid file description
EDEADLK: possible deadlock detected
EFAULT: the lock operation occurs outside the accessible address space.
EINTR: The operation is interrupted.
EINVAL: the parameter is invalid.
EMFILE: The process has exceeded the maximum available range of files.
ENOLCK: the lock has been exhausted.
EPERM: The power is not allowed.
Struct flock {
Short l_type;/* lock type: F_RDLCK, F_WRLCK, F_UNLCK */
Short l_whence;/* l_start field reference point: SEEK_SET (file header), SEEK_CUR (current file location), SEEK_END (file tail )*/
Off_t l_start;/* offset from the l_whence field */
Off_t l_len;/* length to be locked */
Pid_t l_pid;/* process ID (F_GETLK) of the file lock Currently obtained )*/
};