Questions tagged [brk]

brk() and sbrk() change the location of the program break, which defines the end of the process's data segment (i.e., the program break is the first location after the end of the uninitialized data segment).

From the Linux man page:

brk() and sbrk() change the location of the program break, which defines the end of the process's data segment (i.e., the program break is the first location after the end of the uninitialized data segment). Increasing the program break has the effect of allocating memory to the process; decreasing the break deallocates memory.

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

sbrk() increments the program's data space by increment bytes. Calling sbrk() with an increment of 0 can be used to find the current location of the program break.

See also this SO question.

46 questions
244
votes
8 answers

What does the brk() system call do?

According to Linux programmers manual: brk() and sbrk() change the location of the program break, which defines the end of the process's data segment. What does the data segment mean over here? Is it just the data segment or data, BSS, and heap…
nik
  • 8,387
  • 13
  • 36
  • 44
33
votes
5 answers

In malloc, why use brk at all? Why not just use mmap?

Typical implementations of malloc use brk/sbrk as the primary means of claiming memory from the OS. However, they also use mmap to get chunks for large allocations. Is there a real benefit to using brk instead of mmap, or is it just tradition?…
Nate C-K
  • 5,744
  • 2
  • 29
  • 45
20
votes
4 answers

How are sbrk/brk implemented in Linux?

I was thinking about how the Linux kernel implements system calls and I was wondering if someone could give me a high level view of how sbrk/brk work? I've reviewed the kernel code, but there is just so much of it and I don't understand it. I was…
samoz
  • 56,849
  • 55
  • 141
  • 195
17
votes
1 answer

Why does calling sbrk(0) twice give a different value?

I'm trying to understand the sbrk() function. From what I know: sbrk(0) returns the current address of the break and doesn't increment it. sbrk(size) increments the address of the break by size bytes and returns the previous address of the…
Bibas
  • 498
  • 1
  • 4
  • 17
10
votes
1 answer

What's unsafe/legacy about brk/sbrk?

I've heard in a lot of places (musl mailing list, macOS forums, etc.) that brk() and sbrk() are unsafe. Many of these places either don't give explanations at all, or give very vague explanations. For example, this link states that "these functions…
S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
9
votes
5 answers

What is program break? Where does it start from,0x00?

int brk(void *end_data_segment); void *sbrk(intptr_t increment); Calling sbrk() with an increment of 0 can be used to find the current location of the program break. What is program break? Where does it start from,0x00?
cpuer
  • 7,413
  • 14
  • 35
  • 39
9
votes
4 answers

Assembly x86 brk() call use

I am trying to dynamically allocate memory into the heap and then assign values in those memory addresses. I understand how to allocate the memory but how would I assign for example the value in a register to that first dynamic memory address? This…
8
votes
2 answers

What do brk and sbrk stand for?

While I know what the Unix system call brk and function sbrk do, I have no idea what they stand for. Can anyone enlighten me?
Thomas Eding
  • 35,312
  • 13
  • 75
  • 106
5
votes
2 answers

who is calling the brk(NULL) and why?

I have written a small hello world program, and run strace on its binary, and it listed all system calls which have been called as part of execution of my Hello_world program. strace ./a.out execve("./a.out", ["./a.out"], [/* 40 vars */]) =…
Ellanti Kishore
  • 104
  • 1
  • 9
3
votes
1 answer

How does sbrk() work?

I'm trying to understand how sbrk works. Here is my little code: int main() { printf("end of the break : %p\n", sbrk(0)); printf("end of the break : %p\n", sbrk(10)); printf("new end of the break : %p\n\n", sbrk(0)); } This outputs:…
Myranova
  • 123
  • 1
  • 10
3
votes
4 answers

x86_64 printf segfault after brk call

While i was trying do use brk (int 0x80 with 45 in %rax) to implement a simple memory manager program in assembly and print the blocks in order, i kept getting segfault. After a while i could only reproduce the error, but have no idea why is this…
gmb11
  • 33
  • 6
2
votes
2 answers

Will malloc round up to the nearest page size?

I'm not sure if I'm asking a noob question here, but here I go. I also searched a lot for a similar question, but I got nothing. So, I know how mmap and brk work and that, regardless of the length you enter, it will round it up to the nearest page…
mediocrevegetable1
  • 4,086
  • 1
  • 11
  • 33
2
votes
1 answer

Why is the return value of the first sbrk different than subsequent calls?

I am trying to understand how memory works in C, so am experimenting with the sbrk function now. I know that sbrk(0) should return the current program break, that is the end of the data segment. So I tried to call sbrk(0) multiple times and for some…
aboria
  • 123
  • 6
2
votes
0 answers

Strace detects calls to brk, but GDB doesn't stop on breakpoint

I've been trying debugging memory leaks for quite a bit of time now. My main problem is not being able to use proper tools like Valgrind and the like, so I settled for plain GDB + strace. My program is a loop. On each iteration, it creates some…
user6245072
  • 2,051
  • 21
  • 34
2
votes
1 answer

Long running process debug. Strace, brk

I'm have Magento webshop, it run cron jobs to execute scheduled processes. One of them is product import. It's responsible for importing of products and assigning them to categories. According to sql state I can see this part was finished. After it…
Viacheslav Kondratiuk
  • 8,493
  • 9
  • 49
  • 81
1
2 3 4