Sync (Unix)
Encyclopedia
sync is a standard system call
System call
In computing, a system call is how a program requests a service from an operating system's kernel. This may include hardware related services , creating and executing new processes, and communicating with integral kernel services...

 in the Unix
Unix
Unix is a multitasking, multi-user computer operating system originally developed in 1969 by a group of AT&T employees at Bell Labs, including Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas McIlroy, and Joe Ossanna...

 operating system, which commits to disk all data in the kernel filesystem buffers
Buffer (computer science)
In computer science, a buffer is a region of a physical memory storage used to temporarily hold data while it is being moved from one place to another. Typically, the data is stored in a buffer as it is retrieved from an input device or just before it is sent to an output device...

, i.e., data which has been scheduled for writing via low-level I/O
I/O
I/O may refer to:* Input/output, a system of communication for information processing systems* Input-output model, an economic model of flow prediction between sectors...

 system calls. Note that higher-level I/O layers such as stdio may maintain separate buffers of their own.

As a function in C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....

, the sync call is typically declared as void sync(void) in . The system call is also available via a command line utility also called sync, and similarly named functions in other languages such as Perl
Perl
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular...

.

The related system call fsync commits just the buffered data relating to a specified file descriptor
File descriptor
In computer programming, a file descriptor is an abstract indicator for accessing a file. The term is generally used in POSIX operating systems...

. fdatasync is also available to write out just the changes made to the data in the file, and not necessarily the file's related metadata.

Unix systems typically run some kind of flush or update daemon
Daemon (computing)
In Unix and other multitasking computer operating systems, a daemon is a computer program that runs as a background process, rather than being under the direct control of an interactive user...

, which calls the sync function on a regular basis. On some systems, the cron
Cron
Cron is a time-based job scheduler in Unix-like computer operating systems. Cron enables users to schedule jobs to run periodically at certain times or dates...

 daemon does this, and on Linux
Linux
Linux is a Unix-like computer operating system assembled under the model of free and open source software development and distribution. The defining component of any Linux system is the Linux kernel, an operating system kernel first released October 5, 1991 by Linus Torvalds...

 it's handled by the pdflush daemon. Buffers are also flushed when filesystems are unmounted
Mount (computing)
Mounting takes place before a computer can use any kind of storage device . The user or their operating system must make it accessible through the computer's file system. A user can access only files on mounted media.- Mount point :A mount point is a physical location in the partition used as a...

 or remounted read-only
Read-only
In computing, read-only can mean:* Read-only memory , a type of storage media* Read-only access to files or directories in file system permissions...

, for example prior to system shutdown.

Database use

In order to provide proper durability, databases need to use some form of sync in order to make sure the information written has made it to Non-volatile storage rather than just being stored in a memory-based write cache that would be lost if power failed. PostgreSQL
PostgreSQL
PostgreSQL, often simply Postgres, is an object-relational database management system available for many platforms including Linux, FreeBSD, Solaris, MS Windows and Mac OS X. It is released under the PostgreSQL License, which is an MIT-style license, and is thus free and open source software...

 for example uses a variety of different sync calls, including fsync and fdatasync, in order for commits to be durable. Unfortunately, for any single client writing a series of records, a rotating hard drive can only commit once per rotation, which makes for at best a few hundred such commits per second. Turning off the fsync requirement can therefore greatly improve commit performance, but at the expense of potentially introducing database corruption after a crash.

Databases also employ log files (typically much smaller than the main data files) that have information about recent changes, such that changes can be reliably redone in case of crash; then the main data files can be synced less often.

Controversy

Hard disks may default to using their own volatile write cache to buffer writes, which greatly improves performance while introducing a potential for lost writes. The performance impact of turning caching off is so large that even the normally conservative FreeBSD community rejected disabling write caching by default in FreeBSD 4.3.

fsync has been found to slow down performance of Firefox 3.0; the call was introduced in order to guarantee the integrity of the embedded sqlite
SQLite
SQLite is an ACID-compliant embedded relational database management system contained in a relatively small C programming library. The source code for SQLite is in the public domain and implements most of the SQL standard...

 database. Linux Foundation
Linux Foundation
The Linux Foundation is a non-profit technology consortium chartered to foster the growth of Linux.Founded in 2007 by the merger of the Open Source Development Labs and the Free Standards Group , the Linux Foundation sponsors the work of Linux creator Linus Torvalds and is supported by leading...

 Chief technical officer
Chief technical officer
A chief technology officer is an executive-level position in a company or other entity whose occupant is focused on scientific and technological issues within an organization....

 Theodore Ts'o
Theodore Ts'o
Theodore Y. "Ted" Ts'o is a software developer mainly known for his contributions to the Linux kernel, in particular his contributions to file systems.He graduated in 1990 from MIT with a degree in computer science...

 claims there is no need to "fear fsync".

libnofsync is a hack that replaces fsync with a dummy function that does nothing.
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK