EXTERNAL DATA QUEUE
Please, give us tools that encourage us to write better programs, by enhancing our pleasure when we do so.
DONALD E. KNUTH in CACM No. 12 1974, p. 672
I introduce to you in detail external data queue (I don't suppose your knowledge about this feature of the Rexx language) - as a device for interprogram communication, as an input or output device, as useful data structure: stack or queue or deque (double-ended queue) ...
The external data queue is an AS/400 object that consists of a series of entries similar to database records. When a job is started (in interactive mode after login - SIGN ON), the queueing services are immediately made available to the job. These services remain available until the job ends (in interactive mode by the SIGNOFF command). Similarly, entries placed on the queue will remain on the queue and be available until explicitly removed or until the job is ended. After starting a job, any program that is part of the job can continue to use any of the queuing services until the job ends. Thus, one program can place an entry on the queue and end. Another following program can then receive what was placed in the queue at a later time. This can serve as a device for inter-program communication.
A helpful analogy is to think of the external data queue of a hose of a vacuum cleaner (see on the next picture); you can placed ping-pong balls as entries at the front of the hose or at the end of the hose; but you can retrieved a ball only from the front of the hose.
The queue instruction places an entry at the end of the queue.
The push instruction places an entry at the front of the external data queue.
The parse pull instruction receives and removes an entry from the front of the external data queue in a single step. If the external queue is empty then an entry will read from the standard input stream STDIN.
Calling of the built-in function QUEUED() returns the number of entries contained in the external data queue.
In addition, the Add REXX Buffer (ADDREXBUF) and the Remove REXX Buffer (RMVREXBUF) commands are available to REXX programs for working with queue buffers. These commands extend the flexibility of the REXX queue instructions. The command 'RMVREXBUF 0' clears all existing entries from the external data queue.
I will demonstrate use of the External data queue on an example from the area of the personal programming. When you need a simulation of a lottery then you use obviously the RANDOM function. I want to show you another way. Jon Bentley wrote:
How can a computer deal a poker hand? If we assign each card in the deck its own integer between 1 and 52, then we can make a hand from a "random sample" of 5 integers in the range 1..52, for instance,
(It is important that no number appear twice; holding more than one ace of spades can jeopardize a card-player's health.) --
We will write a program to shuffle a deck of cards, then deal out the pack. We begin with a deck of cards, which we will assume are labelled in increasing order with the integers from 1 to 52. The following SHUFFLE program places the deck - string 1 2 ... 52 - at the front of the data queue (when the data queue is empty) and places the current time (in the form seconds since midnight) at the front of the data queue. Who does it shuffle? - Time ...
When you start the following DEAL program randomly, it removes the time of starting the shuffle and then it removes the deck from the data queue; executes the J value - the randomly chosen card; deals this card, i.e. displays the number of the chosen card and removes this card from the deck:
By the help of alternating executions of the SHUFFLE and DEAL programs (SHUFFLE - DEAL - SHUFFLE - DEAL - ...) you can shuffle and deal cards. Between the ending the SHUFFLE program and starting the DEAL program you can do anything except the SIGNOFF command or actions changing a content of the external data queue.
Out of using the external data queue to the interprocess comunication you can use it as data structures. For instance: You can eliminate the recursion call by using of stack (exactly by the push, parse pull instructions and the QUEUED() function); you can easy program the topological sort by using of queue (exactly by queue, parse pull instructions and the QUEUED() function).
When you use the external data queue you must always be aware of what is in the queue. Write a program, which writes on the screen the content of the external data queue and according to the answer on the question Erase the external data queue? (Y/N) the queue either erases or keeps without change. A solution follows:
since 1st January 2000, last changed 13th April 2018
Copyright © 2000-2018 Vladimir Zabrodsky.