5. Bigloo -- Standard Library<br>5.8 Process support

5. Bigloo -- Standard Library 5.8 Process support

Browsing
Home: | Bigloo A ``practical Scheme compiler'' User manual for version 2.6b December 2003
Previous chapter: | Core Language
Next chapter: | Pattern Matching
Standard Library
5.1 | Scheme Library
| 5.1.1 | Booleans
| 5.1.2 | Equivalence predicates
| 5.1.3 | Pairs and lists
| 5.1.4 | Symbols
| 5.1.5 | Keywords
| 5.1.6 | Numbers
| 5.1.7 | Characters
| 5.1.8 | UCS-2 Characters
| 5.1.9 | Strings
| 5.1.10 | Unicode (UCS-2) Strings
| 5.1.11 | Vectors
| 5.1.12 | Control features
5.2 | Input and output
5.3 | Structures and Records
| 5.3.1 | Structures
| 5.3.2 | Records (SRFI-9)
5.4 | Serialization
5.5 | Bit manipulation
5.6 | Hash Tables
| 5.6.1 | Hash tables
| 5.6.2 | Deprecated Hash tables
5.7 | System programming
| 5.7.1 | Operating System interface
| 5.7.2 | Files
5.8 | Process support
5.9 | Socket support
5.10 | Date
5.11 | Posix Regular Expressions
| 5.11.1 | Regular Expressions Procedures
| 5.11.2 | Regular Expressions Pattern Language
| 5.11.3 | An Extended Example
Chapters
| Acknowledgements
1. | Table of contents
2. | Overview of Bigloo
3. | Modules
4. | Core Language
5. | Standard Library
6. | Pattern Matching
7. | Object System
8. | Threads
9. | Regular parsing
10. | Lalr(1) parsing
11. | Errors and Assertions
12. | Eval and code interpretation
13. | Macro expansion
14. | Command Line Parsing
15. | Explicit typing
16. | The C interface
17. | The Java interface
18. | Bigloo Libraries
19. | Extending the Runtime System
20. | SRFIs
21. | DSSSL support
22. | Compiler description
23. | User Extensions
24. | Bigloo Development Environment
25. | Global Index
26. | Library Index
| Bibliography
| Bigloo provides access to Unix-like processes as first class objects. The implementation and this documentation are to a great extent copies of the STk [Gallesio95] process support. Basically, a process contains four informations: the standard Unix process identification (aka PID) and the three standard files of the process.
run-process command arg... | bigloo procedure
`run-process` creates a new process and run the executable specified in `command`. The `arg` correspond to the command line arguments. When is process completes its execution, non pipe associated ports are automatically closed. Pipe associated ports have to be explicitly closed by the program. The following values of `p` have a special meaning: `input:` permits to redirect the standard input file of the process. Redirection can come from a file or from a pipe. To redirect the standard input from a file, the name of this file must be specified after `input:`. Use the special keyword `pipe:` to redirect the standard input from a pipe. `output:` permits to redirect the standard output file of the process. Redirection can go to a file or to a pipe. To redirect the standard output to a file, the name of this file must be specified after `output:`. Use the special keyword `pipe:` to redirect the standard output to a pipe. `error:` permits to redirect the standard error file of the process. Redirection can go to a file or to a pipe. To redirect the standard error to a file, the name of this file must be specified after `error:`. Use the special keyword `pipe:` to redirect the standard error to a pipe. `wait:` must be followed by a boolean value. This value specifies if the process must be ran asynchronously or not. By default, the process is run asynchronously (i.e. `wait:` if `#f`). `host:` must be followed by a string. This string represents the name of the machine on which the `command` must be executed. This option uses the external command `rsh`. The shell variable `PATH` must be correctly set for accessing it without specifying its absolute path. `fork:` must be followed by a boolean value. This value specifies if the process must substitute the current execution. That is, if the value is `#t` a new process is spawned otherwise, the current execution is stopped and replaced by the execution of `command`. `env:` must be followed by a string of the form ``var`=`val``. This will bound an environment variable in the spawned process. A `run-process` command may contain several `env:` arguments. The current variables of the current process are also passed to the new process. The following example launches a process which execute the Unix command `ls` with the arguments `-l` and `/bin`. The lines printed by this command are stored in the file `tmp/X`.
    (run-process "ls" "-l" "/bin" output: "/tmp/X")

The same example with a pipe for output:

    (let* ((proc (run-process "ls" "-l" "/bin" output: pipe:))
           (port (process-output-port proc)))
       (let loop ((line (read-line port)))
          (if (eof-object? line)
              (close-input-port port)
              (begin
                 (print line)
                 (loop (read-line port))))))

One should not that the same program can be written with explicit process handling but making use of the `|` notation for `open-input-file`.

    (let ((port (open-input-file "| ls -l /bin")))
       (let loop ((line (read-line port)))
          (if (eof-object? line)
              (close-input-port port)
              (begin
                 (print line)
                 (loop (read-line port))))))

Both input and output ports can be piped:

    (let* ((proc (run-process "/usr/bin/dc" output: pipe: input: pipe:))
           (inport (process-input-port proc))
           (port (process-output-port proc)))
       (fprint inport "16 o")
       (fprint inport "16 i")
       (fprint inport "10")
       (fprint inport "10")
       (fprint inport "+ p")
       (flush-output-port inport)
       (let loop ((line (read-line port)))
          (if (eof-object? line)
              (close-input-port port)
              (begin
                 (print line)
                 (loop (read-line port))))))   -| 20

Note: The call to `flush-output-port` is mandatory in order to get the `dc` process to get its input characters.
Note: Thanks to Todd Dukes for the example and the suggestion of including it this documentation.

process? obj | bigloo procedure
Returns `#t` if `obj` is a process, otherwise returns `#f`.

process-alive? process | bigloo procedure
Returns `#t` if `process` is currently running, otherwise returns `#f`.

close-process-ports command arg... | bigloo procedure
Close the three ports associated with a process. In general the ports should not be closed before the process is terminated.

process-pid process | bigloo procedure
Returns an integer value which represents the Unix identification (PID) of the `process`.

process-input-port process | bigloo procedure
process-output-port process | bigloo procedure
process-error-port process | bigloo procedure
Return the file port associated to the standard input, output and error of `process` otherwise returns `#f`. Note that the returned port is opened for reading when calling `process-output-port` or `process-error-port`. It is opened for writing when calling `process-input-port`.

process-wait process | bigloo procedure
This function stops the current process until `process` completion. This function returns `#f` when `process` is already terminated. It returns `#t` otherwise.

process-exit-status process | bigloo procedure
This function returns the exit status of `process` if it is has finished its execution. It returns `#f` otherwise.

process-send-signal process s | bigloo procedure
Sends the signal whose integer value is `s` to `process`. Value of `s` is system dependent. The result of `process-send-signal` is undefined.

process-kill process | bigloo procedure
This function brutally kills `process`. The result of `process-kill` is undefined.

process-stop process | bigloo procedure
process-continue process | bigloo procedure
Those procedures are only available on systems that support job control. The function `process-stop` stops the execution of `process` and `process-continue` resumes its execution.

process-list | bigloo procedure
This function returns the list of processes which are currently running (i.e. alive).

This Scribe page has been generated by `scribeinfo`.
Last update Tue Dec 16 13:46:41 2003