in reply to [SOLVED] Strange IO + concurrency issue
I'm not entirely sure what you are trying to achieve, but the issue seems to be that you are only locking the file open not the file writes.
so :- fork 1 fork 2 takes the lock opens file1 releases the lock starts writing data gets pre-empted takes the lock opens file1 (& deletes contents) releases the lock starts writing data Etc....
So potentially many children all writing to the same file at the same time. If you get each fork to write a different character to the file you'll see what's happening.