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. Kill a process and its children
Forum Updated to NodeBB v4.3 + New Features

Kill a process and its children

Scheduled Pinned Locked Moved Solved General and Desktop
34 Posts 8 Posters 12.5k Views 4 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.
  • H Offline
    H Offline
    hbatalha
    wrote on last edited by hbatalha
    #16

    I thank everyone's reply here, I was able to find a solution for windows by, as suggested in the replies, sending SIGNT signal to the parent process.

    I found the solution in @JonB referenced SO post in this answer and translated it into Qt code:

    QProcess::execute("taskkill", {"/pid", QString::number(process->processId()), "/t", "/f"});
    

    I am yet to test the QProcess::kill behavior in Linux. It would be a bonus if someone who knows telling me.

    JoeCFDJ 1 Reply Last reply
    0
    • H hbatalha

      I thank everyone's reply here, I was able to find a solution for windows by, as suggested in the replies, sending SIGNT signal to the parent process.

      I found the solution in @JonB referenced SO post in this answer and translated it into Qt code:

      QProcess::execute("taskkill", {"/pid", QString::number(process->processId()), "/t", "/f"});
      

      I am yet to test the QProcess::kill behavior in Linux. It would be a bonus if someone who knows telling me.

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

      @hbatalha kill( m_process->processId(), SIGINT ); is same as Ctrl + C.
      will work on both Windows and Linux. kill() is c code. You do not need taskkill pid on Windows and kill -9 pid on Linux.

      H 1 Reply Last reply
      0
      • JoeCFDJ JoeCFD

        @hbatalha kill( m_process->processId(), SIGINT ); is same as Ctrl + C.
        will work on both Windows and Linux. kill() is c code. You do not need taskkill pid on Windows and kill -9 pid on Linux.

        H Offline
        H Offline
        hbatalha
        wrote on last edited by
        #18

        @JoeCFD in which header will I find the kill function?

        JoeCFDJ 1 Reply Last reply
        0
        • H hbatalha

          @JoeCFD in which header will I find the kill function?

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

          @hbatalha #include <signal.h>

          H 1 Reply Last reply
          0
          • JoeCFDJ JoeCFD

            @hbatalha #include <signal.h>

            H Offline
            H Offline
            hbatalha
            wrote on last edited by
            #20

            @JoeCFD said in Kill a process and its children:

            @hbatalha #include <signal.h>

            I am still getting undeclared identifier 'kill' error after including <signal.h>

            JoeCFDJ 1 Reply Last reply
            0
            • H hbatalha

              @JoeCFD said in Kill a process and its children:

              @hbatalha #include <signal.h>

              I am still getting undeclared identifier 'kill' error after including <signal.h>

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

              @hbatalha https://man7.org/linux/man-pages/man2/kill.2.html

              H 1 Reply Last reply
              0
              • JoeCFDJ JoeCFD

                @hbatalha https://man7.org/linux/man-pages/man2/kill.2.html

                H Offline
                H Offline
                hbatalha
                wrote on last edited by
                #22

                @JoeCFD that's for linux, I was asking for MingW equivalent

                JoeCFDJ 1 Reply Last reply
                0
                • jeremy_kJ Offline
                  jeremy_kJ Offline
                  jeremy_k
                  wrote on last edited by
                  #23

                  kill() is a posix API. It isn't part of standard C or C++.

                  Asking a question about code? http://eel.is/iso-c++/testcase/

                  1 Reply Last reply
                  2
                  • H hbatalha

                    @JoeCFD that's for linux, I was asking for MingW equivalent

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

                    @hbatalha Sorry. My bad. It is not for Windows. I thought it is standard C code.
                    You may try kill -9 pid on MingW.

                    H 1 Reply Last reply
                    0
                    • JoeCFDJ JoeCFD

                      @hbatalha Sorry. My bad. It is not for Windows. I thought it is standard C code.
                      You may try kill -9 pid on MingW.

                      H Offline
                      H Offline
                      hbatalha
                      wrote on last edited by
                      #25

                      @JoeCFD what about taskkill that I am currently using?

                      JoeCFDJ 1 Reply Last reply
                      0
                      • H hbatalha

                        @JoeCFD what about taskkill that I am currently using?

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

                        @hbatalha if it works, it should be ok. Ctrl+C is cleaner, I believe.

                        https://stackoverflow.com/questions/813086/can-i-send-a-ctrl-c-sigint-to-an-application-on-windows

                        H 1 Reply Last reply
                        1
                        • JoeCFDJ JoeCFD

                          @hbatalha if it works, it should be ok. Ctrl+C is cleaner, I believe.

                          https://stackoverflow.com/questions/813086/can-i-send-a-ctrl-c-sigint-to-an-application-on-windows

                          H Offline
                          H Offline
                          hbatalha
                          wrote on last edited by
                          #27

                          @JoeCFD Ok, thanks for your input in this post, I appreciate it.

                          kshegunovK 1 Reply Last reply
                          0
                          • H hbatalha

                            @JoeCFD Ok, thanks for your input in this post, I appreciate it.

                            kshegunovK Offline
                            kshegunovK Offline
                            kshegunov
                            Moderators
                            wrote on last edited by
                            #28

                            What you want is QProcess::terminate, which does the right thing™, and the right thing is to traverse the threads of the process and post the WM_QUIT message to each one that runs an event loop. If the process doesn't run an event loop, then the program you're trying to run was never (ever) intended to be controlled from the outside and you should open a feature request for its developers. Coincidentally if it doesn't integrate into the windows event queue, then don't run it, or at least don't expect to (semi)magically be able to control it from another process.

                            Don't kill processes, just as you don't (assumedly) go around killing kittens. It's called kill for a good reason and it's bad and ugly, and you shan't do it unless you've exhausted every other means, which you haven't.

                            Read and abide by the Qt Code of Conduct

                            H 2 Replies Last reply
                            1
                            • kshegunovK kshegunov

                              What you want is QProcess::terminate, which does the right thing™, and the right thing is to traverse the threads of the process and post the WM_QUIT message to each one that runs an event loop. If the process doesn't run an event loop, then the program you're trying to run was never (ever) intended to be controlled from the outside and you should open a feature request for its developers. Coincidentally if it doesn't integrate into the windows event queue, then don't run it, or at least don't expect to (semi)magically be able to control it from another process.

                              Don't kill processes, just as you don't (assumedly) go around killing kittens. It's called kill for a good reason and it's bad and ugly, and you shan't do it unless you've exhausted every other means, which you haven't.

                              H Offline
                              H Offline
                              hbatalha
                              wrote on last edited by
                              #29

                              @kshegunov said in Kill a process and its children:

                              What you want is QProcess::terminate

                              QProcess::terminate doesn't do anything when I call it on Windows

                              and the right thing is to traverse the threads of the process and post the WM_QUIT message to each one that runs an event loop.

                              How do you propose to do that?

                              then the program you're trying to run was never (ever) intended to be controlled from the outside and you should open a feature request for its developers.

                              Could you elaborate?

                              kshegunovK 1 Reply Last reply
                              0
                              • kshegunovK kshegunov

                                What you want is QProcess::terminate, which does the right thing™, and the right thing is to traverse the threads of the process and post the WM_QUIT message to each one that runs an event loop. If the process doesn't run an event loop, then the program you're trying to run was never (ever) intended to be controlled from the outside and you should open a feature request for its developers. Coincidentally if it doesn't integrate into the windows event queue, then don't run it, or at least don't expect to (semi)magically be able to control it from another process.

                                Don't kill processes, just as you don't (assumedly) go around killing kittens. It's called kill for a good reason and it's bad and ugly, and you shan't do it unless you've exhausted every other means, which you haven't.

                                H Offline
                                H Offline
                                hbatalha
                                wrote on last edited by
                                #30

                                @kshegunov Also QProcess::terminate :

                                Console applications on Windows that do not run an event loop, or whose event loop does not handle the WM_CLOSE message, can only be terminated by calling kill().

                                1 Reply Last reply
                                0
                                • H hbatalha

                                  @kshegunov said in Kill a process and its children:

                                  What you want is QProcess::terminate

                                  QProcess::terminate doesn't do anything when I call it on Windows

                                  and the right thing is to traverse the threads of the process and post the WM_QUIT message to each one that runs an event loop.

                                  How do you propose to do that?

                                  then the program you're trying to run was never (ever) intended to be controlled from the outside and you should open a feature request for its developers.

                                  Could you elaborate?

                                  kshegunovK Offline
                                  kshegunovK Offline
                                  kshegunov
                                  Moderators
                                  wrote on last edited by
                                  #31

                                  @hbatalha said in Kill a process and its children:

                                  QProcess::terminate doesn't do anything when I call it on Windows

                                  In that case don't run such processes at all. Sorry to break it to you, but you're trying to fit a square peg into a round hole and you actually think this is the right thing to do.

                                  Anecdote time (warning metaphor incoming) ...

                                  You go on this much anticipated date ... you like the girl, you want to have a meaningful conversation with her. You try this and that but somehow it doesn't work out. You stand up to leave but instead of saying a polite goodbye and just walking out, like any regular guy, you reach for and take a baseball bat and beat your date senseless ...

                                  ... this in a nutshell is what running a process and then killing it is ...

                                  Read and abide by the Qt Code of Conduct

                                  H 1 Reply Last reply
                                  3
                                  • kshegunovK kshegunov

                                    @hbatalha said in Kill a process and its children:

                                    QProcess::terminate doesn't do anything when I call it on Windows

                                    In that case don't run such processes at all. Sorry to break it to you, but you're trying to fit a square peg into a round hole and you actually think this is the right thing to do.

                                    Anecdote time (warning metaphor incoming) ...

                                    You go on this much anticipated date ... you like the girl, you want to have a meaningful conversation with her. You try this and that but somehow it doesn't work out. You stand up to leave but instead of saying a polite goodbye and just walking out, like any regular guy, you reach for and take a baseball bat and beat your date senseless ...

                                    ... this in a nutshell is what running a process and then killing it is ...

                                    H Offline
                                    H Offline
                                    hbatalha
                                    wrote on last edited by
                                    #32

                                    @kshegunov said in Kill a process and its children:

                                    In that case don't run such processes at all. Sorry to break it to you, but you're trying to fit a square peg into a round hole and you actually think this is the right thing to do.

                                    But I am running a console application that by the looks of it is a

                                    Console applications on Windows that do not run an event loop, or whose event loop does not handle the WM_CLOSE message, can only be terminated by calling kill().

                                    Source: QProcess::terminate

                                    @kshegunov said in Kill a process and its children:

                                    ... this in a nutshell is what running a process and then killing it is ...

                                    But running this program is unavoidable in my app, so what other options do I have on Windows?

                                    kshegunovK 1 Reply Last reply
                                    0
                                    • H hbatalha

                                      @kshegunov said in Kill a process and its children:

                                      In that case don't run such processes at all. Sorry to break it to you, but you're trying to fit a square peg into a round hole and you actually think this is the right thing to do.

                                      But I am running a console application that by the looks of it is a

                                      Console applications on Windows that do not run an event loop, or whose event loop does not handle the WM_CLOSE message, can only be terminated by calling kill().

                                      Source: QProcess::terminate

                                      @kshegunov said in Kill a process and its children:

                                      ... this in a nutshell is what running a process and then killing it is ...

                                      But running this program is unavoidable in my app, so what other options do I have on Windows?

                                      kshegunovK Offline
                                      kshegunovK Offline
                                      kshegunov
                                      Moderators
                                      wrote on last edited by
                                      #33

                                      @hbatalha said in Kill a process and its children:

                                      But running this program is unavoidable in my app, so what other options do I have on Windows?

                                      Use a different program, one that's aware it may be notified from the outside (i.e. one running an event loop), or open a feature request with the developers of the one you'd wanted to use.

                                      Read and abide by the Qt Code of Conduct

                                      H 1 Reply Last reply
                                      1
                                      • kshegunovK kshegunov

                                        @hbatalha said in Kill a process and its children:

                                        But running this program is unavoidable in my app, so what other options do I have on Windows?

                                        Use a different program, one that's aware it may be notified from the outside (i.e. one running an event loop), or open a feature request with the developers of the one you'd wanted to use.

                                        H Offline
                                        H Offline
                                        hbatalha
                                        wrote on last edited by
                                        #34

                                        @kshegunov said in Kill a process and its children:

                                        Use a different program, one that's aware it may be notified from the outside (i.e. one running an event loop), or open a feature request with the developers of the one you'd wanted to use.

                                        Ok thanks for the info, really appreciate it.

                                        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