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.
- Path Selection: The shell inspects the environment
for a
TEMPorTMPvariable. 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. - File Naming: MS-DOS generates unique temporary file
names, typically following patterns like
_PIPE1.$$$and_PIPE2.$$$, using MS-DOS function calls (specificallyINT 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
SORTand serves as input forFIND.
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:
- Standard Handles: MS-DOS predefines standard input
(
STDIN) as File Handle0and standard output (STDOUT) as File Handle1. - Handle Duplication:
COMMAND.COMopens the temporary file and usesINT 21h, Function 46h(Force Duplicate Handle) to redirect Handle0or Handle1to point to the temporary file on disk. - 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":
Phase 1 (Source Command):
COMMAND.COMredirectsSTDOUT(Handle 1) toTEMP\_PIPE1.$$$.TYPE DATA.TXTexecutes and outputs all text intoTEMP\_PIPE1.$$$.- Execution terminates, returning control to
COMMAND.COM.
Phase 2 (
SORT.EXE):COMMAND.COMredirectsSTDIN(Handle 0) to read fromTEMP\_PIPE1.$$$.- It redirects
STDOUT(Handle 1) to write toTEMP\_PIPE2.$$$. SORT.EXEis 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.
Phase 3 (
FIND.EXE):COMMAND.COMredirectsSTDIN(Handle 0) to read fromTEMP\_PIPE2.$$$.STDOUT(Handle 1) is restored to its original destination (usually the video display,CON).FIND.EXEexecutes, 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.COMrestores the defaultSTDINandSTDOUThandles.- The shell deletes the temporary files (
_PIPE1.$$$,_PIPE2.$$$). - If a user aborts the execution via
Ctrl+Cor a critical disk error occurs mid-stream,COMMAND.COMattempts to clean up the intermediate files, though unexpected crashes or hard resets can leave orphaned$$$files in the temporary directory.