Apue (3)---file I/O (3)

Source: Internet
Author: User

12. function sync, Fsync and Fdatasync

Deferred write: Traditional UNIX systems have buffers or page buffers in the kernel, most disk I/O are through buffers, and when we write data to a file, the kernel usually first copies the data to the buffer, then queues it, and writes it to disk later. The UNIX system provides three functions for sync, Fsync, and Fdatasync.

#include <unistd.h>int  fsync (INF FD); int fdatasync (int  FD); // If successful, returns 0; If an error occurs, return-1 void sync (void);

Sync simply queues all the modified block buffers into the write queue and then returns, not waiting for the actual disk operation to end. The favorite daemon, often called update, periodically calls the Sync function, which ensures that the block buffers of the flush kernel are regularly flushed.

The Fsync function only works on one of the files specified by the kernel file descriptor fd, and waits for the write disk operation to end before returning, the main scenario being the in-database application.

The Fdatasync function resembles Fsync, but he responds to the data portion of the file. In addition to the data, Fsync also updates the properties of the file.

13. Function Fcntl

#include <fcntl.h>int fcntl (intint cmd, ... /* int arg */ ); // If successful, it depends on CMD; 1 if an error occurs

The FCNTL function can change the properties of a file that has been opened with the following 5 features:

1. Copy an existing descriptor (cmd = F_DUPFD or f_duped_cloexec)

2. Get, set file descriptor flags (cmd = F_GETFD or F_SETFD)

3. Get, set file status flag (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_setlkm)

F_DUPFD: Copy file descriptor fd, new file descriptor as function return value, which is not yet open in each descriptor greater than or equal to the third parameter value returned. The new descriptor and FD share a file table entry, but the new file descriptor is marked by his own set of file descriptor files. Its fd_cloexec file descriptor flag is cleared.

F_duped_cloexec: Copies the file descriptor, setting the value of the Fd_cloexec file descriptor flag associated with the new descriptor.

F_GETFD: The file descriptor flag corresponding to FD is returned as a function value. Only one file descriptor flag is currently defined Fd_cloexec

F_SETFD: Set file descriptor flags for FD. The new flag value is set by the third parameter.

F_GETFL: The file status flag corresponding to FD is returned as a function value, and its return value is the same as the open flag.

F_SETFL: Sets the file status flag to the value of the third parameter: Several flags that can be changed are: O_append, O_nonblock, O_sync, O_dsync, O_rsync, O_fsync, and O_async

F_fetown: Gets the process or process group ID that currently accepts Sigio and Sigurg signals

F_setown: Sets the process ID or process group ID of the Sigio and Sigurg signals. Positive ARG specifies a process ID, and the negative arg specifies a process group ID.

Apue (3)---file I/O (3)

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.