Skip to content

using file lock for a single instance program fails if spawn child process

I need to have a single instance program in Linux. That is if someone tries to run the program the new instance should print a message and exit. At the moment I have a lock mechanism like this:

JavaScript

The problem is if I do anything that spawns child processes using int system(const char *command); and at some point, someone uses “kill” to end this program, the child processes will stay alive and the lock will not get deleted hence preventing my app to run again.

Is there a solution to this shortcoming?

Advertisement

Answer

You need O_CLOEXEC as a flag to open(). Then the handle won’t be open in the child process.

system() eventually calls exec(), which is how new process binaries are loaded.

User contributions licensed under: CC BY-SA
4 People found this is helpful