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
Qt 6.11 is out! See what's new in the release blog

Process and child process

Scheduled Pinned Locked Moved Solved General and Desktop
35 Posts 4 Posters 6.7k 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.
  • 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
      • 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?

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by
        #21

        @SPlatten said in Process and child process:

        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?

        Don't use Q_PID for this (Windows). Use qint64 QProcess::processId() const.

        1 Reply 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)
          
          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by
          #22

          @SPlatten said in Process and child process:

          QObject: Cannot create children for a parent that is in a different thread.

          That would explain a lot...! :)

          SPlattenS 1 Reply Last reply
          0
          • JonBJ JonB

            @SPlatten said in Process and child process:

            QObject: Cannot create children for a parent that is in a different thread.

            That would explain a lot...! :)

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

            @JonB, I think I said from the start these are two different processes, not threads in the same process.

            Kind Regards,
            Sy

            jsulmJ 1 Reply Last reply
            0
            • SPlattenS SPlatten

              @JonB, I think I said from the start these are two different processes, not threads in the same process.

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

              @SPlatten said in Process and child process:

              I think I said from the start these are two different processes

              This is clear.
              I asked because the warning you posted comes when using threads.

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

              1 Reply Last reply
              0
              • 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
                #25

                @JonB I tried start and it didn't work at all, so I'm back to startDetached which does launch the application and I can see it creates a log file then disappears.

                Kind Regards,
                Sy

                SPlattenS 1 Reply Last reply
                0
                • SPlattenS SPlatten

                  @JonB I tried start and it didn't work at all, so I'm back to startDetached which does launch the application and I can see it creates a log file then disappears.

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

                  @J-Hilk , @JonB , @jsulm, I've modified the function:

                  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());
                  
                          if ( intLastSep > 0 ) {
                              QString strName = strFullPath.mid(intLastSep + 1)
                                     ,strPath = strFullPath.mid(0, intLastSep + 1);
                              qdbg() << "Launching: " << strFullPath;
                              clsMainWnd::mspobjProcess->setArguments(slstArgs);
                              clsMainWnd::mspobjProcess->setWorkingDirectory(strPath);
                              clsMainWnd::mspobjProcess->setProgram(strName);
                              clsMainWnd::mspobjProcess->startDetached(&int64PID);
                          }
                      }    
                      if ( int64PID > 0 ) {
                          qdbg() << "Process: " << strFullPath << ", is running PID: " << QString::number(int64PID);
                          return true;
                      }
                      return false;
                  }
                  

                  I can see that a valid PID is returned and that the log files are created, but when I check for the process:

                  ps -A
                  

                  The PID isn't present.

                  Kind Regards,
                  Sy

                  JonBJ 1 Reply Last reply
                  0
                  • SPlattenS SPlatten

                    @J-Hilk , @JonB , @jsulm, I've modified the function:

                    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());
                    
                            if ( intLastSep > 0 ) {
                                QString strName = strFullPath.mid(intLastSep + 1)
                                       ,strPath = strFullPath.mid(0, intLastSep + 1);
                                qdbg() << "Launching: " << strFullPath;
                                clsMainWnd::mspobjProcess->setArguments(slstArgs);
                                clsMainWnd::mspobjProcess->setWorkingDirectory(strPath);
                                clsMainWnd::mspobjProcess->setProgram(strName);
                                clsMainWnd::mspobjProcess->startDetached(&int64PID);
                            }
                        }    
                        if ( int64PID > 0 ) {
                            qdbg() << "Process: " << strFullPath << ", is running PID: " << QString::number(int64PID);
                            return true;
                        }
                        return false;
                    }
                    

                    I can see that a valid PID is returned and that the log files are created, but when I check for the process:

                    ps -A
                    

                    The PID isn't present.

                    JonBJ Online
                    JonBJ Online
                    JonB
                    wrote on last edited by JonB
                    #27

                    @SPlatten
                    As we said before, if you want help on this you really need to put in more debugging information calls. Like slots for errorOccurred, finished, started, stateChanged, and reading of anything appearing on stdout/stderr, e.g. via readyReadStandardError/Output. And there is the exitCode(). Sorry, but that's how it is. I also offered to look for clues if you wanted to tell us the program you are executing and what arguments you pass , but you didn't reply yea or nay.

                    And btw you have something fishy going on if you can run this command via startDetached() but not via start().

                    And one other thing: the fact that you can't find the pid in itself proves nothing. It might be running fine. Processes can spawn sub-processes and exit.

                    SPlattenS 1 Reply Last reply
                    1
                    • JonBJ JonB

                      @SPlatten
                      As we said before, if you want help on this you really need to put in more debugging information calls. Like slots for errorOccurred, finished, started, stateChanged, and reading of anything appearing on stdout/stderr, e.g. via readyReadStandardError/Output. And there is the exitCode(). Sorry, but that's how it is. I also offered to look for clues if you wanted to tell us the program you are executing and what arguments you pass , but you didn't reply yea or nay.

                      And btw you have something fishy going on if you can run this command via startDetached() but not via start().

                      And one other thing: the fact that you can't find the pid in itself proves nothing. It might be running fine. Processes can spawn sub-processes and exit.

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

                      @JonB, I'll give it a go now.

                      Kind Regards,
                      Sy

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

                        @JonB , I've connected up slots to the following signals:

                        connect(clsMainWnd::mspobjProcess, &QProcess::errorOccurred, this, &clsMainWnd::qprocErrorOccurred);
                        connect(clsMainWnd::mspobjProcess, &QProcess::started, this, &clsMainWnd::qprocStarted);
                        connect(clsMainWnd::mspobjProcess, &QProcess::stateChanged, this, &clsMainWnd::qprocStateChanged);
                        

                        The implementation:

                        void clsMainWnd::qprocErrorOccurred(QProcess::ProcessError error) {
                           qdbg() << "QProcess errorOccurred:" << error;
                        }
                        void clsMainWnd::qprocStarted() {
                            qdbg() << "QProcess started";
                        }
                        void clsMainWnd::qprocStateChanged(QProcess::ProcessState newState) {
                            qdbg() << "QProcess stateChanged, newState:" << newState;
                        }
                        

                        I also put breakpoints in each slot, none of these got triggered. I'm not surprised though because I'm not suggesting the problem is with QProcess. I'm just trying to find out why the process when started this way is terminating where as if I launch it in Qt Creator it continues to run.

                        I would have though the "started" and "stateChanged" signals would get raised, but I don't get anything.

                        Kind Regards,
                        Sy

                        JonBJ 1 Reply Last reply
                        0
                        • SPlattenS SPlatten

                          @JonB , I've connected up slots to the following signals:

                          connect(clsMainWnd::mspobjProcess, &QProcess::errorOccurred, this, &clsMainWnd::qprocErrorOccurred);
                          connect(clsMainWnd::mspobjProcess, &QProcess::started, this, &clsMainWnd::qprocStarted);
                          connect(clsMainWnd::mspobjProcess, &QProcess::stateChanged, this, &clsMainWnd::qprocStateChanged);
                          

                          The implementation:

                          void clsMainWnd::qprocErrorOccurred(QProcess::ProcessError error) {
                             qdbg() << "QProcess errorOccurred:" << error;
                          }
                          void clsMainWnd::qprocStarted() {
                              qdbg() << "QProcess started";
                          }
                          void clsMainWnd::qprocStateChanged(QProcess::ProcessState newState) {
                              qdbg() << "QProcess stateChanged, newState:" << newState;
                          }
                          

                          I also put breakpoints in each slot, none of these got triggered. I'm not surprised though because I'm not suggesting the problem is with QProcess. I'm just trying to find out why the process when started this way is terminating where as if I launch it in Qt Creator it continues to run.

                          I would have though the "started" and "stateChanged" signals would get raised, but I don't get anything.

                          JonBJ Online
                          JonBJ Online
                          JonB
                          wrote on last edited by JonB
                          #30

                          @SPlatten
                          You have been changing: with the code you now display in this latest post, can you show how you start the sub-process, please paste the line.

                          SPlattenS 1 Reply Last reply
                          0
                          • JonBJ JonB

                            @SPlatten
                            You have been changing: with the code you now display in this latest post, can you show how you start the sub-process, please paste the line.

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

                            @JonB , the actual function:

                            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());
                            
                                    if ( intLastSep > 0 ) {
                                        QString strName = strFullPath.mid(intLastSep + 1)
                                               ,strPath = strFullPath.mid(0, intLastSep + 1);
                                        qdbg() << "Launching: " << strFullPath;
                                        clsMainWnd::mspobjProcess->setArguments(slstArgs);
                                        clsMainWnd::mspobjProcess->setWorkingDirectory(strPath);
                                        clsMainWnd::mspobjProcess->setProgram(strName);
                                        clsMainWnd::mspobjProcess->startDetached(&int64PID);                        
                                    }
                                }    
                                if ( int64PID > 0 ) {
                                    qdbg() << "Process: " << strFullPath << ", is running PID: " << QString::number(int64PID);
                                    return true;
                                }
                                return false;
                            }
                            

                            The call itself:

                            std::string strPort = std::to_string(clsMainWnd::intGetServerPort());
                            const char* cpszPort = strPort.data();
                            QString strFullPath(strModulesPath + strModule.toString());
                            QStringList slstArgs;
                            qint64 int64PID;
                            slstArgs << cpszPort;                                           //RX port
                            slstArgs << QString::number(clsSocketThread::uint16NextPort()); //TX port
                            
                            if ( clsMainWnd::blnLaunch(strFullPath, slstArgs, int64PID) == true ) {
                            

                            This is all working and the process is started I can see that because when the application starts it creates a log file:

                            -rw-r--r--  1 simonplatten  staff   201 21 Oct 17:33 mdFileIO20201021.001
                            

                            And this is its content:

                            D00000000000000000001:mdFileIO Version: 1.00
                            D00000000000000000002:Setting up socket on ip: 127.0.0.1, port: 8124
                            D00000000000000000003:Setting up socket on ip: 127.0.0.1, port: 8123, for heartbeat
                            

                            Then it dies...

                            Kind Regards,
                            Sy

                            JonBJ 1 Reply Last reply
                            0
                            • SPlattenS SPlatten

                              @JonB , the actual function:

                              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());
                              
                                      if ( intLastSep > 0 ) {
                                          QString strName = strFullPath.mid(intLastSep + 1)
                                                 ,strPath = strFullPath.mid(0, intLastSep + 1);
                                          qdbg() << "Launching: " << strFullPath;
                                          clsMainWnd::mspobjProcess->setArguments(slstArgs);
                                          clsMainWnd::mspobjProcess->setWorkingDirectory(strPath);
                                          clsMainWnd::mspobjProcess->setProgram(strName);
                                          clsMainWnd::mspobjProcess->startDetached(&int64PID);                        
                                      }
                                  }    
                                  if ( int64PID > 0 ) {
                                      qdbg() << "Process: " << strFullPath << ", is running PID: " << QString::number(int64PID);
                                      return true;
                                  }
                                  return false;
                              }
                              

                              The call itself:

                              std::string strPort = std::to_string(clsMainWnd::intGetServerPort());
                              const char* cpszPort = strPort.data();
                              QString strFullPath(strModulesPath + strModule.toString());
                              QStringList slstArgs;
                              qint64 int64PID;
                              slstArgs << cpszPort;                                           //RX port
                              slstArgs << QString::number(clsSocketThread::uint16NextPort()); //TX port
                              
                              if ( clsMainWnd::blnLaunch(strFullPath, slstArgs, int64PID) == true ) {
                              

                              This is all working and the process is started I can see that because when the application starts it creates a log file:

                              -rw-r--r--  1 simonplatten  staff   201 21 Oct 17:33 mdFileIO20201021.001
                              

                              And this is its content:

                              D00000000000000000001:mdFileIO Version: 1.00
                              D00000000000000000002:Setting up socket on ip: 127.0.0.1, port: 8124
                              D00000000000000000003:Setting up socket on ip: 127.0.0.1, port: 8123, for heartbeat
                              

                              Then it dies...

                              JonBJ Online
                              JonBJ Online
                              JonB
                              wrote on last edited by
                              #32

                              @SPlatten
                              At least you are using an instance! bool QProcess::startDetached(qint64 *pid = nullptr)

                              Returns true on success; otherwise returns false.

                              Please at least check the return result, we're trying to debug here....

                              I don't know why whatever you are running exits. If you really say you cannot start it instead via QProcess::start() I would want to know why. Although it may not matter with startDetached(), what is the scope/duration of your clsMainWnd::mspobjProcess object? I presume you're sure it does persist?

                              I would verify that my Qt code successfully launches some other program. Then I don't know what it is different about whatever you are trying to run. I would put the very small amount of code you have for launching this process into a very simple standalone program and see how that behaves.

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

                                @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?

                                Kind Regards,
                                Sy

                                SPlattenS 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

                                    @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.

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

                                    @JonB , @jsulm, just found:
                                    https://doc.qt.io/qt-5/ipc.html

                                    Kind Regards,
                                    Sy

                                    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