Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Regarding brk and sbrk

Reply
Thread Tools

Regarding brk and sbrk

 
 
venkat
Guest
Posts: n/a
 
      10-17-2007
Hi folks,

Can some help in understanding brk and sbrk. how does these are
implemeted(means how the memeory will be allocated). How malloc will
call these brk and sbrk. Please also give any example C program, where
brk and sbrk are used to allocate memory.

Appreciate your help in this regard,


Thanks,
Vikas.

 
Reply With Quote
 
 
 
 
vipvipvipvip.ru@gmail.com
Guest
Posts: n/a
 
      10-17-2007
On Oct 17, 1:24 pm, venkat <(E-Mail Removed)> wrote:
> Can some help in understanding brk and sbrk. how does these are
> implemeted(means how the memeory will be allocated). How malloc will
> call these brk and sbrk. Please also give any example C program, where
> brk and sbrk are used to allocate memory.


brk() and sbrk() are not defined in the C Standard and are
deliberately
excluded from the POSIX.1 standard (see paragraphs B.1.1.1.3 and B.
8.3.3).

 
Reply With Quote
 
 
 
 
cr88192
Guest
Posts: n/a
 
      10-17-2007

"venkat" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Hi folks,
>
> Can some help in understanding brk and sbrk. how does these are
> implemeted(means how the memeory will be allocated). How malloc will
> call these brk and sbrk. Please also give any example C program, where
> brk and sbrk are used to allocate memory.
>


as noted, these are not standard.
following this, they are not even a good idea...

now, for general info:
malloc implementations tend to grab chunks of memory from the OS, and brk
and sbrk were one such method (they worked by sliding a pointer, and
generally mapping in pages as needed and so on).

another, generally better, method, is the use of mmap (on linux and
friends).

on windows, a general way to grab raw memory is through VirtualAlloc.

however, all this is a generally non-portable issue, and so, the exact
answers will depend highly on the target OS...

or such...


> Appreciate your help in this regard,
>
>
> Thanks,
> Vikas.
>



 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      10-17-2007
venkat wrote:
>
> Can some help in understanding brk and sbrk. how does these are
> implemeted(means how the memeory will be allocated). How malloc
> will call these brk and sbrk. Please also give any example C
> program, where brk and sbrk are used to allocate memory.


This is off-topic. These calls normally appear in some Unix-like
OS, and are used to expand (or possibly contract) the memory
available to a process.

You can see one example in nmalloc, a malloc/free/realloc system
for DJGPP using sbrk, available at:

<http://cbfalconer.home.att.net/download/>

Since such calls are system dependent, try a news group that deals
with your system if more data is needed.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
user923005
Guest
Posts: n/a
 
      10-17-2007
On Oct 17, 3:24 am, venkat <(E-Mail Removed)> wrote:
> Hi folks,
>
> Can some help in understanding brk and sbrk. how does these are
> implemeted(means how the memeory will be allocated). How malloc will
> call these brk and sbrk. Please also give any example C program, where
> brk and sbrk are used to allocate memory.


Try over on news:comp.unix.programmer, where they will promptly tell
you not to use those functions.

 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      10-18-2007
venkat wrote:
>
> Hi folks,
>
> Can some help in understanding brk and sbrk. how does these are
> implemeted(means how the memeory will be allocated). How malloc will
> call these brk and sbrk. Please also give any example C program, where
> brk and sbrk are used to allocate memory.


In chapter 8 of The C Programming Language, there is
an example of a general purpose storage allocator function.

The general purpose storage allocator function,
calls a function named morecore.

The definition of morecore, shows a call to sbrk.

--
pete
 
Reply With Quote
 
karthikbalaguru
Guest
Posts: n/a
 
      10-19-2007
On Oct 17, 3:24 pm, venkat <(E-Mail Removed)> wrote:
> Hi folks,
>
> Can some help in understanding brk and sbrk. how does these are
> implemeted(means how the memeory will be allocated). How malloc will
> call these brk and sbrk. Please also give any example C program, where
> brk and sbrk are used to allocate memory.
>
> Appreciate your help in this regard,
>
> Thanks,
> Vikas.



