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. How to close the subroutine of the program started by qprocess?

How to close the subroutine of the program started by qprocess?

Scheduled Pinned Locked Moved Solved General and Desktop
22 Posts 6 Posters 6.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.
  • J jiaming

    I use QProcess::start()
    My program needs to open multiple A's, sometimes close A's, and then open A's, and so on.
    If B cannot be shut down, system resources will be exhausted.

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

    @jiaming
    As @Christian-Ehrlicher has said. And as I said earlier, what else would you like us to say? Just because you would like A to shut down B does not mean Qt app has any control over this, or that we can wave magic wand to make it so it does....

    1 Reply Last reply
    1
    • JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on last edited by JoeCFD
      #6

      Try this
      kill( m_process->pid(), SIGINT );
      before m_process is closed.

      or
      QString cmd = QString( "kill -9 %1" ).arg( m_process->pid() );
      call this
      exec( cmd.toStdString().c_str() );

      std::string exec(const char* cmd) {
      std::array<char, 128> buffer;
      std::string result;
      std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
      if (!pipe) {
      throw std::runtime_error("popen() failed!");
      }
      while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
      result += buffer.data();
      }
      return result;
      }
      from https://stackoverflow.com/questions/478898/how-do-i-execute-a-command-and-get-the-output-of-the-command-within-c-using-po

      J Christian EhrlicherC 2 Replies Last reply
      0
      • JoeCFDJ JoeCFD

        Try this
        kill( m_process->pid(), SIGINT );
        before m_process is closed.

        or
        QString cmd = QString( "kill -9 %1" ).arg( m_process->pid() );
        call this
        exec( cmd.toStdString().c_str() );

        std::string exec(const char* cmd) {
        std::array<char, 128> buffer;
        std::string result;
        std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
        if (!pipe) {
        throw std::runtime_error("popen() failed!");
        }
        while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
        result += buffer.data();
        }
        return result;
        }
        from https://stackoverflow.com/questions/478898/how-do-i-execute-a-command-and-get-the-output-of-the-command-within-c-using-po

        J Offline
        J Offline
        jiaming
        wrote on last edited by
        #7

        @JoeCFD
        No kill function found. Do you need to import any files?

        1 Reply Last reply
        0
        • JoeCFDJ JoeCFD

          Try this
          kill( m_process->pid(), SIGINT );
          before m_process is closed.

          or
          QString cmd = QString( "kill -9 %1" ).arg( m_process->pid() );
          call this
          exec( cmd.toStdString().c_str() );

          std::string exec(const char* cmd) {
          std::array<char, 128> buffer;
          std::string result;
          std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
          if (!pipe) {
          throw std::runtime_error("popen() failed!");
          }
          while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
          result += buffer.data();
          }
          return result;
          }
          from https://stackoverflow.com/questions/478898/how-do-i-execute-a-command-and-get-the-output-of-the-command-within-c-using-po

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #8

          @JoeCFD How should this help killing a sub-sub-process? Apart from this the OP is on windows...

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          1
          • J Offline
            J Offline
            jiaming
            wrote on last edited by
            #9

            My program runs on Ubuntu

            JonBJ 1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #10

              Then 'kill' should be available. But it still does not solve your problem. Fix your 'A' program to properly shut down it's spawned processes.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              2
              • J jiaming

                My program runs on Ubuntu

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

                @jiaming

                My program runs on Ubuntu

                And so? As I & @Christian-Ehrlicher have said, you cannot use this to kill a "sub-sub-process". You don't know the PID of the process you want to kill.

                Can you please take the time to address what @Christian-Ehrlicher wrote earlier:

                Then fix A so it properly shuts down B

                [Under Linux you could try using pkill to kill all B processes, but I really wouldn't recommend it....]

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  jiaming
                  wrote on last edited by
                  #12

                  I forgot to say that if A is opened in a terminal, B will also be closed when using Ctrl+C to close. What I am looking for is a method similar to sending a Ctrl+C signal.

                  JonBJ JoeCFDJ 2 Replies Last reply
                  0
                  • J jiaming

                    I forgot to say that if A is opened in a terminal, B will also be closed when using Ctrl+C to close. What I am looking for is a method similar to sending a Ctrl+C signal.

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

                    @jiaming
                    Use Linux man 2 kill call: kill(pid_of_A, SIGINT). But I doubt it will work from here.

                    It's nice to see that you ignore every suggestion we make.... Best of luck.

                    1 Reply Last reply
                    0
                    • J Offline
                      J Offline
                      jiaming
                      wrote on last edited by
                      #14

                      The main reason is that I didn't write A and B programs. They have formed a good system. It's troublesome for me to change them. It's better to have a simpler method.

                      I found that PPID of B is 1......

                      I'm sorry for the trouble.

                      jsulmJ JonBJ 3 Replies Last reply
                      0
                      • J jiaming

                        The main reason is that I didn't write A and B programs. They have formed a good system. It's troublesome for me to change them. It's better to have a simpler method.

                        I found that PPID of B is 1......

                        I'm sorry for the trouble.

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

                        @jiaming said in How to close the subroutine of the program started by qprocess?:

                        I found that PPID of B is 1

                        You need PID if you want to kill it...

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

                        1 Reply Last reply
                        0
                        • J jiaming

                          The main reason is that I didn't write A and B programs. They have formed a good system. It's troublesome for me to change them. It's better to have a simpler method.

                          I found that PPID of B is 1......

                          I'm sorry for the trouble.

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

                          @jiaming You could try to get PID from B using PID from A, see https://stackoverflow.com/questions/17743879/how-to-get-child-process-from-parent-process

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

                          1 Reply Last reply
                          0
                          • J jiaming

                            The main reason is that I didn't write A and B programs. They have formed a good system. It's troublesome for me to change them. It's better to have a simpler method.

                            I found that PPID of B is 1......

                            I'm sorry for the trouble.

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

                            @jiaming said in How to close the subroutine of the program started by qprocess?:

                            I found that PPID of B is 1......

                            Which is why I said I do not think that sending a SIGINT to the parent (A) will have any effect on B...!

                            A process ID value of 1 indicates that there is no parent process associated with the [...] process."

                            1 Reply Last reply
                            1
                            • J jiaming

                              I forgot to say that if A is opened in a terminal, B will also be closed when using Ctrl+C to close. What I am looking for is a method similar to sending a Ctrl+C signal.

                              JoeCFDJ Offline
                              JoeCFDJ Offline
                              JoeCFD
                              wrote on last edited by JoeCFD
                              #18

                              @jiaming kill( m_process->pid(), SIGINT ); <==this does what you need. You need somehow to find the pid of B.
                              std::string pid_str = exec( "pidof B" ); exec is defined in the post above and "pidof" is a linux command to find process id.
                              if( !pid_str.empty() ) {
                              int pid = std::stoi( pid_str );
                              kill( pid, SIGINT );
                              }

                              JonBJ 1 Reply Last reply
                              0
                              • JoeCFDJ JoeCFD

                                @jiaming kill( m_process->pid(), SIGINT ); <==this does what you need. You need somehow to find the pid of B.
                                std::string pid_str = exec( "pidof B" ); exec is defined in the post above and "pidof" is a linux command to find process id.
                                if( !pid_str.empty() ) {
                                int pid = std::stoi( pid_str );
                                kill( pid, SIGINT );
                                }

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

                                @JoeCFD said in How to close the subroutine of the program started by qprocess?:

                                You need somehow to find the pid of B.

                                But he doesn't know the PID of B.

                                1 Reply Last reply
                                0
                                • JoeCFDJ Offline
                                  JoeCFDJ Offline
                                  JoeCFD
                                  wrote on last edited by
                                  #20

                                  Be aware that Control + C ( kill( pid, SIGINT ); ) may not kill your process all the time.
                                  exec( "kill -9 pid" ) will be very clean.

                                  1 Reply Last reply
                                  0
                                  • Kent-DorfmanK Offline
                                    Kent-DorfmanK Offline
                                    Kent-Dorfman
                                    wrote on last edited by
                                    #21

                                    Nothing original to add, but I recommend all participants in this thread read http://morningcoffee.io/killing-a-process-and-all-of-its-descendants.html as he does a fair job explaining the relationships in POSIX environments.

                                    If you meet the AI on the road, kill it.

                                    JoeCFDJ 1 Reply Last reply
                                    3
                                    • Kent-DorfmanK Kent-Dorfman

                                      Nothing original to add, but I recommend all participants in this thread read http://morningcoffee.io/killing-a-process-and-all-of-its-descendants.html as he does a fair job explaining the relationships in POSIX environments.

                                      JoeCFDJ Offline
                                      JoeCFDJ Offline
                                      JoeCFD
                                      wrote on last edited by
                                      #22

                                      @Kent-Dorfman thanks for sharing.

                                      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