EmptyData
EmptyData

Reputation: 2576

c++ multiprocess writing a unique file

I have a process which creates multiple fork processes and each forked process creates a file with the same name. But the requirement is to create only one file by any of the forked processes.

Can you please suggest how to make sure file is created by only one process.

I tried to check if the file exists before writing a file but it is not working as multiple processes are getting pass beyond that check.

Upvotes: 1

Views: 494

Answers (2)

Aconcagua
Aconcagua

Reputation: 25526

You might open the file in the parent process before forking. Each child then can, according to this answer, just re-use the file descriptor and you should be fine.

Of course, you'd still need some means to synchronise your different processes such that writes to the file are interleaved properly (one process writing AB and the other one C resulting in ABC or CAB, but not ACB...). Doing so is easier with threads, so if you can use them you might prefer them. Otherwise, this answer might be of interest to you.

Edit (according to new comments to question): If I understand correctly, you want to do the following:

  • Parent process creates several child processes.
  • Each child writes some information to a file.
  • After each or all children having terminated, parent reads back the information from files.

You might do the following then:

  1. Parent process opens a new temporary file with with arbitrary name for each child - right before forking. This way, according to above, parent and child have the same file descriptors available.
  2. After forking, child process uses the file descriptor to write it's information to, parent process pushes the descriptor to e. g. a std::vector or, if more suitable, a std::map mapping PID to FD.
  3. Parent process waits for children, on termination uses the file descriptors stored to read information back.

Have you considered using pipes instead? They might be the better alternative...

Upvotes: 0

Maxim Egorushkin
Maxim Egorushkin

Reputation: 136256

Can you please suggest how to make sure file is created by only one process.

  1. Each of the concurrent processes (or threads) creates its own file in the target directory with a unique temporary filename with mkstemp.
  2. Then the process renames that file to the target filename. Renaming is atomic and either succeeds or fails. If it fails, then another process already created the file. In this case the temporary file can be removed and the file with the target filename should be opened instead.

Upvotes: 2

Related Questions