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.
execReplace the current process with a new external command.
waitpidThis is an interface to waitpid(3), an extended version of wait.
spawnfork and exec.
pipeCreates a pipe, and returns two ports.
getpidReturns the process ID of the current interpreter process.
call-processRun an external command using a shell.
start-processRun an external command.
process-listReturns a process list as an a-list.
process-terminate!Kill process identified by OS dependent process identifier.

(mosh process)

Process Management Library

Summary
Functions
forkFork the current process.
execReplace the current process with a new external command.
waitpidThis is an interface to waitpid(3), an extended version of wait.
spawnfork and exec.
pipeCreates a pipe, and returns two ports.
getpidReturns the process ID of the current interpreter process.
call-processRun an external command using a shell.
start-processRun an external command.
process-listReturns a process list as an a-list.
process-terminate!Kill process identified by OS dependent process identifier.

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

exec

Replace the current process with a new external command.  The ports attached to the subprocess must be binary, and must be either file input ports or ports generated by the ‘pipe’ procedure.  They can also be #f which means the subprocess inherits the parent’s ports.

Prototype

(exec command args in out err . path-search? env)

Parameters

commandcommand string to spawn.
argslist of command line arguments.
ininput port to attach to standard input of the subprocess.
outoutput port to attach to standard output of the subprocess.
erroutput port to attach to standard error of the subprocess.
path-search?optional, defaults to #t.  If #t, command will be searched for in shell path, otherwise command must be an absolute path.
envoptional association list of environment variables as strings.  If specified, replaces the environment variables of the subprocess.  Use an empty list to empty the subprocess environment completely.

Returns

pid in out err is returned as multiple values.

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

3 values are returned

  • Child process ID as exact integer.
  • Exit status as exact integer, or #f on abnormal termination.
  • Signal number as exact integer if the process was signaled, or #f on normal termination.

spawn

fork and exec.

Prototype

(spawn command args . io-list path-search? env)

Parameters

The same as exec, except for io-list, which is an optional list of three ports representing in, out, err respectively.  (‘exec’ takes these values as separate arguments, and they are all required.)  If io-list is not provided, it defaults to ‘(#f #f #f), i.e. the subprocess will inherit the parent’s ports.

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

getpid

Returns the process ID of the current interpreter process.

Prototype

(getpid)

Returns

Process ID as exact integer

call-process

Run an external command using a shell.

Prototype

(call-process command)

Returns

3 values

  • The command’s complete output on stdout (stderr is not captured).
  • The exit status of the command, or #f if it terminated abnormally.
  • #f on normal termination, or the signal number if it was terminated by a signal.

start-process

Run an external command.  Unlike call-process, start-process doesn’t wait it’s terminatation.

Prototype

(start-process command . os-depedent-args)

process-list

Returns a process list as an a-list.  Keys of the a-list depend on OS.

Prototype

(process-list)

Returns

a process list as an a-list.  Keys of the a-list depend on OS.

process-terminate!

Kill process identified by OS dependent process identifier.

Prototype

(process-terminate! id)

Returns

#t if terminated otherwise #f.

Close