How to Spawn Child Processes that Don't Die with Parent?

前端 未结 6 1233
说谎
说谎 2021-01-18 11:09

I have a C++ program that acts as a watchdog over others. If it detects that a process is no longer running, it restarts it via system. The problem is, if I kil

6条回答
  •  梦谈多话
    2021-01-18 11:54

    Don't use system(...) it is not multi-threading safe.

    int pid = fork();
    
    if(pid==0)
    {
      int rc = execv("processname", "/path/to/processConfig.xml &");
      if(rc == -1)
      {
        printf(stderr, "processname failed to start. %d", errno);
      }
      ...
    }
    else
    {
      //continue with the parent
    }
    

提交回复
热议问题