Processes and threads Isis software documentation

Processes and threads

This document explains the process control utilities in Isis, including routines for executing programs, manipulating environment variables, sending signals, and creating threads.


Environment variables

(get-env varname) # get value of environment variable (set-env varname stringval) # set value of environment variable These functions retrieve and store values in the program's environment. Both the variable name and value should be strings. get-env returns Null if the variable is not found.
-> (get-env "TERM") "xterm" -> (set-env "TERM" "vt100") "vt100"


Program execution

(execute progname args ...) # execute a program (exec progname arg1 arg2 ...) # execute a program, pass each arg separately (system command-string) # run a command in the sh shell execute accepts one or more strings that together contain a command and arguments to execute. If successful, it will not return because the new program will be running. Otherwise an error will be printed and Null returned. exec works the same way except that each program argument must be passed as a separate string (which allows spaces to be in arguments).

system runs a command in the sh shell, and returns the program's exit status, or -1 if unsuccessful. Calling system with no arguments returns True or False depending on whether an sh command interpreter is available.

-> (system "date") Mon Aug 16 15:51:48 EDT 1999 0 -> (execute "boguscommand") ** exec: Unable to execute boguscommand. Null -> (execute "xv picture.jpg")


Process information

(get-process-id) # get id of current process (get-parent-id) # get id of parent process (get-user-id) # get user id of the current process (get-group-id) # get group id of the current process These routines take no arguments and return integers.
-> (get-process-id) 4017 -> (get-parent-id) 1313 -> (get-user-id) 31443 -> (get-group-id) 2000


Process creation and control

(fork) # create a new process identical to the current one, return pid (wait) # block until any child completes, return [pid status] (wait pid) # block until a particular child completes, return [pid status] fork creates a new process identical to the original. It returns 0 to the child process and a non-zero child process id to the parent process. The most common use of fork is to create a child process and execute a different program in that process using exec.

wait blocks until a child process exits. Both functions accept either no arguments or an integer process id. When no argument is passed, the status of any child process is returned. The return value is a list containing the process id and the status (0 if the process no longer exists or exited).

(set pid (fork)) # create new process (if (= 0 pid) # if this is the child process (begin (exec "xclock") # run a command (exit))) # exit if exec returns in failure (print "Child id: " pid newline) # this is what happens in the parent (set status (wait pid)) # wait for child to complete (print "Child completed with exit status: " (status 1) newline)


Inter-process communication

(pipe) # create a pipe for inter-process communication (dup fdport fdport) # duplicate a unix file descriptor port into another (send-signal pid signal) # send a signal to a process The pipe function will return a list of two file handles, the first open for reading and the second open for writing. Data written to the second file handle shows up for reading from the first file handle. If a pipe is created before a fork command, it can be used to communicate between the parent and child processes.

The dup function is normally used in Unix to duplicate a file handle into the standard input or standard output of a program about to be executed. Passing two integers or Unix FD ports closes the first port, and duplicates the first port into the second port. pipe and dup are often used in conjunction with each other to set up a new process whose standard input and output are connected to ports in an Isis process. For more information on these commands, please see their manual pages, or read a book on Unix programming.

The example function below creates a pipe, creates a new process, duplicates the input end of the pipe into the standard input (file handle 0) of the new process, and then runs the line printer program. Thus, any data written to the other end of the pipe will be transferred into the standard input of the "lpr" command and appear on the printer!

(set send-text-to-printer (proc (text) (local (pipeinfo iport oport pid) (begin (set pipeinfo (pipe)) # create pipe (set iport (pipeinfo 0)) # this is the input end of the pipe (set oport (pipeinfo 1)) # this is the output end of the pipe (if (= 0 (set pid (fork))) # create new process (begin # if this is the child process, (close oport) # output end of pipe not needed in the child (dup iport 0) # duplicate input end into to the standard input (0) (exec "lpr"))) # execute the line printer program (close iport) # input end of pipe not needed in the parent (write-string oport text) # write text to lpr process (close oport))))) # close output end of pipe
send-signal sends a signal to the process and returns True if successful or False if not. See below for a list of possible signals to send. Some of the most useful signals are perhaps sig-interrupt and sig-kill which terminate the receiving process.
-> (send-signal 23904 sig-kill) True -> (send-signal 1 sig-kill) False
The following constants may be used for the signal argument:
sig-hangup sig-interrupt sig-quit sig-illegal-instruction sig-trace-trap sig-abort sig-floating-exception sig-kill sig-bus sig-segmentation-fault sig-pipe sig-alarm sig-urgent sig-stop sig-interactive-stop sig-continue sig-child sig-tty-input sig-tty-output sig-io sig-window-change sig-user-1 sig-user-2


Utilities

(send-text-to-printer string) (send-data-to-printer numbytes address) These functions pipe input to the line printer program "lpr" on your machine.


Threads

(spawn proc arg arg ...) # run a procedure in a new thread, return thread-id (join thread-id) # block until thread finishes, return result (self) # return thread id of current thread A thread is much like a "process" in a computer--it represents a flow of instructions that run concurrently with other "threads" in the same process. Threads are different from processes in that they share the same memory space and resources. Isis allows you to create threads and run procedures in them. When the threads finish, you can retrieve the results of those applications, if desired.

Threads in Isis are still under development and available for you to use on a trial basis only, and you must use a special interpreter called multi-isis. The thread interface is currently very simple. To create a new thread running a procedure, use the spawn operator, which will return a thread identifier or Null if an error occurred. join waits for the specified thread to finish and returns the result of the procedure application that was running it it, or Null if an error occurred. self returns the thread identifier of the current thread.


Requirements:
Scripts: (load "timer.isis")
Libraries: -lisis_unix
Headers: isis_unix.h
Binders: bind_unix_library(script);


Thanks to Ann Bui for her contributions to this library.