rasjani rasjani - 1 year ago 48
C++ Question

Portable way to "fork()" in Qt4 application?

Say, I need to run a bunch of code that is prone to crash so I need to run it on a different process. Typically I'd do it like this:

pid = fork();
if (pid == -1) {
std::cout << "Cant Spawn New Thread";
exit(0);
} else if (pid == 0) {
std::cout << "Im a child that will crash\n";
char *foo = (char *) 0xffff;
std::cout << foo;
exit(0);
} else {
std::cout << "PID: " << pid << "\n";
}
do {
std::cout << "Waiting for " << pid << " to finish .. \n";
pid_w = waitpid(pid,&status,0);
} while (pid_w == -1);


Obviously I can just use fork in my Qt4 application but I'm wondering if can I archive same functionality with any anything that Qt4 provides or any portable manner without resorting to having bunch of architecture
#ifdefs
?

In any case, I'm targeting this app to have only
pthread
implementation but I'd still like to keep things as much close to "native" Qt API as possible.

I've tested
QThread
, and segfaulting in thread crashes the whole application obviously and it seems that
QProcess
is only targetted to be used when spawning completely different executables. Any other alternatives ?

Answer Source

Windows flat-out doesn't have fork() in any publicly consumable way, so there's no Qt call to emulate it; you'll need to do something like start yourself with special command-line params or something.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download