in reply to Forking and writing to files

You could try adding a non-blocking wait and looping until each child successfully writes to the output file.

I dimly recall using semaphores to over come a similar problem when I did some C programming about 12 years ago.

CC