[comp.unix.sysv386] SCO Unix 3.2 brk

yml@grebyn.com (Yermo M. Lamers) (01/03/91)

According to the Programmers Reference Manual, if sbrk() is given a
negative number it should DECREASE the amount of memory allocated for 
the calling process. 

Should this decrease in allocated memory show up in the SZ column of the
ps command?

Under SCO Unix 3.2 when I use sbrk() with a positive argument, the size
of the process as reported by ps -l does get larger, but when I use
sbrk() with a negative argument the process size as reported by ps -l
does not get smaller. It seems to remain constant. (This same problem
also occurs with malloc() and free(). )

I printed out the return value of sbrk() after each call. The value it
returned became smaller after each call in proportion to the magnitude
of the argument to sbrk(). (-32000 in my case).

I used the following code fragment :

int count;
long address;

sleep( 30 );          /* run ps on another screen, SZ around 104 */

for ( count = 0; count < 100; count++ )
   if ( sbrk( 32000 ) == -1 )
      perror( "sbrk error" );

sleep(30);            /* run ps on another screen, SZ around 500 */

for ( count = 0; count < 100; count++ )
    {
   if (( address = sbrk( -32000 )) == -1 )
      perror( "sbrk error" );
   printf( "sbrk return value on DECREASE %ld\n", address );
   }

sleep( 30 );           /* run ps on another screen, SZ same as before,
                          ( that is 500 )
                          is this right????? even though the value of
                          address gets smaller on each call to sbrk? */

Is there another way to find out the size of a process instead of using
ps? (could there be a bug in ps?)

Do I misunderstand something about how sbrk()/brk() malloc() and free()
are supposed to work?

Please send replies to :

yml@grebyn.com

Any suggestions, comments, hints, etc. will be GREATLY appreciated.

Thankyou in advance,

Yermo Lamers

Radix II Inc.
Oxon Hill Md.

yml@grebyn.com

chapman@sco.COM (Brian Chapman) (01/04/91)

yml@grebyn.com (Yermo M. Lamers) writes:

>Under SCO Unix 3.2 when I use sbrk() with a positive argument, the size
>of the process as reported by ps -l does get larger, but when I use
>sbrk() with a negative argument the process size as reported by ps -l
>does not get smaller. It seems to remain constant. (This same problem
>also occurs with malloc() and free(). )

>Is there another way to find out the size of a process instead of using
>ps? (could there be a bug in ps?)

Yes there is a bug in ps(C).  More percisly, there is a bug in the
kernel data strucures that ps reads.   The size fields in the upage
were not being updated properly.  These fields have *no*function*
except to be read by ps, so nothing else is going wrong.

The size of data was never decreased or cleared, even in exec()!
try:
$ sh
$ sh
$ sh
$ sh
$ sh
$ ps -l

Notice that the processes are listed in increasing data sizes.

I would like to repeat:  This problem is understood, and fixed
for the next release.  There are no ill effects except for the
incorrect size reported in ps(C).
-- 
Brian Chapman		uunet!sco!chapman
Pay no attention to the man behind the curtain!

chapman@sco.COM (Brian Chapman) (01/05/91)

chapman@sco.COM (Brian Chapman) writes:

>The size of data was never decreased or cleared, even in exec()!

I recieved some mail, so I would like to be very clear here.

The actual size of the process *is* reduced when you sbrk() down,
that is to say the memory above the new sbrk() *is* given back
to the operating system.  It is simply not reported correctly to ps(C).

-- 
Brian Chapman		uunet!sco!chapman
Pay no attention to the man behind the curtain!