Linux,unix has buffer cache or page caching in the kernel, most disk I/O is buffered and delayed write technology is used.
Sync: Put all the modified cache into the write queue, then return, do not wait for the actual write disk operation to end
Fsync: Only work with a single file with a file descriptor, and wait for some disk operations to end and then return.
Fdatasync: Similar to Fsync, but it only affects the data portion of the file. Fsync also synchronizes the properties of the updated file.
Fflush: Standard I/O functions (such as: fread,fwrite) create buffers in memory, which refreshes the memory buffer, writes the contents to the kernel buffer, and calls Fsync to write to the disk. (Call Fsync before calling Fflush, otherwise it will not work)
---------------------------------------------------------------------------
Mainly involves Setbuf, Fflush, Fsync,sync and other functions.
First, enter the buffer for the output library.
The three types of buffering available are unbuffered, block buffered, and line buffered. When a output stream is unbuffered, information appears on the destination file or terminal as soon as written; When it is
Block buffered many characters are saved up and written as a block; When it's line buffered characters are
Saved up until a newline is output or input are read from No stream attached to a terminal device (typically
stdin). The function fflush (3) May is used to force the blocks out early. (Fclose (3).) Normally all files
are block buffered. When the i I/O operation occurs on a file, malloc (3) is called, and a buffer is
Obtained. If A stream refers to a terminal (as stdout normally does) it are line buffered. The standard error
Stream stderr is always unbuffered by default.
In general, block buffered is more efficient and combines multiple operations into one operation. Now a part of the standard library is cached,
The update operation will be performed until the buffer is full, or when the program displays a call to Fflush.
Setbuf, however, can set the size of the buffer.
#include <stdio.h>
void Setbuf (FILE *stream, char *buf);
This function should be called before how the output is written to the file. Usually put in main in front of the statement.
But Setbuf has a classic mistake, as mentioned in The Man Handbook, C pitfalls and flaws.
You are must make sure this both BUF and the space it points to still exist by the time stream is closed, which also ha Ppens at the program termination. For example, the following is illegal:
#include <stdio.h>
int main ()
{
Char Buf[bufsiz];
Setbuf (stdin, buf);
printf ("Hello, world!/n");
return 0;
}
This program is wrong. BUF buffers should be last emptied after the main function is finished, the program returns control to the operating system
Part of the cleanup work that the C Run-time library must do before, but at this point the BUF character array has been released.
The modified method is to set the BUF to static or global, or call malloc to dynamically request memory.
char * malloc ();
Setbuf (Stdout,malloc (bufsize));
There is no need to judge the return value of the malloc, and if the malloc call fails, a null pointer will be returned, and Setbuf's second argument can
Is null and is not buffered at this time.
Correspondingly, the Fflush function refreshes the buffer and updates the contents of the buffer to the file.
#include <stdio.h>
int fflush (FILE *stream);
The function Fflush forces a write of all user-space buffered data for the given output or update stream via the Strea m underlying write function. The open status of the stream is unaffected.
If The stream argument is NULL, fflush flushes all open output streams.
But fflush only refreshes the buffer in the C library.
Some of the other data refreshes need to be called fsync or sync!!
Note this fflush () only flushes the user spaces buffers provided by the C library. To ensure which the data is physically stored on disk The kernel buffers must are flushed too, e.g. with Sync (2) or Fsyn C (2).
Fsync and sync eventually update the buffered data to the file.
#include <unistd.h>
int fsync (int fd);
Fsync copies all In-core parts of an a file to disk, and waits until the device reports this all parts are on stable. It also updates metadata stat information. It does not necessarily ensure this entry in the directory containing the file has also reached disk. For this explicit fsync on the file descriptor to the directory is also needed.
NAME
Sync-commit buffer cache to disk
Synopsis
#include <unistd.h>
void sync (void);
DESCRIPTION
Sync-I-commits inodes to buffers, and then buffers to disk.
ERRORS
This function is always successful.
Sync command sync directly calls the Sync function to update the buffer on the disk.
-------------------------------------------------------------------------------------------------------------