I have a Perl application which writes logs to a file using open and print calls.
open (FH, "d:\\temp.txt");
print FH "Some log";
close (FH);
However during an abrupt shutdown of the machine, the logs are not persisted to the file. So after searching at several places, two options were suggested for doing unbuffered IO (i.e writing the text to the disk instead of maintaining it in cache and then flushing it):
I have tried both these options and it just doesn't work. Any write that I do seconds before the abnormal shutdown gets lost.
Is there any way that I can almost deterministically accomplish unbuffered IO in Perl? I am running Windows 7 64-bit with Perl 5.8.3.
EDIT: I searched for how to have windows perform unbuffered IO and this is how it can be done! Call
- CreateFile with FILE_FLAG_NO_BUFFERING for the dwFlagsAndAttributes parameter. However, this has memory alignment issues to consider (i.e File Access buffers should be sector aligned; An application determine the sector size by calling GetDiskFreeSpace)
- Use WriteFile to write data to the file. This write would be unbuffered and instead of going to the cache, it straightaway goes to the disk.
- Finally, call FlushFileBuffers to flush the metadata associated with the files.
Could someone please assist with the Win32 APIs from Perl for these 3 calls.