How MS-DOS 6.22 Executes Pipes with FIND and SORT

In MS-DOS 6.22, command pipelining does not use real-time concurrent memory buffers like modern multitasking operating systems. Instead, the command interpreter (COMMAND.COM) handles pipe chains involving utilities like FIND and SORT sequentially by redirecting intermediate data into hidden temporary files on disk. This article explains how MS-DOS parses nested pipe chains, reassigns file handles, sequentially invokes external filter commands, and manages cleanup.

Single-Tasking Architecture and Pseudo-Pipes

Because MS-DOS 6.22 is a single-tasking operating system, it cannot run two programs simultaneously to pass data directly from one memory buffer to another. When a command line contains pipe characters (|), such as:

TYPE DATA.TXT | SORT | FIND "COMPLETED"

COMMAND.COM simulates a pipeline by serializing the execution of each command. The left-hand command must finish its execution entirely and write its full output to disk before the right-hand command can begin reading it.

Temporary File Creation

When COMMAND.COM detects a pipe operator (|), it prepares temporary files to store the intermediate stream.

  1. Path Selection: The shell inspects the environment for a TEMP or TMP variable. If defined, the temporary files are created in that directory; if neither exists, they are written to the current working directory or the root directory.
  2. File Naming: MS-DOS generates unique temporary file names, typically following patterns like _PIPE1.$$$ and _PIPE2.$$$, using MS-DOS function calls (specifically INT 21h, Function 5Ah - Create Temporary File).

In a chain containing three elements (such as COMMAND | SORT | FIND), COMMAND.COM creates two temporary intermediate files:

  • File A: Receives output from the initial command and serves as input for SORT.
  • File B: Receives output from SORT and serves as input for FIND.

File Handle Redirection via Interrupt 21h

To pass data into and out of standard filter utilities like SORT.EXE and FIND.EXE, COMMAND.COM manipulates DOS file handles prior to spawning the programs:

  1. Standard Handles: MS-DOS predefines standard input (STDIN) as File Handle 0 and standard output (STDOUT) as File Handle 1.
  2. Handle Duplication: COMMAND.COM opens the temporary file and uses INT 21h, Function 46h (Force Duplicate Handle) to redirect Handle 0 or Handle 1 to point to the temporary file on disk.
  3. Execution via EXEC: The shell executes the utility using INT 21h, Function 4Bh (EXEC). Because standard input or output has been reassigned at the DOS handle table level, the utility reads from or writes to the temporary disk file without needing any internal awareness of the pipe.

Execution Sequence in a Nested Chain

For a pipeline like TYPE DATA.TXT | SORT | FIND "KEYWORD":

  1. Phase 1 (Source Command):

    • COMMAND.COM redirects STDOUT (Handle 1) to TEMP\_PIPE1.$$$.
    • TYPE DATA.TXT executes and outputs all text into TEMP\_PIPE1.$$$.
    • Execution terminates, returning control to COMMAND.COM.
  2. Phase 2 (SORT.EXE):

    • COMMAND.COM redirects STDIN (Handle 0) to read from TEMP\_PIPE1.$$$.
    • It redirects STDOUT (Handle 1) to write to TEMP\_PIPE2.$$$.
    • SORT.EXE is loaded into memory. It reads all data from _PIPE1.$$$, performs an in-memory or temporary disk-based sort depending on file size, writes the sorted output to _PIPE2.$$$, and exits.
  3. Phase 3 (FIND.EXE):

    • COMMAND.COM redirects STDIN (Handle 0) to read from TEMP\_PIPE2.$$$.
    • STDOUT (Handle 1) is restored to its original destination (usually the video display, CON).
    • FIND.EXE executes, scans the lines from _PIPE2.$$$ for "KEYWORD", prints matching lines directly to the screen, and exits.

Pipeline Cleanup and Error States

Once the final command in the pipeline finishes execution:

  • COMMAND.COM restores the default STDIN and STDOUT handles.
  • The shell deletes the temporary files (_PIPE1.$$$, _PIPE2.$$$).
  • If a user aborts the execution via Ctrl+C or a critical disk error occurs mid-stream, COMMAND.COM attempts to clean up the intermediate files, though unexpected crashes or hard resets can leave orphaned $$$ files in the temporary directory.