5.4 Child Programs

A child program is any MS-DOS program that has been started by another program. While a child program is running, the system temporarily suspends the parent program, returning control to it when the child program terminates. A good example of a parent program is COMMAND.COM, which loads and runs a child program whose name is typed at the command prompt. While the child program is running, the system suspends COMMAND.COM, returning control to it when the child program terminates.

A program loads and runs a child program by using Load and Execute Program (Interrupt 21h Function 4B00h). Once started, the child program can use any MS-DOS system function to carry out its work, but it must terminate by using End Program (Interrupt 21h Function 4Ch). This function frees the child program's memory, closes any open files, and returns control to the parent program. The parent program can then call Get Child-Program Return Value (Interrupt 21h Function 4Dh) to retrieve the child program's return value.

Most parent programs provide their child programs with such information as the environment block, the command tail, and the default file control blocks (FCBs). In addition, parent programs handle the following:

Parameter block

Inherited files

Standard-device redirection

Return values

Batch files

When Load and Execute Program returns, the carry flag indicates whether the child program was run. If the carry flag is set, the function failed and the AX register contains an error value indicating the reason for the failure. The parent program can retrieve additional information about the failure by using Get Extended Error (Interrupt 21h Function 59h).

By default, MS-DOS sets a .COM program's stack at the high end of the 64K segment that contains the program. Before reducing its memory allocation, a .COM program must move its stack within the new range of memory to be allocated. .COM programs>.COM programs>

Note:

MS-DOS version 2.x does not preserve the parent program's registers (except CS:IP). Before calling Load and Execute Program, the parent program must push onto the stack all registers it needs to preserve.