PDA

View Full Version : Kill a Process started with Qprocess ??



jesse_mark
28th November 2012, 15:41
Hello Guys,

I am using Qprocess to start an application, which will run for long time.

but i want to be able to kill this application after i start it, i tried to kill my Qprocess that i used to start that application but that did not work
as the Qprocess will has its own process and the application has different process after i start it.

i tried to use the pid(), but its the same too, where the pid() i get is the process id for the Qprocess not the application.

so could you please help me who can reach the application process and how can i kill it ??

Thank you

wysota
28th November 2012, 18:01
What exactly did you try?

jesse_mark
29th November 2012, 14:50
well

first i have myQprocess started my Application.

myQprcess.start(MyApplicatoin);

then i tried to stop my application from running,
i tried
myQprocess.kill();

this did not work.

then i tired to get the process id ,that is running the application.

qint64 id myQporcess.pid();

the id that i got is different than the process id that is running my application

when i used the "top" command to see the process id they are different, not the same.

so i dont know how can related between myQprocess and the process that is running my application, and how can i stop/kill it.

wysota
29th November 2012, 14:58
Please show your exact code, not pseudocode.

jesse_mark
29th November 2012, 16:53
well, i think i have to because you can not run the same application i'm running.

Let me give try to make it more clear:




void MyWindow::startApp(QString Command)
{
/* command = " Run -nn 1 -np 12 myApp"
where :
nn: number of nodes i want to use
np: number of processes per each node */

Myprocess->start(Command);

//I catch all the signals from the Myprcess , readyread, fininshe ..etc
}

void MyWindow::stopApp()
{

qint64 id = Myprocess->pid();
qDebug() << QString::number(id); // just to know the process id the was started before i kill it.

Myprocess->kill();

}


This is what i am doing.
so now i am just using one node, which my local machine, and forking 12 processes to run the application.

Now when i use the terminal to show the processes i have.

// the process id that im getting from Myprocess.pid() is >> 62524

before kill
qgrep Myapp.exe

result:
62524
64073
64074
64075
64076
64077
64078
64079
64080
64081
64082
64083


After Kill

pgrep myApp.exe

64073
64074
64075
64076
64077
64078
64079
64080
64081
64082
64083

only the first process get killed which the parent process that lead to fork all the other processes but the other process continue to works and the application too.

hope this make it more clear.

Thanks

wysota
29th November 2012, 19:49
So everything works fine. You are killing the process you started with QProcess. If those other processes have detatched from their parent, you have no control over them anymore. It's not a matter of QProcess but rather how your "Run" program works.

jesse_mark
29th November 2012, 20:14
Here i think i figure out what is my problem is :

1- I am using kill() witch will raise SIGKILL signal will send to the process which will not give it time to end all the other processes and clean.
I need to use terminate() witch will send SIGTERM signal to all it to end all the forked processes and end cleanly.

2- the command i using is actually calling a script witch will launch the real application.
so myQprocess has the idea for the script and when i kill it or terminate it, it wont affect the real application, it just ends the script with already started the application.

so what i think i need to do is to find a way to handle the child of myQprocess.

Is there away to reach the child of Qprocess ??

wysota
29th November 2012, 21:29
What child? There is no child of QProcess.

jesse_mark
29th November 2012, 23:11
the Qprocess make process to run the script, which will for a process to run the app.

so is not the process that was created by the script to run the app, is a child process to the process that is running the script ??

its a bit confusing

qprocess >>
run scrpit >>
run app.

is not run app is a child to qprocess ??

in numbers:

MyQprocess.start("script");

pgrep myapp

62011
62012

MyQprocess.pid() //= 62011

but 62012 is created by 62011 which The Qprocess.

wysota
29th November 2012, 23:38
Again, if the child process forks and creates a number of other processes that detach from their parent, you have no control over them. You could probably use platform dependent means to get a list of pids of child processes of a process but that's outside the scope of this forum.