View Single Post
  #2  
Old February 25th, 2003, 05:27 AM
rhg rhg is offline
Registered User
 
Join Date: Dec 2002
Location: Velbert, Germany
Posts: 220
Thanks: 0
Thanked 0 Times in 0 Posts
rhg is on a distinguished road
Your problem doesn't have anything to do with platform differences. It is just a race condition.

Your program relies on the fact that the child process reads the file after the parent process has finished writing it. This, however, is not guarantteed at all.
Also, you cannot rely on wait(NULL) for this purpose. If you want to guarantee it you must synchronize the processes or explicitely lock the file while writing or reading it.

BTW:

1) Your temp array is too small. The "Child_%d.txt" format string will result in 11 characters for single digit file numbers. However, a C string requires an invisible 0 terminating byte at the end so your string will be 12 characters.

It is good practice to make arrays big enough for the largest possible value. A 32bit integer can use up to 10 digits so I would suggest to make the temp array at least 22 bytes.

2) strcpy(filename, temp) is not necessary. Why not eliminate the temp array, make filename 22 bytes and sprintf into filename? Anyway, sprintf returns the resulting string length which you assign to xxx. However, you never use this value afterwards, so why the assignment?
Reply With Quote