How to use the spawn function in Rexx

Overview

Sometimes it is necessary to start a separate process (child process) from a Rexx program in batch. For this task, there is the function spawn. Unfortunately, the information content of the documentation is rather limited on this feature.

For this reason, we show in this article how simple it really is to use the function. In our example, a Java program is to be started as a child process.

The general call to the function is:

spawn pathname fd_count fd_map arg_stem env_stem

The data exchange between the Rexx program and the child process takes place over the three standard channels: STDIN, STDOUT and STDERR. These are passed in the system fd_map to the child process. And that's about the first hurdle. The ddnames from the JCL DOES NOT WORK. The function expects a file handle. This file handle you get in two ways: 

  • Create/open a physical file, for example /tmp/stdin.txt
  • Using Pipes

Unnecessary resources should not be created. For communication between Rexx program and child process pipes are used.

First of all, a pipe is to be created for the three channels (STDIN, STDOUT and STDERR).

"pipe stdin."
"pipe stdout."
"pipe stderr."

stem.1 is to read from the pipe and stem.2 is to write to the pipe. The fd_map looks like this:

map.0 = stdin.1
map.1 = stdout.2
map.2 = stderr.2

Now it can be written in the standard input. For simplicity, here is a small procedure:

writeLine:
  buf = arg(1) !! esc_n
  len = length(buf)
  "write" stdin.2 "buf"
return;

In the next step, the program to be called is defined. In our example, it is a Java program named RexxSpawnSample.

parm.0 = 2
parm.1 = '/usr/lpp/java/J7.0/bin/java'
parm.2 = 'RexxSpawnSample'

For the successful call, some environment variables must be set (PATH, CLASSPATH, etc.). This is done via entries in a stem.

env.0 = 3
env.1 = "PATH=.:/usr/lpp/java/J7.0/bin"
env.2 = "JAVA_HOME=/usr/lpp/java/J7.0"
env.3 = "CLASSPATH=/my/home/java/classes:.:"

Before the start, one side of the STDIN pipe has to be closed. That's all. Now, the Java program can be accessed from spawn. The status of the call should be queried via the variable RETVAL.

"close" stdin.2

'spawn' parm.1 '3 map. parm. env.'

spid = RETVAL
serrno = ERRNO

if spid==-1 then do
   str ='unable to spawn' parm.1', errno='serrno
   say str
   exit serrno
end

'waitpid (spid) waitpid. 0'
javarc = waitpid.W_EXITSTATUS

exit javarc
comments powered by Disqus