*BSD News Article 36472


Return to BSD News archive

Xref: sserve comp.os.misc:3341 comp.unix.bsd:15010 comp.unix.internals:7774 comp.unix.programmer:20525 comp.unix.questions:55951
Path: sserve!newshost.anu.edu.au!harbinger.cc.monash.edu.au!msuinfo!agate!howland.reston.ans.net!news.intercon.com!panix!ddsw1!news.cic.net!locust.cic.net!pauls
From: pauls@locust.cic.net (Paul Southworth)
Newsgroups: comp.os.misc,comp.unix.bsd,comp.unix.internals,comp.unix.programmer,comp.unix.questions
Subject: Re: Question: What exactly does sbrk(0) do?
Followup-To: comp.unix.bsd
Date: 2 Oct 1994 23:04:08 GMT
Organization: CICNet, Inc.
Lines: 50
Message-ID: <36ne98$l29@spruce.cic.net>
References: <36n65p$blf@sal-sun2.usc.edu>
NNTP-Posting-Host: locust.cic.net
Keywords: Question: What exactly does sbrk(0) do?

In article <36n65p$blf@sal-sun2.usc.edu>,
Frank Fabbrocino <fabbroci@sal-sun2.usc.edu> wrote:
>From what I understand, calling sbrk(0) returns the size of the data segment
>in the executable.  However, I can't find this call in our system documentation
>or my UNIX books (specifically, calling sbrk with 0).  Since I can't look
>at the code for sbrk() since our system is proprietary :( and I can't find
>the code on BSD ftp sites

Based on the description below, it would seem that you could code around
your lack of sbrk(2) fairly easily.  Read on for discussion of what
"sbrk(0)" will do; the man page is pretty good.

In NetBSD the code appears to be in the architecture-specific sections
of "sys" in the libc sources.  

ftp.iastate.edu:/pub/netbsd/NetBSD-current/src/src/lib/libc/arch/*/sys/brk.S
                /pub/netbsd/NetBSD-current/src/src/lib/libc/arch/*/sys/sbrk.S

For the man page to brk(2) and sbrk(2) you can look in the non-architecture
specific section:

ftp.iastate.edu:/pub/netbsd/NetBSD-current/src/src/lib/libc/sys/brk.2

The man page describes the function as follows:

     The brk and sbrk functions are historical curiosities left over from ear-
     lier days before the advent of virtual memory management.   The brk()
     function sets the break or lowest address of a process's data segment
     (uninitialized data) to addr (immediately above bss).  Data addressing is
     restricted between addr and the lowest stack pointer to the stack seg-
     ment.  Memory is allocated by brk in page size pieces; if addr is not
     evenly divisible by the system page size, it is increased to the next
     page boundary.

     The current value of the program break is reliably returned by
     ``sbrk(0)'' (see also end(3)).  The getrlimit(2) system call may be used
     to determine the maximum permissible size of the data segment; it will
     not be possible to set the break beyond the rlim_max value returned from
     a call to getrlimit,  e.g.  ``qetext + rlp->rlim_max.'' (see end(3) for
     the definition of etext).

To format the man page you will need the "mandoc" macros.  These are 
not included in most systems' [tn]roff implementations, but are standard
in GNU groff.  prep.ai.mit.edu:/pub/gnu/groff-1.09.tar.gz (requires C++)


--
Paul Southworth
CICNet Systems Support
pauls@cic.net