Solved How to close the subroutine of the program started by qprocess?
-
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? -
@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 usingQProcess::start()
and notQProcess::startDetached()
, FWIW. -
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. -
Then fix A so it properly shuts down B
-
@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.... -
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 -
@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.