Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Parent process, child process and QCoreApplication
Forum Updated to NodeBB v4.3 + New Features

Parent process, child process and QCoreApplication

Scheduled Pinned Locked Moved Solved General and Desktop
26 Posts 5 Posters 4.7k Views 1 Watching
  • 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.
  • jsulmJ jsulm

    @SPlatten said in Parent process, child process and QCoreApplication:

    Still doesn't work

    So, did you actually check whether the onExitModule slot was called?

    SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by
    #16

    @jsulm , yes and it isn't.

    Kind Regards,
    Sy

    J.HilkJ 1 Reply Last reply
    0
    • SPlattenS SPlatten

      @jsulm , yes and it isn't.

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #17

      @SPlatten than I would assume your application doesn't actually quit :P

      call quit manually in a timer after x amount of seconds, to see if everything closes correctly


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      0
      • SPlattenS SPlatten

        I launch a child process with:

        QProcess* clsScriptHelper::pLaunch(QString strApp, QStringList& slstArgs) {
            QString strFullPath(clsDebugService::strGetUserFolder(strApp));
            qdbg() << "Checking for PID for: " << strFullPath;
            QProcess* pModule = nullptr;
            qint64 int64PID;
            if ( blnGetPID(strFullPath, int64PID) == true ) {
            //Process already running, no action required
            } else {
                int intLastSep = strFullPath.lastIndexOf(QDir::separator());
                qdbg() << "Process is NOT running, launching";
        
                if ( intLastSep > 0 ) {
                    QString strName = strFullPath.mid(intLastSep + 1)
                           ,strPath = strFullPath.mid(0, intLastSep + 1);
                    pModule = new QProcess();
                    pModule->setArguments(slstArgs);
                    pModule->setWorkingDirectory(strPath);
                    pModule->setProgram(strName);
                    pModule->start();
                    int64PID = pModule->processId();
        
                    if ( int64PID == 0 ) {
                        delete pModule;
                        pModule = nullptr;
                    }
                }
            }
            if ( int64PID > 0 ) {
                qdbg() << "Process: " << strFullPath << " started, PID: " << QString::number(int64PID);
            }
            return pModule;
        }
        

        According to the documentation, using start will cause the child process to close when the parent is closed, which is exactly the functionality I want. It did work then I think after I modified the child to use QCoreApplication it has stopped working, in that when the parent process is terminated, the child process keeps running.

        Is there anything I can do to resolve this? I need the functionality of QCoreApplication.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #18

        @SPlatten said in Parent process, child process and QCoreApplication:

        According to the documentation, using start will cause the child process to close when the parent is closed, which is exactly the functionality I want. It did work then I think after I modified the child to use QCoreApplication it has stopped working, in that when the parent process is terminated, the child process keeps running.

        I'm going to throw my 2 cents in. Child process exiting when parent process exits --- so long as not detached, and child does not do something in its start up code to prevent it --- is an OS thing, not a Qt/C++ thing. So I would expect it to exit when parent exits, regardless of QCoreApplication or otherwise.[*]

        Are you sure (I mean 100%, guaranteed, you'll stake your grandmother on it) your parent process is exiting? For example, if it doesn't have a UI you won't get the default "exit app on last window closed" behaviour....

        [*] EDIT I'm rethinking my claim/stance on this, OS-wise. It's more complex than I recalled. Luckily, I don't have any grandmother to stake on this statement, though I may be 2 cents poorer....

        SPlattenS 1 Reply Last reply
        0
        • JonBJ JonB

          @SPlatten said in Parent process, child process and QCoreApplication:

          According to the documentation, using start will cause the child process to close when the parent is closed, which is exactly the functionality I want. It did work then I think after I modified the child to use QCoreApplication it has stopped working, in that when the parent process is terminated, the child process keeps running.

          I'm going to throw my 2 cents in. Child process exiting when parent process exits --- so long as not detached, and child does not do something in its start up code to prevent it --- is an OS thing, not a Qt/C++ thing. So I would expect it to exit when parent exits, regardless of QCoreApplication or otherwise.[*]

          Are you sure (I mean 100%, guaranteed, you'll stake your grandmother on it) your parent process is exiting? For example, if it doesn't have a UI you won't get the default "exit app on last window closed" behaviour....

          [*] EDIT I'm rethinking my claim/stance on this, OS-wise. It's more complex than I recalled. Luckily, I don't have any grandmother to stake on this statement, though I may be 2 cents poorer....

          SPlattenS Offline
          SPlattenS Offline
          SPlatten
          wrote on last edited by
          #19

          @JonB , It doesn't the process was launched using QProcess and the start method, I stopped the process that launched the child by closing all the windows. I then checked the processes running using:

          ps -A
          

          I can see the child is still running. I also attached to the child process using Qt Creator and put a break point in the onExitModule slot, it never gets there.

          Kind Regards,
          Sy

          JonBJ 1 Reply Last reply
          0
          • SPlattenS SPlatten

            @JonB , It doesn't the process was launched using QProcess and the start method, I stopped the process that launched the child by closing all the windows. I then checked the processes running using:

            ps -A
            

            I can see the child is still running. I also attached to the child process using Qt Creator and put a break point in the onExitModule slot, it never gets there.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #20

            @SPlatten said in Parent process, child process and QCoreApplication:

            I stopped the process that launched the child by closing all the windows.

            I don't understand. You said it's now a QCoreApplication...

            Oh, it's the child which is the QCoreApplication, not the parent?

            I stopped the process that launched the child by closing all the windows

            Maybe. Nonetheless, verify that the parent really does exit? Verify its (the parent's) PID has gone?

            I am lost. Could you please make clear which process(es) are QCoreApplications and which are QApplications, out of your parent & children? Just simply & clearly.

            Now that I have withdrawn/reneged on my earlier post claiming that OS will terminate children on parent exit:

            If your parent is a UI application, you should be able to get its aboutToQuit(), and forcibly kill any child processes. Why you should need to do that when you used QProcess::start() I'm not sure.

            SPlattenS 1 Reply Last reply
            0
            • JonBJ JonB

              @SPlatten said in Parent process, child process and QCoreApplication:

              I stopped the process that launched the child by closing all the windows.

              I don't understand. You said it's now a QCoreApplication...

              Oh, it's the child which is the QCoreApplication, not the parent?

              I stopped the process that launched the child by closing all the windows

              Maybe. Nonetheless, verify that the parent really does exit? Verify its (the parent's) PID has gone?

              I am lost. Could you please make clear which process(es) are QCoreApplications and which are QApplications, out of your parent & children? Just simply & clearly.

              Now that I have withdrawn/reneged on my earlier post claiming that OS will terminate children on parent exit:

              If your parent is a UI application, you should be able to get its aboutToQuit(), and forcibly kill any child processes. Why you should need to do that when you used QProcess::start() I'm not sure.

              SPlattenS Offline
              SPlattenS Offline
              SPlatten
              wrote on last edited by
              #21

              @JonB To clarify, the child application main:

              int main(int intArgc, char* parystrArgv[]) {
                  QCoreApplication a(intArgc, parystrArgv);
                  QCoreApplication::setApplicationName(clsModFileIO::scpszTitle());
                  clsModFileIO obj(&a, intArgc, parystrArgv);
                  return a.exec();
              }
              

              The constructor for clsModFileIO:

              clsModHelper::clsModHelper(QObject* pParent, int intArgc, char* parystrArgv[]
                                        ,const char* cpszTitle, double dblVersion)
                          : QTcpSocket(pParent)            
                          , mdblVersion(dblVersion)
                          , mfpDbgLog(nullptr)
                          , mint64AppPID(QCoreApplication::applicationPid())
                          , mstrTitle(cpszTitle)
                          , muint16ModulePort(0), muint16LauncherPID(0)
                          , muint16XMLMPAMport(0) {
                  if ( intArgc < CLA_LAUNCHER_PID ) {
                      std::cout << "Insufficient arguments, aborting!" << std::endl;
                      exit(EXIT_FAILURE);
                  }
                  if ( mspThis == nullptr ) {
                      mspThis = this;
                  }
                  muint16XMLMPAMport = (quint16)atoi(parystrArgv[CLA_XMLMPAM_PORT]);
                  muint16ModulePort = (quint16)atoi(parystrArgv[CLA_MODULE_PORT]);
                  muint16LauncherPID = (quint16)atoi(parystrArgv[CLA_LAUNCHER_PID]);
                  setSocketOption(QAbstractSocket::LowDelayOption, 1);
                  //Connect up the signals
                  QObject::connect(QCoreApplication::instance(), &QCoreApplication::aboutToQuit
                                  ,this, &clsModHelper::onExitModule);
                 ...
              

              Kind Regards,
              Sy

              JonBJ 1 Reply Last reply
              0
              • SPlattenS SPlatten

                @JonB To clarify, the child application main:

                int main(int intArgc, char* parystrArgv[]) {
                    QCoreApplication a(intArgc, parystrArgv);
                    QCoreApplication::setApplicationName(clsModFileIO::scpszTitle());
                    clsModFileIO obj(&a, intArgc, parystrArgv);
                    return a.exec();
                }
                

                The constructor for clsModFileIO:

                clsModHelper::clsModHelper(QObject* pParent, int intArgc, char* parystrArgv[]
                                          ,const char* cpszTitle, double dblVersion)
                            : QTcpSocket(pParent)            
                            , mdblVersion(dblVersion)
                            , mfpDbgLog(nullptr)
                            , mint64AppPID(QCoreApplication::applicationPid())
                            , mstrTitle(cpszTitle)
                            , muint16ModulePort(0), muint16LauncherPID(0)
                            , muint16XMLMPAMport(0) {
                    if ( intArgc < CLA_LAUNCHER_PID ) {
                        std::cout << "Insufficient arguments, aborting!" << std::endl;
                        exit(EXIT_FAILURE);
                    }
                    if ( mspThis == nullptr ) {
                        mspThis = this;
                    }
                    muint16XMLMPAMport = (quint16)atoi(parystrArgv[CLA_XMLMPAM_PORT]);
                    muint16ModulePort = (quint16)atoi(parystrArgv[CLA_MODULE_PORT]);
                    muint16LauncherPID = (quint16)atoi(parystrArgv[CLA_LAUNCHER_PID]);
                    setSocketOption(QAbstractSocket::LowDelayOption, 1);
                    //Connect up the signals
                    QObject::connect(QCoreApplication::instance(), &QCoreApplication::aboutToQuit
                                    ,this, &clsModHelper::onExitModule);
                   ...
                
                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #22

                @SPlatten
                Your reply may have crossed with my update.

                • I can now see that your child processes are clearly QCoreApplication.

                • What is your parent application? QApplication or QCoreApplication?

                EDIT
                No, no, what's this:

                    //Connect up the signals
                    QObject::connect(QCoreApplication::instance(), &QCoreApplication::aboutToQuit
                                    ,this, &clsModHelper::onExitModule);
                

                That is "about to quit" in the child. I thought you said you want to the parent to exit, and the children should exit at the same time, isn't that your whole question?

                SPlattenS 1 Reply Last reply
                0
                • JonBJ JonB

                  @SPlatten
                  Your reply may have crossed with my update.

                  • I can now see that your child processes are clearly QCoreApplication.

                  • What is your parent application? QApplication or QCoreApplication?

                  EDIT
                  No, no, what's this:

                      //Connect up the signals
                      QObject::connect(QCoreApplication::instance(), &QCoreApplication::aboutToQuit
                                      ,this, &clsModHelper::onExitModule);
                  

                  That is "about to quit" in the child. I thought you said you want to the parent to exit, and the children should exit at the same time, isn't that your whole question?

                  SPlattenS Offline
                  SPlattenS Offline
                  SPlatten
                  wrote on last edited by
                  #23

                  @JonB Parent application main:

                  QApplication a(argc, argv);
                  

                  Kind Regards,
                  Sy

                  JonBJ 1 Reply Last reply
                  0
                  • SPlattenS SPlatten

                    @JonB Parent application main:

                    QApplication a(argc, argv);
                    
                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #24

                    @SPlatten
                    Did you see my EDIT in previous post?

                    SPlattenS 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @SPlatten
                      Did you see my EDIT in previous post?

                      SPlattenS Offline
                      SPlattenS Offline
                      SPlatten
                      wrote on last edited by SPlatten
                      #25

                      @JonB Thats in the child process, sorry, just read the whole edit. What I want is the child process to automatically close when the parent is terminated.

                      Kind Regards,
                      Sy

                      JonBJ 1 Reply Last reply
                      0
                      • SPlattenS SPlatten

                        @JonB Thats in the child process, sorry, just read the whole edit. What I want is the child process to automatically close when the parent is terminated.

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by JonB
                        #26

                        @SPlatten
                        I can see your connect(aboutToQuit) is in the child process. That's the wrong place if you are saying:

                        1. You have a parent process (UI QApplication).

                        2. You have that spawn some child processes, via QProcess::start().

                        3. You exit the parent (perhaps you close all its windows)...

                        4. ... And you want the previously-spawned child processes to exit when the parent does.

                        Can't you just say if that is what you are asking?

                        EDIT Ah, OK, I think you have edited to say that is indeed what you want.

                        OK then, you are putting the code in the wrong application :)

                        Supposedly, you shouldn't have to do anything. QProcess::start() looks like it should do that, as a result of what it says for startDetached():

                        If the calling process exits, the detached process will continue to run unaffected.

                        I think you may have said at one point the children were exiting when the parent did. And then I think you say you happened to change the children to QCoreApplication, and, for whatever reason, they ceased exiting when the parent does?

                        Anyway. If your children are not exiting and you want them to do so when exiting the parent, we are (at least I am) suggesting you forcibly terminate them from the parent just before it exits. So, in the parent, keep a list of the created, non-exited child QProcesses. Catch the aboutToQuit or whatever signal in the parent, make that QProcess::terminate() (or failing that QProcesss::kill()) the children just before exiting the parent.

                        Is that what you need?

                        Separately. IF your parent/children are server/clients across sockets to each other --- as I thought they were --- the server should be able to send a message and close the sockets to the clients, and they should exit on that, instead of terminating processes.

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved