mutex
serves no purpose. It is not shared between processes, and your program is not multithreaded.If you want an exclusion between unrelated instances of the program, you need to place it in a named shared memory segment (see example).
-
// Don't need any semaphores here - writing processes write to a temp file then rename so no issues with reading
Not so. First of all, every process writes to the same temp file (
tempIndex.bin
).After you fix this, there is still a race condition. Process
P1
writes totemp1
. Meanwhile, processP2
writes totemp2
. After renaming, one of the edits is lost. The line
index = fopen("index.bin", "r");
at the end of
addIndexLine
has no effect. The caller still has the original file pointer. BTW, after the call toaddIndexLin
the pointed file is closed, and cannot be used by a caller anymore. Doesn't look promising.remove("data.bin")
is superfluous. Justrename
works as well.Don't spell out the program name in the help/error messages. Use
argv[0]
.const
-qualification of arguments is sorely missing.compareString
... What's wrong withstrncmp
?
PS: I didn't dig deep into the design of the DB files. To be honest, I was quickly lost.