[Qt-interest] Is it safe to fork() after an app.exec()

Thiago Macieira thiago.macieira at trolltech.com
Mon Jun 1 22:45:17 CEST 2009


Will Rutherdale (rutherw) wrote:
>Hi.
>
>In writing a console application using QCoreApplication, someone came up
>with the idea of creating a self-starting program that processes
>command-line arguments, then decides whether it wants to start itself as
>a server in the background.  It is already running a method in
>QCoreApplication, but depending on a certain argument may decide to
>fork.  The child process on the other side of the fork() call then
>invokes QCoreApplication::exec().  From that point on you have a server
>process listening for commands from dbus.
>
>My question:  is this safe to do?  Testing so far seems to indicate that
>it runs without a hitch.  However, without knowing the sources in Qt in
>depth, I wonder if the code might ever get confused by the fact that the
>pid has changed.
>
>If there is a problem, then I could just start this program out of a
>shell using ampersand notation.  (Or possibly fork() first?)

If you fork() and you have any sockets or files or other such resources 
open, you must make sure that only one of the two processes ever touches 
those resources.

The easiest and best way to guarantee that is to get either the parent or 
the child process to call _exit(2). That is really with the underscore, 
not exit(3).

-- 
Thiago Macieira - thiago.macieira (AT) nokia.com
  Senior Product Manager - Nokia, Qt Software
      Sandakerveien 116, NO-0402 Oslo, Norway
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090601/bb5e76da/attachment.bin 


More information about the Qt-interest-old mailing list