Progress
External Program
Interfaces
UNIX Named Pipe Examples
The following examples show different uses of named pipes. To provide a simple example of how named pipes operate, the first example shows how to use the shell to create a named pipe, send a message to it, and read the message back. The second example shows how to use named pipes with Progress.
Example 1 — Creating and Using a Named Pipe From the Shell
In this example, the
cat
command sets up a message handler routine and theecho
command acts as the requestor:
To try this example, run the shell script,
e-pipex1
.This script performs the following actions:
- The
mknod
command creates a named pipe calledtrypipe
.- The
cat
command openstrypipe
for reading. It blocks becausetrypipe
has not yet been opened for writing. Notice that an ampersand (&
) is present at the end of thecat
command; this runs thecat
command as a background process.- The
echo
command openstrypipe
for writing and writes a message. Thecat
command, blocked until now, resumes execution, and the message appears on the display.- The
rm
command deletestrypipe
.Example 2 — Using a Named Pipe With Progress
Before working with the following procedures, create a copy of the
demo
database with the PRODB utility:
This example shows a simple user program that sends one line requests to a Progress message handler routine running in the background, and displays the results. The example consists of four files:
A display of these files follows:
To prepare and run the example, follow these steps:
The
e-pipex2
script performs the following actions:
- It uses
mknod
to create two named pipes:inpipe
andoutpipe
. Named pipeinpipe
carries requests from the requestor to the message handler routine. Named pipeoutpipe
carries results back in the opposite direction, from the message handler to the requestor.Figure 4–3 illustrates this process.
Figure 4–3: Named Pipes and Progress
![]()
- It starts a background Progress session that runs the message handler procedure,
e-pipex2.p
, with the demo database. The following line ine-pipex2.p
opens the named pipeinpipe
for input:
Notice that Progress accesses named pipes in exactly the same way as UNIX text files. At this point,
e-pipex2.p
blocks until a requestor process opensinpipe
for output.- After starting the Progress message handler, the script starts the requestor,
e-asksql
, which opensinpipe
for output using the following statements:
- As
e-asksql
opensinpipe
,e-pipex2.p
unblocks and blocks again as it attempts to read a message frominpipe
. The message handler procedure,e-pipex2.p
, expects single-line requests from requestors, and can handle more than one requestor. This is the syntax for message handler requests:
Each request contains the name of the named (output-named-pipe) pipe from which the requestor expects to receive results and an SQL statement (SQL-statement) surrounded by double quotes (" "). The message handler procedure reads these messages with the following statement:
Each requestor must specify a unique value for output-pipe-name, or results might be intermixed. (Using the requestor’s PID number as part of the name ensures uniqueness. However, for that to work, the requestor probably has to create its own named pipe using the
mknod()
system call.) Note that for this example, the requestor,e-asksql
, uses the existing named pipeoutpipe
created by the script.- As the message handler waits for input, the requestor displays the following prompt:
You can enter a one-line SQL query like the following SELECT from the demo database:
- The requestor constructs a message from the name of the output pipe (
outpipe
, in the example) and the contents of your query, and writes the message toinpipe
, as in the following statements frome-asksql.c
:
- As the message handler receives (and removes) the message from
inpipe
, it unblocks and opens the output pipe named in the message with the following statement:
- The message handler blocks again, waiting for the requestor to open the same pipe for input (to receive the query result), as in the following statements from
e-asksql.c
:
- The Progress message handler then continues to compile and run the SQL query using the following statement:
As the query generates output, Progress writes it one line at a time to the named pipe specified by
NOTE: If there is no output, you might have entered your SQL statement incorrectly. This causesoutpipe
. The requestor reads each line as it is written to the output pipe, as in the following statements frome-asksql
. In the example, the requestor also writes each line to its standard output:e-pipex2.p
to terminate. To trap this type of error, write the SQL statement to a file instead of to a named pipe, then compile the file. If the compilation is successful, run it.
Note that although the query procedure,
e-do-sql.p
, contains only the single procedure parameter,{1}
, you can extend it with formatting statements to avoid having to include these in each query, as in the following examples:
- The example requestor,
e-asksql
, continues to prompt for queries, repeating Actions 5 through 9, until you press RETURN with no additional input.- After the requestor terminates, the
e-pipex2
script terminates the Progress background process with the following commands:
The first command sends the Progress QUIT statement to the message handler (instead of an SQL statement). The second command takes the place of Action 8, originally handled by the requestor. The requestor does not send the QUIT to terminate the Progress background process so that multiple copies of the requestor — each with its own output pipe — can run without affecting the message handler. It is necessary because a process blocks until a named pipe it opens for writing is opened for reading (see the "Operational Characteristics Of Named Pipes" section). In this case, the message handler opens named pipe
outpipe
for writing, and cannot execute QUIT until thecat
command opensoutpipe
for reading.- The,
e-pipex2
script uses therm
command to remove the named pipes that it created.
Copyright © 2004 Progress Software Corporation www.progress.com Voice: (781) 280-4000 Fax: (781) 280-4095 |