Frage

I'm using a List of QProcess objects to keep track of some processes that need to be start/stopped at user defined intervals.

I'm able to start and stop the processes OK. But the issue arises when I stop a process using the following methods (Pseudo code):

process->start("PathToProcess","Some Arguments");
//Do some stuff.
process->terminate();

However, if I try to start the process again at another time, I get the error:

QProcess::start:  Process is already running

I can do a ps -ef|grep processName and find that it is indeed dead, but it's sitting in a defunct state which I think is preventing me from starting it again.

What do I need to do to prevent this defunct state, or remove the defunct method so I can start my process again without reconstruction?

War es hilfreich?

Lösung

Figured out what was causing the error.

In qprocess_unix.cpp, you'll find a class called QProcessManager. Essentially this class has signal handlers that watch for child processes that have died. When a child dies, the QProcessManager sends a message across a pipe that lets the QProcess class know that it terminated/died.

In a unrelated part of my code, I had set up some signal catching statements that I used for various puposes. However, these signal catches were catching my SIGCHLD event and thus the QProcessManager was never being triggered to pipe to the QProcess that it died.

In my case, my only options are to either watch for the death of the child manually or to remove the signal catching I'm performing in my other sections of code.

For future reference, if you have this problem, you may be better off doing POSIX calls for kills and terminates, and checking the return value of those calls manually. If success, perform a:

process->setProcessState(ProcessState::NotRunning);//Specify the process is no longer running
waitpid(process->pid(),NULL,WNOHANG); //Clear the defunct process.

Thanks all.

Andere Tipps

Call process->waitForFinished() after calling process->terminate() in order to reap the zombie process. Then you can reuse the process object.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top