Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    QProcess won't terminate immediately in a forked process

    General and Desktop
    2
    2
    2037
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • L
      lederer last edited by

      I have the following problem:

      I have a linux application, which can be either run as a standalone application or a service (forked).

      My application later starts another application (in this case, mplayer) using a QProcess.
      If I terminate my application, I first sent a SIGTERM to my sub process, wait for its termination and then delete my QProcess object and terminate my application. All this works fine, if I am in standalone mode.

      If I do the same in service mode, the QProcess will NOT terminate after I sent the SIGTERM, but the started process gots zombie.
      The result is that my application waits 30 seconds (even, if I do NOT add a "waitForFinished") until it kills the QProcess and terminates.

      My question now is:
      does anyone know, WHY QProcess cannot be terminated/killed, if the application is running in service mode, but can, if it is running normally?

      Thx for any help!!

      Some details:
      Service mode means:
      @
      // start player as daemon
      pid_t pid, sid;
      // fork off parent process
      pid = fork();
      if (pid < 0) {
      // fork failed
      fprintf(stderr, "Unable to fork daemon process.");
      exit(EXIT_FAILURE);
      }
      if (pid > 0) {
      // fork succeeded. terminate parent
      qDebug() << "Forking child (" << pid << ") succeeded. Terminating parent (" << getpid() << ").\n";
      exit(EXIT_SUCCESS);
      }

      // child continues...
      umask(0);
      // start new session
      sid = setsid();
      if (sid < 0) {
          fprintf(stderr, "Unable to start new session.");
          exit(EXIT_FAILURE);
      }
      

      @

      my QProcess is started:
      @
      this->activePlayer = new QProcess(this);
      this->activePlayer->start(QString("mplayer"), this->playerArgs);
      this->activePlayer->waitForStarted();
      @

      and terminated
      @
      kill(this->activePlayer->pid(), SIGTERM);
      this->activePlayer->waitForFinished();
      system("killall mplayer 2> /dev/null"); // kill all remaining videoplayer processes!
      delete this->activePlayer;
      @

      Instead of "kill", I also tried here
      @this->activePlayer->terminate();@
      and
      @this->activePlayer->kill();@

      same results! :-(

      1 Reply Last reply Reply Quote 0
      • T
        tzander last edited by

        Remove the 'exit' from your parent project. Just return.

        You shared your QApplication between both processes, only one can 'exit'.

        1 Reply Last reply Reply Quote 0
        • First post
          Last post