PRB: Spawned Process Runs Out of Environment Space

ID: Q51742


The information in this article applies to:


SYMPTOMS

With a Microsoft C created MS-DOS application, using spawn() to create a child process which then attempts to increase the environment space of the child (by adding a new environment variable or expanding an existing environment variable) results in the following error:

Out of Environment Space
This problem does NOT happen if the parent and child are built as native OS/2 or Win32-based applications.


RESOLUTION

To work around this problem, set up a dummy environment variable that is large enough to hold the new environment variable you plan to use in the spawned process. When the child process is called, you can then set the dummy variable to null (with a "set dummy="), then you can set your processes environment variable. Please note that this will change the dummy environment variable for the child only, not the parent process.


MORE INFORMATION

The following program(s) illustrates this behavior:

Sample Code


/*
parent.c

Compile options needed: none

This will call the child process (child) with the spawnlp
function.
*/ 

#include <stdio.h>
#include <process.h>

void main (void);

void main (void)
{
    printf ("In the parent process\n");

    spawnlp (P_WAIT, "child.exe", "child", NULL);

    printf ("\nAnd back to the parent process.\n");
}



/*
child.c

Compile options needed: none

Called by parent.c, uses the system function to call a batch
file (BATCH.BAT) to attempt to set a new environment variable.
*/ 

#include <stdio.h>
#include <process.h>
#include <conio.h>
#include <errno.h>

void main (void);

void main (void)
{
    printf ("At child process...\n");
    system ("batch.bat");
    getch ();
}


/*-----------------------------------------------------------------*/ 
BATCH.BAT  The batch file, which is called by the child process
            (CHILD.EXE). It just shows the environment variables,
            attempts to set another environment variable, then shows
            the environment variables one more time.
/*-----------------------------------------------------------------*/ 

set
set blah=thisisatestonlyatestsoitdoesnotreallymatter
set

/*-----------------------------------------------------------------*/   
Parent will spawn child, which in turn spawns (through system) BATCH.BAT. The idea is to show that when BATCH.BAT is called, an "Out of Environment Space" error will be given. Yet, if BATCH.BAT is run from MS-DOS, no such error is issued.

Under MS-DOS, the child receives only enough space to hold the current environment variables.

Additional query words: 5.10 6.00 6.00a 6.00ax 7.00 1.00 1.50


Keywords          : kb16bitonly 
Version           : 
Platform          : 
Issue type        : 

Last Reviewed: July 30, 1999