1

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:

main() {
    // init some stuff...


    // set or check lock
    auto pidFile = open("/var/run/my-app.lock", O_CREAT | O_RDWR, 0666);
    auto rc = flock(pidFile, LOCK_EX | LOCK_NB);
    if(rc) {
        if(errno == EWOULDBLOCK) {
            cout << "Program is already running!\n";
            exit(0);
        }
    }

    // do other stuff or the main loop
    

   // when the loop ends by sigkill or sigterm or ...
   exit(0);
 

}

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?

Joshua
  • 40,822
  • 8
  • 72
  • 132
DEKKER
  • 877
  • 6
  • 19
  • 1
    Usually, you put the pid into the file and if the file already exist at the startup, you retireve the pid and check if the process associated is actually legit (just existing ins't enougth as pid can be reused). If not, then you can alert that the previous process was halted improperly and start your new program – Tom's Apr 19 '22 at 13:21
  • 2
    Decide which language you use. C and C++ are quite different. – the busybee Apr 19 '22 at 13:37
  • @thebusybee: This question doesn't care though. The answer is good for both. – Joshua Apr 19 '22 at 17:03
  • @Joshua In principle you're right, but then no language tag is right, because all programming languages can use your solution. ;-) – the busybee Apr 19 '22 at 20:25

1 Answers1

3

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.

Joshua
  • 40,822
  • 8
  • 72
  • 132