Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Qt support forked processes

Tags:

c++

qt

When I fork() in a Qt application, what parts of Qt behave sane? Does Qt support this?

Obviously, e.g. the GUI on MacOSX will not work because Cocoa itself does not support forked processes.

But there are many other parts, e.g. the list of threads, etc.

QCoreApplication::applicationPid() seems to return the wrong value. (According to here.)

Or to put the question a bit different: I must fork() in my app and there are certain parts which might access Qt in the child process. Where do I need to take extra care, despite all the Qt GUI stuff?

like image 896
Albert Avatar asked Feb 12 '14 11:02

Albert


2 Answers

TL;DR "What can I use from Qt in the fork?" "Anything, as long as you prove to yourself, via code audit, that there are no bugs there related to forking".

None of this is tested for, so nobody knows if anything related to forking used to work, and got broken, and whether it still works.

Qt has a continuous integration system and it is tested on multiple platforms as a prerequisite for staging the changes into a future release. This includes testing for performance regressions in the key areas. IOW, it's not a joke of a test suite, it's for real. The fact that forking is not addressed in the test suite should be a strong signal to you that you're on your own here.

So, you need to take care everywhere, and you need to audit the entirety of Qt code that you call. Sorry, that's the only reasonable answer.

like image 145
Kuba hasn't forgotten Monica Avatar answered Oct 19 '22 05:10

Kuba hasn't forgotten Monica


On MeeGo there was a daemon applauncherd which boosted start up time of Qt applications by forking it when it was needed. So in general it is possible, but as you noticed QCoreApplication::applicationPid() returns wrong value. I far as I known MeeGo used a slightly modified version of Qt. It is open source project so you can inspect code how they fixed the problem.

Another thing when you are using fork then you loost portability, so once you have used fork you can use fork related API and don't use QCoreApplication::applicationPid() but use directly getpid function.

like image 1
Marek R Avatar answered Oct 19 '22 07:10

Marek R