You already know that when data is written to a file, the kernel arranges to write the data into a memory buffer and not straight out to disk. You have also seen that data writes can be forced by the use of the sync command. This command uses the underlying sync() system call:
#include <unistd.h> int sync(void);
You should appreciate, if you intend to use this system call, that it only schedules the disk data to be written out to disk; it doesn't wait for the physical writes to take place. This means that you must not assume that the disk writes have actually taken place when the sync() call returns to your program, only that they have been scheduled to take place as soon as possible.
In fact, it is very rare that you will need to call sync() yourself as there is a pair of programs called update and bdflush which most Linux distributions arrange to execute automatically at system boot time. These commands remain active the whole time the system is running, and they flush the disk buffer cache out to disk on a regular basis, every 30 seconds (more often for really important data). In this way even a had machine crash should lose no more than 30 seconds worth of work.
Unlike mount() and unount(), the sync() system call can be used at any time by any user.