chickadee » daemon » daemon

daemon thunk #!key (cwd /) (killothers? #f) (stderr /dev/null-w) (stdin /dev/null-r) (stdout /dev/null-w) (want-pid? #f)procedure

The one and only procedure exported from the daemon module. Creates a background process to run the given thunk.

If want-pid? is #f returns #f; otherwise, and if everything goes well, returns the PID of the created process, as a fixnum.

As with all OS things, the devil is in the details -- implementation details do matter a great deal!

thunk
The thunk to run in the created daemon process.
cwd
The working directory of the created process. Defaults to "/". If #f is given, doesn't change the working directory (which will be inherited from the parent process P). If the given value is a string, changes to that directory. Otherwise, uses the default value.
killothers?
Check chicken.process.process-fork for details. Defaults to #f.
stderr, stdin, stdout
These change the (current-error-port), (current-input-port), and (current-output-port) available to the thunk. All three default to /dev/null. Legal values for these parameters are input/output ports (whichever is appropriate); a string designating the path to a file (absolute, or relative to cwd); a fixnum designating a file descriptor; a promise that may be forced or a thunk that may be called, that evaluate to an input/output port.
want-pid?
In some circumstances it is useful to know the PID of the created process. If want-pid? is given and not #f, then the calling process P waits for C2 to be created and for C1 to report back C2's PID; otherwise, the procedure returns #f immediately. The implementation is a hack: P opens a one-way pipe from C1; when C1 forks for C2, C1 writes C2's PID to the pipe; finally P reads the PID from the pipe. Lots could go wrong here, but it's hard to know beforehand of all the possible failure causes.