PDA

View Full Version : QProcess in qt-2.3



winarko
21st June 2008, 05:53
I want to run external program in my app (using qt-2.3), but there is no qprocess class in my qt version.
Is there any idea how to execute external program in qt-2.3?

jacek
23rd June 2008, 01:25
You can use standard C calls for that (assuming you have an OS).

winarko
26th June 2008, 14:20
Now, I'm using popen() function to run program, but it seemed like I can't stop that running program.
I've tried using pclose(), but it didn't work.
Can u tell me how to stop it?

regards,

jacek
27th June 2008, 01:24
pclose() just waits for the application to stop. If you can't convince that application to quit by talking to it through the pipe, you will have to signal it using kill() but for this you need its PID. I don't know if there is a reliable way to get a PID of a child process created with popen(), but you can always use fork+exec combo instead of popen().

winarko
29th June 2008, 11:06
Thank you..
I'm using fork+exec now, b'cause when i used popen my program hang up.
But the problem now I still can't kill the child process which I've made.
This is my source code to kill a child process:

pid_t pid;
pid = getpid();
kill(pid,0);

When I ran it after I created child process, my program ended, it seemed that I've also killed the parent process.
Do you have any idea?

jacek
29th June 2008, 16:51
When I ran it after I created child process, my program ended, it seemed that I've also killed the parent process.
Not "also" --- you kill the parent process. getpid() returns the PID of current process, so you have to use the PID returned by fork() instead.

winarko
2nd July 2008, 15:46
really?
so,how can i get pid created by fork and kill it?

thx.

jacek
2nd July 2008, 22:58
how can i get pid created by fork and kill it?
In parent process fork() returns PID of the child process.

pid_t pid = fork();
if( pid == 0 ) {
// child process
}
else if( pid > 0 ) {
// parent process
// pid == child's PID
}
else {
// fork failed (parent process)
}