473,378 Members | 1,413 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Regarding brk and sbrk

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.

Oct 17 '07 #1
12 12621
On Oct 17, 1:24 pm, venkat <venkatavi...@gmail.comwrote:
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).

Oct 17 '07 #2

"venkat" <ve**********@gmail.comwrote in message
news:11**********************@v23g2000prn.googlegr 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.

Oct 17 '07 #3
On Oct 17, 3:24 am, venkat <venkatavi...@gmail.comwrote:
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.

Oct 17 '07 #4
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
Oct 18 '07 #5
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

Oct 19 '07 #6
On Oct 17, 3:24 pm, venkat <venkatavi...@gmail.comwrote:
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

Oct 19 '07 #7
karthikbalaguru <ka***************@gmail.comwrites:
On Oct 17, 3:24 pm, venkat <venkatavi...@gmail.comwrote:
>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) ks***@mib.org <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"
Oct 19 '07 #8
On Wed, 17 Oct 2007 10:24:53 -0000, venkat <ve**********@gmail.com>
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
Oct 19 '07 #9
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"
Oct 19 '07 #10
On Oct 17, 3:24 pm, venkat <venkatavi...@gmail.comwrote:
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,
The data region corresponds to the data-bss sections(initialized and
uninitialized data, Static
variables) of the executable file.

Its size can be changed with the brk(2) system call.

If the expansion of the bss data or the user stack exhausts available
memory,
the process is blocked and is rescheduled to run again with a larger
memory
space. New memory is added between the data and stack segments.

Karthik Balaguru

Oct 24 '07 #11
In article <11*********************@y27g2000pre.googlegroups. com>,
karthikbalaguru <ka***************@gmail.comwrote:
>The data region corresponds to the data-bss sections(initialized and
uninitialized data, Static
variables) of the executable file.
>Its size can be changed with the brk(2) system call.
>If the expansion of the bss data or the user stack exhausts available
memory,
the process is blocked and is rescheduled to run again with a larger
memory
space. New memory is added between the data and stack segments.
I don't know what system you are describing, but
A) It isn't defined by C; and
B) Matters are handled in *completely* different ways on some systems; and
C) The part about blocking isn't even the way things get handled
on the Unix systems I've used.
--
"Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us." -- Ecclesiastes
Oct 24 '07 #12
karthikbalaguru wrote:
venkat <venkatavi...@gmail.comwrote:
>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.

The data region corresponds to the data-bss sections (initialized
and uninitialized data, Static variables) of the executable file.

Its size can be changed with the brk(2) system call.

If the expansion of the bss data or the user stack exhausts
available memory, the process is blocked and is rescheduled to
run again with a larger memory space. New memory is added
between the data and stack segments.
This only applies to some specific operating systems, and is
off-topic for c.l.c. Please don't give off-topic info here - it is
better to refer the asker to some other newsgroup where answers
will be properly vetted.

--
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

Oct 25 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Francis Lavoie | last post by:
Hello I have some questions regarding webframework, I must say that I quite lost and these questions are basicly to help me understand the way it work. I have some knowledge with PHP and JSP....
3
by: praba kar | last post by:
Dear All, I am new to Python. I am in need of some sorting functions (eg) numerical sorting functions and alphapetical sorting functions. I have searched through net But I cannot find any...
3
by: Samuel | last post by:
I wrote a very simple httpmodule and tried to compile it with no success. This is my code: ============== Imports System Imports System.Web Imports Microsoft.VisualBasic NameSpace...
7
by: Squignibbler | last post by:
Hi all, I have a question regarding the C++ programming language regarding the nature of the relationship between pointers and arrays. If the statement MyArray is functionally identical to...
8
by: Mike | last post by:
Hello, I have a few rather urgent questions that I hope someone can help with (I need to figure this out prior to a meeting tomorrow.) First, a bit of background: The company I work for is...
2
by: Dean R. Henderson | last post by:
For an ASP.NET web application, is there a way for one session (with appropriate security authorization) to set a HttpSessionState variable to point to another session and execute the Abandon...
6
by: heyber.l | last post by:
Hi, could someone explain me how malloc() (particularly doug lea's) deals with programs who calls sbrk on their own. What happens to the "Wilderness" chunk? Thanks.
0
by: J de Boyne Pollard | last post by:
hNow the next step is to write malloc and free, for kernel hheap management. I am keeping my kernel heap at h0XD0000000. My understanding is that malloc calls sbrk hto allocate more memory to the...
7
by: somenath | last post by:
Hi All , As a process of my C language learning I was trying to learn how malloc can be implemented from K&R2 .But I am not able to understand few points . It would be very much helpful if...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.