For one thing, you aren't checking for failures on commands like open(). Those may not succeed. You also don't need to actually flock the file if you open for append and are on a sane POSIX system. The seek will be done for you, and the writes will be atomic.