Solved How to close the subroutine of the program started by qprocess?
-
@JoeCFD
No kill function found. Do you need to import any files? -
@JoeCFD How should this help killing a sub-sub-process? Apart from this the OP is on windows...
-
My program runs on Ubuntu
-
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.
-
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 allB
processes, but I really wouldn't recommend it....] -
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.
-
@jiaming
Use Linuxman 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.
-
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.
-
@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...
-
@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
-
@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 onB
...!A process ID value of 1 indicates that there is no parent process associated with the [...] process."
-
@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 );
} -
@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.
-
Be aware that Control + C ( kill( pid, SIGINT ); ) may not kill your process all the time.
exec( "kill -9 pid" ) will be very clean. -
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.
-
@Kent-Dorfman thanks for sharing.