A program (which needs to read and write from and to the filesystem), has the extra feature of being able to communicate with an external sensor. Therefore it has the unique ability to know if a power loss is imminent. The warning I get is only a few (around 3 to 5) seconds - not enough to perform a full shut-down. A few precious seconds might be added, but it would increase hardware costs.
As writing to a file does not guarantee that the OS will do the job now, even closing the file can, as far as I know, lead to the OS deciding it will do the closing later if no one else tries to access it, so how can I guarantee that
- all the writes I perform now will be saved. (After the warning is received, my program will write a few kilobytes to disk, but there might have been larger amounts of data written before the warning is received, which might or might not yet be finalized by the OS)
- No other corruption occurs because of an improper shut-down of the OS.
Note: by design, the loss of power might be a regular occurrence. Also by design, no other "user applications" will run on the system (so we don't have to worry for example about a music player, a development environment, a spreadsheet editor and gimp all running and accessing the file system).