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?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved General and Desktop
22 Posts 6 Posters 2.8k 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 Offline
    J Offline
    jiaming
    wrote on 1 Mar 2021, 10:48 last edited by
    #1

    I started A with qprocess, then A started B.
    When I use process.close (), only A is turned off and B is still running.
    B how to turn it off at the same time?

    J 1 Reply Last reply 1 Mar 2021, 10:58
    0
    • J jiaming
      1 Mar 2021, 10:48

      I started A with qprocess, then A started B.
      When I use process.close (), only A is turned off and B is still running.
      B how to turn it off at the same time?

      J Online
      J Online
      JonB
      wrote on 1 Mar 2021, 10:58 last edited by JonB 3 Jan 2021, 11:00
      #2

      @jiaming
      You can't. Your Qt app can only do something to the A sub-process it started, not some B sub-process which A starts. Hopefully when A terminates B will terminate too, but you have no control over that. I assume you're using QProcess::start() and not QProcess::startDetached(), FWIW.

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jiaming
        wrote on 1 Mar 2021, 11:10 last edited by
        #3

        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.

        J 1 Reply Last reply 1 Mar 2021, 11:27
        0
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 1 Mar 2021, 11:22 last edited by
          #4

          Then fix A so it properly shuts down B

          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 jiaming
            1 Mar 2021, 11:10

            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.

            J Online
            J Online
            JonB
            wrote on 1 Mar 2021, 11:27 last edited by JonB 3 Jan 2021, 11:27
            #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
            • J Offline
              J Offline
              JoeCFD
              wrote on 1 Mar 2021, 22:19 last edited by JoeCFD 3 Jan 2021, 22:20
              #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 C 2 Replies Last reply 2 Mar 2021, 02:10
              0
              • J JoeCFD
                1 Mar 2021, 22:19

                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 2 Mar 2021, 02:10 last edited by
                #7

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

                1 Reply Last reply
                0
                • J JoeCFD
                  1 Mar 2021, 22:19

                  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

                  C Offline
                  C Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 2 Mar 2021, 05:51 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 2 Mar 2021, 08:49 last edited by
                    #9

                    My program runs on Ubuntu

                    J 1 Reply Last reply 2 Mar 2021, 08:53
                    0
                    • C Offline
                      C Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on 2 Mar 2021, 08:53 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
                        2 Mar 2021, 08:49

                        My program runs on Ubuntu

                        J Online
                        J Online
                        JonB
                        wrote on 2 Mar 2021, 08:53 last edited by JonB 3 Feb 2021, 08:59
                        #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 2 Mar 2021, 09:04 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.

                          J J 2 Replies Last reply 2 Mar 2021, 09:52
                          0
                          • J jiaming
                            2 Mar 2021, 09:04

                            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.

                            J Online
                            J Online
                            JonB
                            wrote on 2 Mar 2021, 09:52 last edited by JonB 3 Feb 2021, 09:52
                            #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 2 Mar 2021, 11:32 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 J 3 Replies Last reply 2 Mar 2021, 11:54
                              0
                              • J jiaming
                                2 Mar 2021, 11:32

                                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 2 Mar 2021, 11:54 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
                                  2 Mar 2021, 11:32

                                  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 2 Mar 2021, 12:03 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
                                    2 Mar 2021, 11:32

                                    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.

                                    J Online
                                    J Online
                                    JonB
                                    wrote on 2 Mar 2021, 12:36 last edited by JonB 3 Feb 2021, 12:38
                                    #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
                                      2 Mar 2021, 09:04

                                      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.

                                      J Offline
                                      J Offline
                                      JoeCFD
                                      wrote on 2 Mar 2021, 20:39 last edited by JoeCFD 3 Feb 2021, 20:54
                                      #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 );
                                      }

                                      J 1 Reply Last reply 2 Mar 2021, 20:49
                                      0
                                      • J JoeCFD
                                        2 Mar 2021, 20:39

                                        @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 );
                                        }

                                        J Online
                                        J Online
                                        JonB
                                        wrote on 2 Mar 2021, 20:49 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
                                        • J Offline
                                          J Offline
                                          JoeCFD
                                          wrote on 2 Mar 2021, 21:03 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

                                          1/22

                                          1 Mar 2021, 10:48

                                          • Login

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