Skip to main content

Local Interprocess Communication

This chapter describes how to set up communication between local Caché processes, and with other processes outside of Caché.

For information on remote Client/Server communications using TCP/IP, refer to the TCP Client/Server Communication chapter of this manual.

Using Pipes to Communicate with Processes

You can communicate between your Caché processes and external UNIX® or Windows processes through a pipe, just as at the UNIX® or Windows operating system level. You can send output to or receive input from the pipe. The pipe is one-way; you cannot read from and write to the same program at the same time.

When you open a pipe to another program for output, you can write to it as if it were a sequential file. The program then uses what you have written as its input stream. This capability is especially helpful when you want Caché processes to share resources with external processes.

For example, many users run Caché and a word processing program together and want the two applications to share printers properly. Caché assumes it has full access and responsibility for letting processes send information to devices. However, most UNIX® applications rely on a standard UNIX® utility, lpsched, to schedule access to the printer and spooling files.

When these UNIX® applications need to print, they call a utility called lp or lpr, instead of writing directly to the printer port. The lp (or lpr) utility then invokes lpsched, which in turn schedules access to the printer for the job from which lp (or lpr) was called. When you use lp, you do not need to wait for printing to occur. As soon as you have finished writing your print job to lp, you simply close the file; lp takes care of spooling the job to disk while awaiting its turn to print.

Caché lets you join in this cooperative environment by an extension to the OPEN command. You can issue this command directly, or through Caché utilities that use it.

Opening Pipes to Caché Utilities

You can open a pipe to a Caché utility as well as to UNIX® or Windows processes. Before you can use a pipe for utility I/O, your system manager must define the pipe device on your Caché system.

After the system manager defines the pipe device, when you run a utility (such as %RD), you answer the “Device:” prompt with the mnemonic the system manager defined. Your output goes automatically to that device.

Pipes and Command Pipes

Caché supports both standard pipes and command pipes. Standard pipes are used for relative short command strings, in which the command name and its arguments are less than 256 characters. Command pipes are used when the command string is 256 characters or more in length. In both cases, pipes can only be used on UNIX® and Windows systems.

Standard Pipe OPEN

The following is the OPEN command syntax for standard pipes:

OPEN program:(parameters):timeout

Because program is the first argument (the device argument), it must follow the OPEN command device name limitation of 256 characters.

Command Pipe OPEN

The following is the OPEN command syntax for command pipes:

OPEN cpipename:program:timeout 
OPEN cpipename:(program:parameters:::closetimeout):timeout

The cpipename argument can take the value "|CPIPE|" if there is only command pipe open concurrently. To open multiple concurrent pipes, specify "|CPIPE|xxxxxx", where xxxxxx represents a user-specified unique identifier. This cpipename argument is the argument specified for subsequent USE and CLOSE commands.

Because program is the second argument, it is not limited to 256 characters. The maximum length of program is platform dependent.

OPEN Command for Interprocess Communication

The OPEN command allows your program to communicate with processes external to Caché.

OPEN Arguments

Argument Description
cpipename Command Pipes Only — either "|CPIPE|" or "|CPIPE|xxxxxx", where xxxxxx represents a user-specified unique identifier.
program

A command pipe can execute a program with a command shell, or without a command shell (directly). Executing without a command shell is preferred in most situations. A standard pipe executes a program with a command shell.

Command Pipes Only — To execute without a command shell, specify /COMMAND=program. If program has arguments, you must specify them using the /ARGS keyword. If you specify either the /COMMAND or /ARGS keyword, the program is executed without a command shell: (/COMMAND=program), (/COMMAND=program:/ARGS=arg1) and (program:/ARGS=arg1) are all valid syntax. /ARGS can take a single argument, a comma-separated list of arguments, or an array. For example, (/COMMAND=program:/ARGS=arg1,arg2). You can specify a variable number of arguments using an array: SET array(1)=arg1, array(2)=arg2, array=2 OPEN device:(/COMMAND=cmd:/ARGS=array...)

To execute using a command shell, specify program, omitting both the /COMMAND and /ARGS keywords.

The program string contains the full pathname of a program installed on your system. It contains the command name and its arguments (if any) to be executed on the host system. For a standard pipe, limited to <256 characters. For command pipe, maximum length is platform dependent, but substantially more than 256 characters.

