Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

186
Visualizações
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:

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?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda