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. Process and child process
Forum Updated to NodeBB v4.3 + New Features

Process and child process

Scheduled Pinned Locked Moved Solved General and Desktop
35 Posts 4 Posters 6.2k Views 2 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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by
    #1

    I have a process that launches another application, both applications are written by myself. I've run the child process in Qt Creator and it functions as I would expect and stays running until I terminate it.

    When I run the main process it launches the child with:

    QString strFullPath(clsDebugService::strGetUserFolder(strApp));
    int64PID = 0;    
    qdbg() << "Checking for PID for: " << strFullPath;
    if ( blnGetPID(strFullPath, int64PID) == true ) {
    //Process already running, no action required
    } else {
        int intLastSep = strFullPath.lastIndexOf(QDir::separator());
    
        if ( intLastSep > 0 ) {
            QString strName = strFullPath.mid(intLastSep + 1)
                   ,strPath = strFullPath.mid(0, intLastSep + 1);
            qdbg() << "Launching: " << strFullPath;
            mspobjProcess->startDetached(strFullPath, slstArgs, strPath, &int64PID);
        }
    }    
    if ( int64PID > 0 ) {
        qdbg() << "Process: " << strFullPath << ", is running PID: " << QString::number(int64PID);
        return true;
    }
    return false;
    

    This is the body of my launching function. I can see that the child process is successfully launched and the PID is returned, however almost instantly the child process terminates and the PID is no longer listed. I'm struggling to determine why? All the processes create log files and log output.

    Is there anything that could explain why is terminates? The host computer is an iMAC:

    macOS Catalina
    Version 10.15.7
    iMac (Retina 5K, 27-inch, Late 2015)
    Processor 4 GHz Quad-Core Intel Core i7
    Memory 16 GB 1867 MHz DDR3
    Graphics AMD Radeon R9 M395X 4GB
    

    Qt:

    Qt Creator 4.13.2
    Based on Qt 5.15.1 (Clang 11.0 (Apple), 64 bit)
    Built on Oct 1 2020 01:16:45
    From revision 2ee1af2032
    

    Kind Regards,
    Sy

    jsulmJ 1 Reply Last reply
    0
    • SPlattenS SPlatten

      @JonB , like I said, its obviously working because the application is started, the log file created and contains content, so how could startDetached return false if it launched the process.

      bool clsMainWnd::blnLaunch(QString strApp, QStringList& slstArgs, qint64& int64PID) {
          Q_ASSERT_X(clsMainWnd::mspobjProcess!=nullptr, "blnLaunch", "mspobjProcess is null!");
          QString strFullPath(clsDebugService::strGetUserFolder(strApp));
          int64PID = 0;    
          qdbg() << "Checking for PID for: " << strFullPath;
          if ( blnGetPID(strFullPath, int64PID) == true ) {
          //Process already running, no action required
          } else {
              int intLastSep = strFullPath.lastIndexOf(QDir::separator());
              qdbg() << "Process is NOT running, lanching";
      
              if ( intLastSep > 0 ) {
                  QString strName = strFullPath.mid(intLastSep + 1)
                         ,strPath = strFullPath.mid(0, intLastSep + 1);            
                  clsMainWnd::mspobjProcess->setArguments(slstArgs);
                  clsMainWnd::mspobjProcess->setWorkingDirectory(strPath);
                  clsMainWnd::mspobjProcess->setProgram(strName);
                  
                  if ( clsMainWnd::mspobjProcess->startDetached(&int64PID) == false ) {
                      return false;
                  }
              }
          }    
          if ( int64PID > 0 ) {
              qdbg() << "Process: " << strFullPath << " started, PID: " << QString::number(int64PID);
              return true;
          }
          return false;
      }
      

      Why don't the signals started and/or stateChanged get raised?

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

      @JonB , @jsulm , Found it!

      Finally! Turned out to be a silly error, obvious when I found it, should have realised. One of the sockets is being used by the parent and the child terminates if it cannot listen to the socket.

      Thank you for your time and input, it helped me along the way.

      This now raises question, is there a better way to communicate between processes instead of using sockets?

      I plan to add lots of other processes the relationship will be parent launching application will always be the consumer and all the processes it launches will provide data services.

      At some point in the future processes may also be on remote systems but there is no plan for that just yet.

      Kind Regards,
      Sy

      SPlattenS 1 Reply Last reply
      0
      • SPlattenS SPlatten

        I have a process that launches another application, both applications are written by myself. I've run the child process in Qt Creator and it functions as I would expect and stays running until I terminate it.

        When I run the main process it launches the child with:

        QString strFullPath(clsDebugService::strGetUserFolder(strApp));
        int64PID = 0;    
        qdbg() << "Checking for PID for: " << strFullPath;
        if ( blnGetPID(strFullPath, int64PID) == true ) {
        //Process already running, no action required
        } else {
            int intLastSep = strFullPath.lastIndexOf(QDir::separator());
        
            if ( intLastSep > 0 ) {
                QString strName = strFullPath.mid(intLastSep + 1)
                       ,strPath = strFullPath.mid(0, intLastSep + 1);
                qdbg() << "Launching: " << strFullPath;
                mspobjProcess->startDetached(strFullPath, slstArgs, strPath, &int64PID);
            }
        }    
        if ( int64PID > 0 ) {
            qdbg() << "Process: " << strFullPath << ", is running PID: " << QString::number(int64PID);
            return true;
        }
        return false;
        

        This is the body of my launching function. I can see that the child process is successfully launched and the PID is returned, however almost instantly the child process terminates and the PID is no longer listed. I'm struggling to determine why? All the processes create log files and log output.

        Is there anything that could explain why is terminates? The host computer is an iMAC:

        macOS Catalina
        Version 10.15.7
        iMac (Retina 5K, 27-inch, Late 2015)
        Processor 4 GHz Quad-Core Intel Core i7
        Memory 16 GB 1867 MHz DDR3
        Graphics AMD Radeon R9 M395X 4GB
        

        Qt:

        Qt Creator 4.13.2
        Based on Qt 5.15.1 (Clang 11.0 (Apple), 64 bit)
        Built on Oct 1 2020 01:16:45
        From revision 2ee1af2032
        
        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @SPlatten said in Process and child process:

        Is there anything that could explain why is terminates?

        Please first add proper error handling to your code (QProcess provides methods for that).

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        SPlattenS 1 Reply Last reply
        4
        • jsulmJ jsulm

          @SPlatten said in Process and child process:

          Is there anything that could explain why is terminates?

          Please first add proper error handling to your code (QProcess provides methods for that).

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

          @jsulm , any suggestions on how? As far as I can see QProcess is launching the application and a valid PID is returned, there is also output in the applications log file so I know it launches.

          Kind Regards,
          Sy

          jsulmJ 1 Reply Last reply
          0
          • SPlattenS SPlatten

            @jsulm , any suggestions on how? As far as I can see QProcess is launching the application and a valid PID is returned, there is also output in the applications log file so I know it launches.

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @SPlatten Then read from https://doc.qt.io/qt-5/qprocess.html#readAllStandardError to see whether the application started by QProcess prints any warnings/errors.
            And you still should check https://doc.qt.io/qt-5/qprocess.html#error and/or connect a slot to https://doc.qt.io/qt-5/qprocess.html#errorOccurred as an error can occur after starting the process.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            SPlattenS 1 Reply Last reply
            2
            • jsulmJ jsulm

              @SPlatten Then read from https://doc.qt.io/qt-5/qprocess.html#readAllStandardError to see whether the application started by QProcess prints any warnings/errors.
              And you still should check https://doc.qt.io/qt-5/qprocess.html#error and/or connect a slot to https://doc.qt.io/qt-5/qprocess.html#errorOccurred as an error can occur after starting the process.

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

              @jsulm , I just added:

              qdbg() << mspobjProcess->readAllStandardError();
              qdbg() << mspobjProcess->readAllStandardOutput();
              

              And the output in the log file was:

              QIODevice::read (QProcess): device not open
              QIODevice::read (QProcess): device not open
              

              Kind Regards,
              Sy

              jsulmJ JonBJ 2 Replies Last reply
              0
              • SPlattenS SPlatten

                @jsulm , I just added:

                qdbg() << mspobjProcess->readAllStandardError();
                qdbg() << mspobjProcess->readAllStandardOutput();
                

                And the output in the log file was:

                QIODevice::read (QProcess): device not open
                QIODevice::read (QProcess): device not open
                
                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #6

                @SPlatten said in Process and child process:

                I just added:

                Where? And when is this code executed?

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • SPlattenS SPlatten

                  @jsulm , I just added:

                  qdbg() << mspobjProcess->readAllStandardError();
                  qdbg() << mspobjProcess->readAllStandardOutput();
                  

                  And the output in the log file was:

                  QIODevice::read (QProcess): device not open
                  QIODevice::read (QProcess): device not open
                  
                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #7

                  @SPlatten
                  I am not 100% sure that @jsulm has noticed you are using startDetached(), and I'm not 100% sure whether for that you must instead use setStandardErrorFile() etc. instead, as per https://doc.qt.io/qt-5/qprocess.html#startDetached ?

                  EDIT Hold on! The overload you are using, https://doc.qt.io/qt-5/qprocess.html#startDetached-1, is static. So you're not going to have a mspobjProcess instance? For more control over it use the https://doc.qt.io/qt-5/qprocess.html#startDetached overload I referred to earlier.

                  jsulmJ 1 Reply Last reply
                  1
                  • JonBJ JonB

                    @SPlatten
                    I am not 100% sure that @jsulm has noticed you are using startDetached(), and I'm not 100% sure whether for that you must instead use setStandardErrorFile() etc. instead, as per https://doc.qt.io/qt-5/qprocess.html#startDetached ?

                    EDIT Hold on! The overload you are using, https://doc.qt.io/qt-5/qprocess.html#startDetached-1, is static. So you're not going to have a mspobjProcess instance? For more control over it use the https://doc.qt.io/qt-5/qprocess.html#startDetached overload I referred to earlier.

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #8

                    @JonB Actually I noticed before, but forgot about it :-)
                    @SPlatten Any special reason you're using startDetached?

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    SPlattenS 1 Reply Last reply
                    0
                    • jsulmJ jsulm

                      @JonB Actually I noticed before, but forgot about it :-)
                      @SPlatten Any special reason you're using startDetached?

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

                      @jsulm To be honest, probably because the example I came across for QProcess was using it. What I want is to launch the child process and allow the launching process to carry on without being held up.

                      Kind Regards,
                      Sy

                      jsulmJ JonBJ 2 Replies Last reply
                      0
                      • SPlattenS SPlatten

                        @jsulm To be honest, probably because the example I came across for QProcess was using it. What I want is to launch the child process and allow the launching process to carry on without being held up.

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #10

                        @SPlatten Usually you would only use startDetached if the second process needs to continue if the parent process terminates. If this is not a requirement for your app then simply use start(...).

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        2
                        • SPlattenS SPlatten

                          @jsulm To be honest, probably because the example I came across for QProcess was using it. What I want is to launch the child process and allow the launching process to carry on without being held up.

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

                          @SPlatten said in Process and child process:

                          What I want is to launch the child process and allow the launching process to carry on without being held up.

                          That happens without startDetached(), QProcess runs asynchronously. You only need startDetached basically if you want to sub-process not to e.g. get terminated when your process exits.

                          SPlattenS 1 Reply Last reply
                          1
                          • JonBJ JonB

                            @SPlatten said in Process and child process:

                            What I want is to launch the child process and allow the launching process to carry on without being held up.

                            That happens without startDetached(), QProcess runs asynchronously. You only need startDetached basically if you want to sub-process not to e.g. get terminated when your process exits.

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

                            @JonB , @jsulm , also startDetached() obtains the PID where as start does not.

                            Why does startDetached take a qint64 for the PID as the last parameter but the QProcess function pid returns not a qint64 but a Q_PID?

                            Why are these different types?

                            Kind Regards,
                            Sy

                            jsulmJ JonBJ 2 Replies Last reply
                            0
                            • SPlattenS SPlatten

                              @JonB , @jsulm , also startDetached() obtains the PID where as start does not.

                              Why does startDetached take a qint64 for the PID as the last parameter but the QProcess function pid returns not a qint64 but a Q_PID?

                              Why are these different types?

                              jsulmJ Offline
                              jsulmJ Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on last edited by
                              #13

                              @SPlatten Do you really need the PID? In the code you posted you only check and print it out.

                              https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              0
                              • SPlattenS Offline
                                SPlattenS Offline
                                SPlatten
                                wrote on last edited by
                                #14

                                @jsulm , I get the PID so I can verify its actually running since neither start or startDetached return anything.

                                Kind Regards,
                                Sy

                                jsulmJ J.HilkJ JonBJ 3 Replies Last reply
                                0
                                • SPlattenS SPlatten

                                  @jsulm , I get the PID so I can verify its actually running since neither start or startDetached return anything.

                                  jsulmJ Offline
                                  jsulmJ Offline
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on last edited by jsulm
                                  #15

                                  @SPlatten See https://doc.qt.io/qt-5/qprocess.html#state
                                  And check https://doc.qt.io/qt-5/qprocess.html#start

                                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  1 Reply Last reply
                                  3
                                  • SPlattenS SPlatten

                                    @jsulm , I get the PID so I can verify its actually running since neither start or startDetached return anything.

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

                                    @SPlatten said in Process and child process:

                                    @jsulm , I get the PID so I can verify its actually running since neither start or startDetached return anything.

                                    well, Process has the started and stateChanged signal for that.
                                    https://doc.qt.io/qt-5/qprocess.html#started

                                    you should actually listen to the stateChanged signal, to see if it changes from Running to NotRunning


                                    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
                                    3
                                    • SPlattenS SPlatten

                                      @jsulm , I get the PID so I can verify its actually running since neither start or startDetached return anything.

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

                                      @SPlatten
                                      You have other things you can look at instead, like QProcess::stateChanged signal for clues.

                                      While you get it working, you may find start() is easier to work with than startDetached().

                                      EDIT Sigh, looks like 3 of us are all trying to answer :)

                                      If it's not secret, you might like to share with us the full command you are running, including the arguments, in case we can spot anything for you....

                                      SPlattenS 2 Replies Last reply
                                      3
                                      • J.HilkJ Offline
                                        J.HilkJ Offline
                                        J.Hilk
                                        Moderators
                                        wrote on last edited by J.Hilk
                                        #18

                                        3 People 3 times the same thought

                                        Up Top🙌


                                        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
                                        1
                                        • JonBJ JonB

                                          @SPlatten
                                          You have other things you can look at instead, like QProcess::stateChanged signal for clues.

                                          While you get it working, you may find start() is easier to work with than startDetached().

                                          EDIT Sigh, looks like 3 of us are all trying to answer :)

                                          If it's not secret, you might like to share with us the full command you are running, including the arguments, in case we can spot anything for you....

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

                                          @JonB I just tried start and it didn't work, I got errors in the Application Output:

                                          QObject: Cannot create children for a parent that is in a different thread.
                                          (Parent is QProcess(0x101e0b420), parent's thread is QThread(0x101e0b880), current thread is clsThread(0x122c1bcc0)
                                          

                                          Kind Regards,
                                          Sy

                                          jsulmJ JonBJ 2 Replies Last reply
                                          0
                                          • SPlattenS SPlatten

                                            @JonB I just tried start and it didn't work, I got errors in the Application Output:

                                            QObject: Cannot create children for a parent that is in a different thread.
                                            (Parent is QProcess(0x101e0b420), parent's thread is QThread(0x101e0b880), current thread is clsThread(0x122c1bcc0)
                                            
                                            jsulmJ Offline
                                            jsulmJ Offline
                                            jsulm
                                            Lifetime Qt Champion
                                            wrote on last edited by
                                            #20

                                            @SPlatten Do you use threads?

                                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                                            1 Reply Last reply
                                            1

                                            • Login

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