[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

6.21.9.1 Fork and exec

Function: sys-system command
[POSIX] Runs command in a subprocess. command is usually passed to sh, so the shell metacharacters are interpreted.

This function returns an integer value system() returned. Since POSIX doesn't define what system() returns, you can't interpret the returned value in a portable way.

Function: sys-fork
[POSIX] Fork the current process. Returns 0 if you're in the child process, and a child process' pid if you're in the parent process. All the opened file descriptors are shared between the parent and the child. See fork(2) of your system for details.

If the forked process runs some Scheme code and exits instead of calling sys-exec, the forked process should call sys-exit to terminate itself. Normal exit call tries to flush the file buffers, and on some OS it messes up the parent's file buffers.

Function: sys-exec command args &optional iomap
[POSIX+] Execute command with args, a list of arguments. The current process image is replaced by command, so this function never returns.

All elements of args must be strings. The first element of args is used as argv[0], i.e. the program name.

The optional iomap argument, when provided, specifies how the open file descriptors are treated. It must be the following format:
 
((to-fd . from-port-or-fd) ...)
To-fd must be an integer, and from-port-or-fd must be an integer file descriptor or a port. Each element of the list makes the file descriptor of from-port-or-fd of the current process be mapped to the file descriptr to-fd in the executed process.

If iomap is provided, any file descriptors other than specified in the iomap list will be closed before exec(). Otherwise, all file descriptors in the current process remain open.

 
(sys-exec "ls" '("ls" "-l")) => ;; ls is executed.

(sys-exec "ls" '("ls" "-l") '((1 . 2) (1 . 1)))
   => ;; ls is executed, with its stderr and stdout are mapped to the
              current process's stdout.

When it encounters an error, most of the time it raises an error condition. Once the file descriptors are permuted, however, it would be impractical to handle errors in reasonable way (you don't even know stderr is still available!), so Gauche simply exits on the error.

See also 9.14 gauche.process - High Level Process Interface, which provides more convenient process handling on top of above primitives.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated by Ken Dickey on November, 28 2002 using texi2html