Process

Process Management

The following procedures provide raw process management.

(mosh process) is not supported on Windows.

Summary
ProcessThe following procedures provide raw process management.
(mosh process)Process Management Library
Functions
forkFork the current process.
waitpidThis is an interface to waitpid(3), an extended version of wait.
spawnfork and exec.
pipeCreates a pipe, and returns two ports.

(mosh process)

Process Management Library

Summary
Functions
forkFork the current process.
waitpidThis is an interface to waitpid(3), an extended version of wait.
spawnfork and exec.
pipeCreates a pipe, and returns two ports.

Functions

fork

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.

Prototype

(fork)

Returns

0 if you’re in the child process, and a child process’ pid if you’re in the parent process

waitpid

This is an interface to waitpid(3), an extended version of wait. pid is an exact integer specifying which child(ren) to be waited.  If it is a positive integer, it waits fot that specific child.  If it is zero, it waits for any member of this process group.  If it is -1, it waits for any child process.  If it is less than -1, it waits for any child process whose process group id is equal to the absolute value of pid.

Prototype

(waitpid pid)

Parameters

pidpid of process to wait.

Returns

The return values are two exact integers, the first one is the child process id, and the second is a status code.

spawn

fork and exec.

Prototype

(spawn command args . io-list)

Parameters

commandcommand string to spawn.
argslist of command line arguments.
io-list(in out err). in, out and err should be binary-port or #f.  #f means use parent’s port.

Example

;; ls -l
(let-values ([(pid cin cout cerr) (spawn "ls" '("-l") (list #f #f #f))])
  (waitpid pid))

;; get output as string
(let-values ([(in out) (pipe)])
  (define (port->string p)
    (let loop ([ret '()][c (read-char p)])
      (if (eof-object? c)
          (list->string (reverse ret))
          (loop (cons c ret) (read-char p)))))
  (let-values ([(pid cin cout cerr) (spawn "ls" '("-l") (list #f out #f))])
    (close-port out)
    (write (port->string (transcoded-port in (make-transcoder (utf-8-codec)))))
    (close-port in)
    (waitpid pid)))

Returns

pid in out err is returned as multiple values.

pipe

Creates a pipe, and returns two ports.  The first returned port is an input port and the second is an output port.  The data put to the output port can be read from the input port.

Prototype

(pipe)

Returns

Two ports

Close