Next: , Up: posix process fork   [Index]


4.6.2.1 Forking a process

Function: fork
Function: fork parent-proc child-thunk

Interface to the C function fork(), (libc)fork. The behaviour depends upon the number of arguments:

When forking a process we must remember about input/output ports:

  1. Before forking, in the parent process we should call flush-ports-in-close-on-exec-mode; flush-ports-in-close-on-exec-mode, for details.
  2. After forking, in the child process we should call close-ports-in-close-on-exec-mode; close-ports-in-close-on-exec-mode, for details.

we must also remember that some file descriptors are marked to be “closed on exec”.

Here is a simple example of forking a process:

#!r6rs
(import (vicare)
  (prefix (vicare posix) px.))

(px.fork
 (lambda (child-pid)
   (printf "in parent pid = ~a, child pid = ~s\n"
	   (px.getpid) child-pid)
   (flush-output-port (current-output-port)))
 (lambda ()
   (printf "in child, pid = ~a\n" (px.getpid))
   (flush-output-port (current-output-port))
   (exit)))

(printf "here we are in the parent\n")
(flush-output-port (current-output-port))