Technical article 104955

Programs using large local variables won't start or dump core.

OpenServer 5.0.0

Written · Last revised

Problem

The program I am trying to run will not start, and this error message occurs:

Cannot fork: Resource temporarily unavailable

Or, the program is able to start, but fails with a SIGSEGV (segmentation violation) and dumps core.

The program appears to be failing in a function that includes
a large local variable declaration.  For example:
myfunc()
{
    char array[1000000];
    anotherfunc();
}

If the program is able to start, and myfunc() is called, it will dump core.

Cause

The kernel is unable to allocate swap for this process.

The kernel always allocates swap for processes when they start
and when they grow in size.  Note that this is not the same as
swap used (as reported by "swap -l").

The kernel monitors this with a variable called "availsmem". (When the kernel starts, this is initialized to the total of user memory plus swap space.) You can monitor "availsmem" by using crash(ADM) when logged in as root:

# crash
dumpfile = /dev/mem, namelist = /unix, outfile = stdout
> od availsmem
f01cb12c:  00004d00
> q
#

The value returned by "od availsmem" is expressed in hex, and is in 4K pages. In the above example, availsmem is 0x4d00 = 19712, which means there is 80,740,352 bytes of swap space still to be allocated on the system on which this was run.

The core dump is caused by the program calling the above function when availsmem is low (less than 1000000 bytes). The following details exactly what happens:

1. The program calls myfunc(). As the array is a local variable, it is allocated on the stack; this causes the stack pointer to be decremented (by 1000000 bytes).

2. It then calls anotherfunc(); this causes the program to reference a stack memory page which is currently invalid. This causes a page fault, at which point the kernel will start to run its page fault handling code.

3. The kernel now attempts to create a valid page of memory,
   and during this grows the processes' stack.  However, it
   finds that the new stack size would cause the process to
   use more than the unallocated swap available; that is,
   "availsmem" would be less than 0.  Thus, it has no choice
   but to terminate the process with a SIGSEGV, which causes
   the core dump.

Solution

This is a feature of how the kernel works.

However, to avoid this problem:

1. Increase the swap space; see the swap(ADM) manual page for details on how to do this.

2. Modify your program so that it either:

a. declares the variable statically, so that it is not on the stack. The above example function could be changed to:

myfunc()
{
    static char array[1000000];
    anotherfunc();
}

b. or uses malloc(S) to allocate the space. The above example function could be changed to:

myfunc()
{
    char *array;
    if ((array = (char *) malloc(10000000)) == NULL)
    {
        perror("malloc failed");
        exit(1);
    }
    anotherfunc();
}

See also

swap(ADM) fork(S) signal(S)

From the Xinuos technical article database, record 104955. Reproduced with permission. The original is at www.sco.com/ta/104955, which is reachable but blocked from search engines. Xinuos marks this article against a retired product line. That refers to the product’s lifecycle, not to whether the advice still works — plenty of it still applies to running systems.

Next step

Need this fixed rather than explained?

Tell us the release, what the system runs and what it is doing. We will reply by email with what we would do next.

  • You deal with the people who do the work
  • No obligation, and no upgrade required before we will help
  • We tell you plainly if something is not worth doing

Send an enquiry

We reply by email. If a server is down, say so in the first line. You can also write to help@scoexpert.com.