sbrk, malloc and gcc (2.6.0) in SunOS 4.1.3

sbrk, malloc and gcc (2.6.0) in SunOS 4.1.3

Post by Ta-Wei » Thu, 29 Sep 1994 03:53:39



  I am working on a program for my OS class and it invloves calling
sbrk(0) after malloc(). According to the "The design and implementation
of 4.3BSD UNIX Operating System," malloc calls sbrk for to allocate
memory. However in Sun's man page of sbrk it says

WARNINGS
     Programs combining the brk() and  sbrk()  system  calls  and
     malloc()  will not work.  Many library routines use malloc()
     internally, so use brk() and sbrk() only when you know  that
     malloc() definitely will not be used by any library routine.

But in the man page of malloc:

     ... When there is no suitable  space  already  free, the
     allocation routines call sbrk() (see brk(2)) to get more memory
     from the system...

As some experiments:

sbrk(0);
malloc(2000);
sbrk(0);

The return values of the function are different while compiling
with gcc and Sun cc (on a Sparc 10 with 256 MB of RAM).

gcc:
  sbrk 114504
  sbrk 114504
Sun cc:
  sbrk 16600
  sbrk 28896

So, first of my questions is that which of the man page is correct? Can I use
sbrk with malloc? Second, why cc and gcc give different answers? Third, where
can I find more information about Sun's implementation of these system calls
and library functions?

Thanks in advance.

--
Ta-Wei "David" Li
UNIX Consultant, University of Southern California
Member, League for Programming Freedom
"Innovate, don't litigate."

 
 
 

1. sbrk/malloc question

Can anyone out there shed light on the warning in the man page for sbrk,

     Programs combining the brk() and  sbrk()  system  calls  and
     malloc()  will not work.  Many library routines use malloc()
     internally, so use brk() and sbrk() only when you know  that
     malloc() definitely will not be used by any library routine.

This is from Sun Release 4.1   Last change: 21 January 1990

Does malloc (and the like) require exclusive access to sbrk?  What

2. Help needed building a new kernel for Red Hat 6.0

3. "malloc" which really returns memory to OS (sbrk)?

4. Loadable Kernel modules under SunOS 4.X

5. When will malloc use sbrk?

6. Shark 250

7. Mixing malloc() and sbrk() safe on Linux?

8. Xconfigurator Help

9. Question: mmap() vs. sbrk() in malloc?

10. malloc(), sbrk() conflicts?

11. Errors compiling Wingz 1.4 Add-ins (Linux 1.3.99, GCC 2.6)

12. does realloc(NULL,size) work like malloc(size) on SunOS 4.1.3 or AIX 2

13. Porting SUNOS to Solaris (signals, sockets and malloc question)