parameters

Read. For a standard pipe specify “Q” or “QR” to open a queue or pipe to accept input from another process. For a command pipe: because a command pipe is unambiguously a pipe, the “Q” letter code is not required; specify “R”.

Write. For a standard pipe specify “QW” to open a queue to send input to another process. For a command pipe: because a command pipe is unambiguously a pipe, the “Q” letter code is not required; specify “W”.

You can specify these and other parameters using the /keyword parameters, separated by colons. For example, OPEN "|CPIPE|":(cmd:/READ:/IOTABLE="UTF8"). The following optional keyword parameters are commonly used with pipes:

“K/name/” (or “Knum” ) to enable I/O translation, if translation has been enabled system-wide. You identify the previously defined table on which the translation is based by specifying the table's name. The “+” and “-” options for turning protocols on and off are not available with the “K” protocol.

“Y/name/” (or “Ynum”) to tell the system to use the named $X/$Y Action Table. You identify the previously defined $X/$Y Action Table on which translation is based by specifying the table's name. $X/$Y action is always enabled. If Y is not specified and a system default $X/$Y is not defined, a built in $X/$Y action table is used. The “+” and “-” options for turning protocols on and off are not available with the Y protocol.

You can specify the “S” (stream), “F” (fixed length), or “U” (undefined length) mode parameters with the above parameters. You cannot specify the “V” (variable length) mode parameter.

For a complete list of letter code and keyword parameters, refer to OPEN Mode Parameters in the “Sequential File I/O” chapter of this manual.

closetimeout Optional — UNIX® only: You can specify the number of seconds the CLOSE command will wait for the command process to exit when closing a piped command device. The default is 30 seconds. You can override this closetimeout by specifying an “I” (immediate) argument on the CLOSE command for interprocess communication.
timeout Optional — A positive integer whose value in seconds is the longest time Caché waits for an OPEN to successfully finish. If Caché is able to open interprocess communication before the timeout expires, it sets $TEST to 1. If Caché is not able to open interprocess communication before the timeout expires, it sets $TEST to 0. If you omit the timeout or specify 0, the OPEN returns control to the process immediately.

OPEN Command Pipe Examples

The following are valid command pipe OPEN statements. Each example specifies a timeout of 10 seconds:

  OPEN "|CPIPE|1":"/nethome/myprog":10                                  // using shell, no args
  OPEN "|CPIPE|1":("/nethome/myprog":/WRITE):10                         // using shell, no args, WRITE
  OPEN "|CPIPE|2":/COMMAND="/nethome/myprog":10                         // no shell, no args
  OPEN "|CPIPE|3":("":/COMMAND="/nethome/myprog"):10                    // no shell, no args
  OPEN "|CPIPE|4":(/COMMAND="/nethome/myprog":/ARGS=arg1):10            // no shell, 1 arg
  OPEN "|CPIPE|5":("/nethome/myprog":/ARGS=arg1):10                     // no shell, 1 arg
  OPEN "|CPIPE|6":("/nethome/myprog":/ARGS=arg1:/WRITE):10              // no shell, 1 arg, WRITE
  OPEN "|CPIPE|7":(/COMMAND="/nethome/myprog":/ARGS=arg1,arg2):10       // no shell, 2 args
  OPEN "|CPIPE|8":(/COMMAND="/nethome/myprog":/ARGS=args...:/WRITE):10  // no shell, args array, WRITE