brk, sbrk - you need unistd.h
They change the amount of space allocated for the
calling process's data segment. They change data segment
size .

int brk(void *end_data_segment);
void *sbrk(ptrdiff_t increment);

brk sets the end of the data segment to the value specified by
end_data_segment, when that value is reasonable, the system does have
enough memory and the process does not exceed its max data size (see
setrlimit(2)).

sbrk increments the program's data space by increment bytes. sbrk
isn't a system call, it is just a C library wrapper. Calling sbrk with
an increment of 0 can be used to find the current location of the
program break.

On success, brk returns zero, and sbrk returns a pointer to the start
of the new area. On error, -1 is returned, and errno is set to
ENOMEM.

The amount of allocated space increases as the break value
increases. Newly allocated
space is set to zero. If, how-ever, the same memory space is
reallocated to the same pro-
cess its contents are undefined.

When a program begins execution using execve() the break is set at
the highest location
defined by the program and data storage areas.

getrlimit and setrlimit get and set resource limits respectively. Each
resource has an associated soft and hard limit, as defined by the
rlimit structure (the rlim argument to both getrlimit() and
setrlimit()):

struct rlimit {
rlim_t rlim_cur; /* Soft limit */
rlim_t rlim_max; /* Hard limit (ceiling
for rlim_cur) */
};


The soft limit is the value that the kernel enforces for the
corresponding resource. The hard limit acts as a ceiling for the soft
limit: an unprivileged process may only set its soft limit to a value
in the range from 0 up to the hard limit, and (irreversibly) lower its
hard limit. A privileged process may make arbitrary changes to either
limit value.

Have a look at these links ->
1) http://linux.about.com/library/cmd/blcmdl2_brk.htm
2) http://www.minix3.org/manpages/man2/brk.2.html

Karthik Balaguru

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-19-2007
karthikbalaguru <(E-Mail Removed)> writes:
> On Oct 17, 3:24 pm, venkat <(E-Mail Removed)> wrote:
>> Can some help in understanding brk and sbrk. how does these are
>> implemeted(means how the memeory will be allocated). How malloc will
>> call these brk and sbrk. Please also give any example C program, where
>> brk and sbrk are used to allocate memory.

>
> brk, sbrk - you need unistd.h

[...]

And therefore topical in comp.unix.programmer, not here.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      10-19-2007
On Wed, 17 Oct 2007 10:24:53 -0000, venkat <(E-Mail Removed)>
wrote:

>Hi folks,
>
>Can some help in understanding brk and sbrk. how does these are
>implemeted(means how the memeory will be allocated). How malloc will
>call these brk and sbrk. Please also give any example C program, where
>brk and sbrk are used to allocate memory.
>
>Appreciate your help in this regard,


Since neither is a standard C function, you might have better luck
asking in a newsgroup related to whatever system they are defined on.


Remove del for email
 
Reply With Quote
 
Tor Rustad
Guest
Posts: n/a
 
      10-19-2007
venkat wrote:
> Hi folks,
>
> Can some help in understanding brk and sbrk.


man man

> how does these are
> implemeted(means how the memeory will be allocated).


There are a number of open sources UNIX'es, why not take a look?

> How malloc will
> call these brk and sbrk. Please also give any example C program, where
> brk and sbrk are used to allocate memory.


GNU provide an open source C library, why not take a look?

http://g.oswego.edu/dl/html/malloc.html

--
Tor <torust [at] online [dot] no>

"I have stopped reading Stephen King novels. Now I just read C code instead"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
need help wit sbrk J de Boyne Pollard C++ 0 09-12-2007 11:28 AM
Malloc & sbrk heyber.l@gmail.com C Programming 6 03-06-2006 03:56 AM
apparent 4 GB memory limit for brk() in solaris 8 on some hardware...? clsmyth@fuzzy-elves.org C Programming 8 07-29-2005 01:16 PM
question regarding precedence and associativity of ++ and * maadhuu C Programming 3 07-29-2005 11:26 AM
A question regarding the duality of pointers and arrays and another on structures. Squignibbler@hotmail.com C++ 7 03-26-2005 09:09 PM



Advertisments