adizone
adizone

Reputation: 213

C memory mapped file vs regular file

I am working on an implementation where multiple processes read a regular file A. While this is happening, a new process P starts up and copies the contents from A to regular file B. All processes starting up after this, should now read file B.
To make the switch from A to B, process P creates a temporary file T once B is written. All the processes check whether T exists to decide where to read data from (i.e., read from A if T does NOT exist, and read from B if T exists).
Since T file is just an indicator here, is it better to use a memory mapped file rather than regular file for faster performance?

Upvotes: 2

Views: 232

Answers (1)

Valentin H
Valentin H

Reputation: 7438

Using a tmp-file for synchronization is not safe. The check, if file exists and reading that file is not atomic. One process could switch the files just after another process has completed the check and is about to read.

If you are developing in C and allowed to use IPC API, you could set the flag in shared memory and guard it by a semaphore. Also, the processes should signal, that they have finished the reading.

Upvotes: 1

Related Questions