kan
kan

Reputation: 28991

Does write the same content into file need sync?

Suppose I have two processes which may write the same content into the same file:

echo "large content" > aFileName
cat aFileName

Does it need synchronization/locks? Could I be sure that after such command the file will have the content not mutilated by race conditions in all processes?

Upvotes: 2

Views: 53

Answers (1)

Jester
Jester

Reputation: 58822

Each process will be using its own file pointer so it should be safe in the normal case. The only problem I can see is:

  1. process A truncates the file
  2. process A writes some data
  3. process B truncates the file
  4. process B writes less than what process A has written in step #2
  5. process B terminates abnormally without writing the whole file

Now some of the data written in step #2 is lost, even if process A then continues to write the rest.

You could write into a temporary file that's atomically renamed after all the content has been written. Make sure the temporary file is on the same file system as the output, and use some unique identifier (such as the process id) in its name. Also, set up a trap handler to delete the file.

Downside to this solution is that it takes up more storage because multiple copies of the temporary file may be written concurrently, and also some of said temporary files may stay behind as garbage if the process dies without the trap handler being able to run.

Upvotes: 1

Related Questions