Next: getrlimit and setrlimit: Resource
Up: Linux System Calls
Previous: fcntl: Locks and Other
Contents
- On most operating systems, when you write to a file, the data is not immediately written to disk. Instead, the operating system caches the written data in a memory buffer, to reduce the number of required disk writes and improve program responsiveness.
- When the buffer fills or some other condition occurs (for instance, enough time elapses), the system writes the cached data to disk all at one time.
- However, this behavior can make programs that depend on the integrity of disk-based records unreliable.
- For example, suppose that you are writing a transaction-processing program that keeps a journal file.
- The journal file contains records of all transactions that have been processed so that if a system failure occurs, the state of the transaction data can be reconstructed.
- It is obviously important to preserve the integrity of the journal file whenever a transaction is processed, its journal entry should be sent to the disk drive immediately.
- To help you implement this, Linux provides the fsync system call.
- The fsync call doesn't return until the data has physically been written.
- The http://siber.cankaya.edu.tr/SystemsProgramming/cfiles/write_journal_entry.c function in Fig. 8.3 illustrates the use of fsync. It writes a single-line entry to a journal file.
Figure 8.3:
Write and Sync a Journal Entry.
|
- The fsync system call enables you to force a buffer write explicitly. You can also open a file for synchronous I/O, which causes all writes to be committed to disk immediately.
To do this, specify the O_SYNC flag when opening the file with the open call.
Next: getrlimit and setrlimit: Resource
Up: Linux System Calls
Previous: fcntl: Locks and Other
Contents
Cem Ozdogan
2007-05-16