On a Windows system, an argument can include a blank space or a double quote (") character. In these cases, the argument can be quoted, and a literal double quote character can be escaped by doubling it:

  OPEN "|CPIPE|9":("/nethome/myprog":/ARGS="string with blanks"):10
  OPEN "|CPIPE|10":("/nethome/myprog":/ARGS="string with literal "" character"):10

Errors

If you issue an OPEN command with the “QW” parameter for a non-IPC device, a <WRITE> error occurs when you try to write to this device.

The following UNIX® example opens an output pipe to the lp program, whose pathname in this case is /usr/bin/lp. Then it sends output from the global ^TEXT to the printer through this pipe.

print ; Send the first layer of global ^TEXT to the printer. 
      SET IO="/usr/bin/lp"
      OPEN IO:"QW"   ; Open the pipe to lp 
      USE IO WRITE "The first layer of ^TEXT",!   ; Print the title 
      ;  . . .
      ;  Print each line, using $ORDER on the global ^TEXT
      USE IO WRITE !,"The End.",#
      CLOSE IO    ; close the pipe, spooling the file to lpsched 
      QUIT

You can alter this example so that the OPEN command passes arguments to the lp program. For example, to specify that lp should send the output to the printer device named laserjet, you could replace the SET command with the following:

   SET IO="/usr/bin/lp -dlaserjet"

The following example shows how to read from an external program. Here the process opens an input pipe to the UNIX® program who, so that it can read the IDs of all users who are currently logged in to UNIX®.

getids ; read the login IDs of everybody currently on 
       SET IO="/usr/bin/who"
       SET $ZTRAP="EOT"
       KILL LOGINS 
       OPEN IO:"Q"
       ; note that "R" (the default) is understood 
       SET users=0
       FOR I=0:0 {
          USE IO
          READ USER
          SET users=users+1
          SET LOGINS(USER)=""
          }
       QUIT
EOT    SET $ZTRAP=""
       USE 0
       WRITE !,USERS," is/are currently logged on.",!
       CLOSE IO
       QUIT

On a Windows system, when a CPIPE OPEN program argument specifies /COMMAND or /ARGS, the system uses CreateProcess() to run the command. If the CreateProcess() fails, the OPEN will fail with a <NOTOPEN> error. The GetLastError() value is available via $SYSTEM.Process.OSError()Opens in a new tab.

On a UNIX® system, when a CPIPE OPEN program argument specifies /COMMAND or /ARGS, the system creates a new process which issues an exec() to run the command. If the exec() fails, the OPEN will fail with a <NOTOPEN> error. The exec() errno is available via $SYSTEM.Process.OSError()Opens in a new tab.

OPEN and USE Command Keywords

The following table describes the keywords for controlling interprocess communications pipes with both OPEN and USE commands.

OPEN and USE Command Keywords for Interprocess Communications Pipes
Keyword Default Description

/IOTABLE[=name]

or

/IOT[=name]

If name is not specified, the default I/O translation table for the device is used. Corresponds to the K\name\ parameter code, which establishes an I/O translation table for the device.

/TRANSLATE[=n]

or

/TRA[=n]

1 Associated with the K parameter code. /TRANSLATE or /TRANSLATE=n for nonzero values of n enable I/O translation for the device. /TRANSLATE=n for a zero value of n disables I/O translation for the device.

/XYTABLE[=name]

or

/XYT[=name]

If name is not specified, the default $X/$Y action table for the device is used. Corresponds to the Y\name\ parameter code, which establishes a $X/$Y action table for the device.

OPEN-only Keywords

The following table describes the keywords for controlling interprocess communications pipes with only the OPEN command.

OPEN-only Command Keywords for Interprocess Communications Pipes
Keyword Default Description

/IGNOREEOF[=n]

or

/IGN[=n]

0 Corresponds to the I parameter code, which specifies that a READ operation should be retried (ignoring any EOF condition) indefinitely or until the specified timeout expires. /IGNOREEOF or /IGNOREEOF=n for nonzero values of n enable the parameter code and /IGNOREEOF=n for a zero value of n disables the parameter code.

/PARAMS=str

or

/PAR=str

No default Corresponds to the parameter codes positional parameter. (It provides a way to specify a parameter code string in a position independent way.)

/QUEUE

or

/QUE

The device is not recognized as an interprocess communications pipe. Corresponds to the “Q” parameter code, which specifies that an interprocess communications pipe should be opened. Note that using this command requires Use permission on the %System_Callout resource.
/Read Read is the default if neither /Read nor /Write is specified. Corresponds to the “R” parameter code, which specifies that a queue or pipe should be opened to accept data from another process.

/Write

or

/WRI

Read is the default if neither /Read nor /Write is specified. Corresponds to the “W” parameter code, which specifies that a queue or pipe should be opened to send data to another process.

READ Command for Interprocess Communication

Syntax

READ:pc readargument,...

READ reads data from a pipe.

where readargument can be:

formatting-mode
string
variable:timeout
*variable:timeout
variable#n:timeout

Use the I formatting-mode parameter with pipes. The I parameter lets you issue a timed READ for a named pipe without losing any data that can occur in a partial record that follows an <ENDOFFILE> error. When you use this parameter on a READ, the READ ignores <ENDOFFILE> messages.

The value of the I formatting-mode is “off” by default. If you include this parameter in a READ command without a timeout, your process hangs until there is data to process.

CLOSE Command for Interprocess Communication

If you create a child process using OPEN with a “Q” (/QUEUE) parameter code, the child process may survive a CLOSE operation on the device. Survivability of a queued interprocess communications pipe is platform-dependent. On UNIX systems the child process always survives the CLOSE. On Windows systems the survival of the process depends upon how old the process is. A child process that has just been initiated does not survive a CLOSE operation, but once a child process is fully established it survives a CLOSE.

On UNIX systems, you can specify the how long the CLOSE command should wait when closing a piped command device. The timeout default is 30 seconds. You can modify this default by specifying the OPEN command closetimeout positional argument. You can override the default or specified timeout for a CLOSE command by specifying the optional “I” positional argument. The “I” argument specifies immediate close (close after 1 second). The CLOSE syntax is as follows:

CLOSE cpipename:"I"

Using Named Pipes to Communicate with Visual Basic

On Windows, use named pipes in Caché as you would use TCP devices, but use the device name “|NPIPE|nnn” instead of “|TCP|nnn”. The OPEN arguments are as follows:

OPEN "|NPIPE|3":(server:pipename)

where server is the Windows NT machine name, and pipename is the name of the pipe that it is to be connected to. Windows 95/98 machines cannot be named pipe servers, but can only connect to them.

To connect to a local pipename, use "." (a quoted period) as a server. To create a pipe (as a server), use "" (quotes without content) as the server name. The following are all valid server names:

   OPEN "|NPIPE|3":(".":"localpipe")
   OPEN "|NPIPE|3":("mother":"test")
   OPEN "|NPIPE|3":("":"info")

A server can open a named pipe and immediately issue a write before the client side has opened the same named pipe. The write operation will hang until the client side opens the named pipe. A user can interrupt the hang by issuing a Control-C.

Once open, a pipe acts like an ordinary device. On the server side, clients can be disconnected as in TCP with:

   USE "|NPIPE|3":"DISCONNECT"

Alternatively:

   USE "|NPIPE|3" WRITE *-2

OPEN Command Keywords

The following table describes the keywords for controlling named pipes with only the OPEN command.

OPEN Command Keywords for Named Pipes
Keyword Default Description

/HOSTNAME=str

or

/HOS=str

The default is "" (quotes without content), which opens the pipe as a server. Corresponds to the server positional parameter, which specifies the Windows NT workstation/server name. It is not necessary to specify this keyword when opening the pipe as a server. Use "." (a quoted period) to connect to a local pipename.

/IBUFSIZE=n

or

/IBU=n

2048 Specifies the size of the named pipe input buffer that holds data received from the pipe but not yet delivered to the application.

/INSTANCES=n

or

/INS=n

1 Specifies the maximum number of instances allowed for the named pipe. A value greater than 1 allows more than one server to open an instance of the named pipe, so that more than one client at a time can be served.

/OBUFSIZE=n

or

/OBU=n

2048 Specifies the size of the output buffer used by the operating system. This buffer size is advisory, since the operating system sizes the buffer according to system-imposed constraints.

/PIPENAME=str

or

/PIP=str

No default Corresponds to the pipename positional parameter which specifies the name of the pipe.

Communication Between Caché Processes

Interjob communication (IJC) devices are a set of special device numbers that let you transfer information between two or more Caché processes. The processes can be either jobbed processes or interactive processes.

IJC devices work in pairs. You can have up to 256 IJC device pairs. You use even-numbered devices, called receivers, to read data. You use odd-numbered devices, called transmitters, to write data. Attempts to read from a transmitter or write to a receiver result in a <NODEV> error.

You issue I/O commands to IJC devices, just as to any other device. After issuing OPEN and USE commands to the device, a process can issue:

  • READ commands to a receiver device

  • WRITE commands to a transmitter device

Only one process at a time can have a device open.

Pairs are based on relative order as mapped in the Caché Device Table, which you can view and edit using the configuration options of the Management Portal.

Each pair of devices is associated with an IJC memory buffer. When a process issues a WRITE command to any odd-numbered IJC device, Caché writes the data into the buffer for that device pair. When another process issues a READ command to the even-numbered device from that pair, Caché reads the data from the same buffer.

Written data is buffered in memory in first-in, first-out fashion. If a READ occurs while the device is empty, the process that issued it suspends until another process issues a corresponding WRITE. A WRITE that occurs while the buffer is full suspends until another process reads from that buffer.

After you write a message to the buffer, it remains there until it is read, even if you close the transmitter. Several users can issue OPEN, USE, WRITE, and CLOSE commands to a transmitter, one at a time in turn. Subsequent READ commands get all of the messages in the order in which they were written.

Specifying Memory Buffers for Interjob Communication Devices

The system manager can configure the IJC buffers using the Management Portal. Select System, Configuration, Advanced Memory Settings. The two parameters that can be set are:

  • ijcnum: The maximum number of IJC devices. The range is from 0 through 256. The default is 16. If you edit this setting, you must restart Caché to apply the change.

  • ijcbuff: The maximum size (in bytes) of each IJC buffer. The range is from 512 through 8192. The default size is 512 bytes. If you edit this setting, you must restart Caché to apply the change.

Each IJC device corresponds to one IJC buffer of the size specified in ijcbuff. You can write a message of length ijcbuff minus 1.

Disabling Interjob Communication Buffers

If you will not be using IJC devices, you can set the maximum number of IJC devices (ijcnum) to 0 to avoid tying up memory.

Interjob Communication Device Numbers

Interjob communication devices are automatically defined numbered by Caché. Their actual identification numbers depends on the maximum number of IJC buffers configured on the system.

The table below gives the ranges of IJC device numbers that are available on your system, depending on the number of IJC buffers that you have allocated.

For example, if you allocate 8 IJC buffers, then device numbers from 224 through 239 are defined on the system (even numbers for READ devices and odd numbers for WRITE devices).

As another example, if you allocate 94 IJC buffers, then the following range of device numbers are defined: 224 through 255, 64 through 199, 4 through 19, and 2048 through 2051. You can use any even/odd number pairs with OPEN, USE, READ, WRITE, and CLOSE commands.

IJC Device Numbers
Buffers Allocated READ Device # WRITE Device #
1 224 225
2 226 227
3 228 229
... ...  
15 252 253
16 254 255
17 64 65
18 66 67
... ... ...
83 196 197
84 198 199
85 4 5
86 6 7
87 8 9
88 10 11
89 12 13
90 14 15
91 16 17
92 18 19
93 2048 2049
94 2050 2051
95 2052 2053
... ... ...
254 2370 2371
255 2372 2373
256 2374 2375

I/O Commands for IJC Devices

You use all of the standard I/O commands with IJC devices: OPEN, USE, READ, WRITE, and CLOSE.

OPEN Command

The OPEN command reserves interjob communication devices for your use.

Syntax
OPEN device::timeout

where:

device A device number from the table above. OPEN an even-numbered device to issue READ commands. OPEN an odd-numbered device to issue WRITE commands. For two processes to communicate, they must open a set of device pairs.
timeout Optional — A positive integer whose value in seconds is the longest time Caché waits for an OPEN to finish. If you specify 0, the OPEN returns control to the process immediately.

This example shows how two processes communicate by opening separate devices for reading and writing:

Process A                          Process B 
OPEN 227 USE 227 WRITE "MSG_1"
WRITE "MSG_2"                       OPEN 226 USE 226 READ X
CLOSE 227                           CLOSE 226
OPEN 224 USE 224 READ X             WRITE X
CLOSE 224                           MSG_1
WRITE X                             .
MSG_3                               .
                                    .
                                    OPEN 225 USE 225 WRITE "MSG_3" 
                                    CLOSE 225

Process A begins by opening device 227 and writing MSG_1 to it. Caché writes this message into the buffer shared by devices 226 and 227. Process A then writes a second message to the same buffer. Now Process B opens companion device 226 and reads the first message (MSG_1) from the buffer.

Now Process A wants to read a message, so it must open a different device, 224. Because the buffer for this device and its companion, 225, is currently empty, Process A waits until Process B opens device 225 and writes MSG_3 to it. After Caché places this message in the buffer shared by devices 224 and 225, the READ command to device 224 succeeds.

FeedbackOpens in a new tab