[net.unix-wizards] Problem with sbrk

NEP.FOUTS@Ames-Vmsb.ARPA (03/25/84)

The following small piece of code attempts to size memory by using sbrk to
set the highest acceptable address.  To do this quickly, it uses a "binary
search" approach.  It selects a large (8mb) memory increment, and attempts
to increase memory by this increment size until sbrk returns an error code.
It then cuts the increment size in half and tries again.  This continues until
it has failed with a 1kb increment size.  As it is going along, it keeps
track of the amount added and at the end reports the final result.

-----  code start

#include <stdio.h>
#include <sys/file.h>
#include <errno.h>

#define kb 1024				/* 1 kb */
#define end_inc 1024			/* smallest buffer to allocate  1kb */
#define start_inc 8192 * 1024		/* largest buffer to allocate  8kb */
#define maximum_size 8192 * 1024	/* maximum memory to allocate  8mb */

int ptrs;

main( )
{
	int i, j;
	i = 0;
	j = start_inc;
	printf("Expanded from %dkb to ",sbrk(0)/kb);
	for (j = start_inc; j > end_inc; j /= 2)
		while ((i < maximum_size)
			&& (sbrk((int) j) > 0))
			i += j;
	printf("%dkb by allocating %dkb of memory.\n",sbrk(0)/kb,i/kb);
}

----- code end

The problem is that according to the sbrk(3) manual page, I should be able
to allocate 6112kb of virtual memory (as indicated by executing the limit
command from csh.  One one of my 780s this works fine.  On another one, I
get a variable result, depending on the system load, from under .5mb to
around 4mb.

The machine on which the code works has two 8mb (16mb total) swap partitions
and 4mb of real memory.  The machine on which the code fails has four 8mb swap
partitions (32mb total) and 8mb of real memory.

I have observed the system using DSD while the problem program (and several
variations) is running, and do not seem to be even close to filling the swap
space when the code executes, although I usualy am near to filling physical
memory.

I have tried tracking down the sbrk code, but I can't find where in the kernal
the "chmk $SYS_brk" instruction in /usr/src/lib/libc/sys/Osbrk.c leads to.

Help!
------

ignatz@ihuxx.UUCP (Dave Ihnat, Chicago, IL) (03/30/84)

Re:  trying to find the maximum break value

Pardon my intolerable lack of knowledge on BSD calls...I don't have access
to any BSD *manuals*, let alone a BSD system.  USG (now WE) Unix
(trademarks, etc.) has the ulimit(2) syscall, specifically for returning
and (under certain restrictions) setting the user's process file size
limit...and returning the maximum possible break value.  If this isn't
in BSD Unix, it certainly should be cross-ported!

				Dave Ihnat
				ihuxx!ignatz