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. 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.Don't spell out the program name in the help/error messages. Use
argv[0]
.compareString
... What's wrong withstrncmp
?
vnp
- 57.3k
- 4
- 51
- 140