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
0
votes
1 answer

Why are user processes responsible for requesting memory from the OS?

Heap allocators are responsible for actively requesting memory from the OS to grow the heap, e.g., with brk or mmap. Accessing unallocated memory leads to segfaults. I could design a different OS-user interface, where users could freely read/write…
Dragonsheep
  • 242
  • 3
  • 10
0
votes
1 answer

Can I enforce sbrk return address to be within a certain specific range?

I want to make sure the return address of sbrk is within a certain specific range. I read somewhere that sbrk allocates from an area allocated at program initialization. So I'm wondering if there's anyway I can enforce the program initialization to…
AetX
  • 193
  • 6
0
votes
1 answer

Need to align memory on a power of 2 and align the program break on a multiple of 2 * getpagesize() in C

I'm re-coding the malloc function using brk, sbrk & getpagesize() I must follow two rules: 1) I must align my memory on a power of 2 It means: If the call to malloc is : malloc(9); i must return them a block of 16 byte. ( the nearest power of…
0
votes
1 answer

why do the argument of function brk() is void* and not int type?

I was looking at the documentation of the function int brk() in the Linux guide: SYNOPSIS #include int brk(void *addr); void *sbrk(intptr_t increment); Feature Test Macro Requirements for glibc (see…
E. Ginzburg
  • 253
  • 2
  • 9
0
votes
0 answers

assembly x86 allocating array of structs dynamically using system calls

I need help with allocating memory with system calls in homework project. I get a number 'x' from the command line argument and I need to allocating array of 'x' structs which include two fields of dword. I tried do that using the brk system call…
UltimateMath
  • 75
  • 2
  • 8
0
votes
2 answers

Bash script segfaults on brk()

can someone explain why this "endless" loop segfaults quickly? For example, let's say we have this function: #!/bin/bash foo() { foo }; foo This segfaults after 8-10 seconds. Examining via strace, we can see a lot of brk()…
py9
  • 598
  • 5
  • 15
0
votes
1 answer

is brk(0) taking taking too much time?

This is an output of strace -T -ttt -ff -x -y -o pid.trace -p 2145 . Trace given below. 1503431273.934716 semop(1204093022, {{0, 1, SEM_UNDO}}, 1) = 0 <0.000004> 1503431273.934737 clock_gettime(CLOCK_REALTIME, {1503431273, 934741536}) = 0…
Umesh C G
  • 45
  • 5
0
votes
1 answer

splitting an sbrk into 2

To recode the malloc function, I do a sbrk(stack) where : void *malloc(size_t size) { stack = 0; while (stack < size) stack += 4096; } I malloc always more than I need then I want to take some of this allocated area of size size and return…
0
votes
0 answers

Mimicking sbrk in 32-bit nasm assembly

Hey guys for my second year Computer Science project we were tasked with creating a garbage collector in 32-bit nasm assembly. I managed to get everything running fine except one thing. We are suppose to mimick the gclib sbrk function using the…
Zhunaid
  • 1
  • 2
0
votes
1 answer

Can't change heap size AMD64

I'm trying to increase the heap size by 100 by changing the brk and I don't know why my code doesn't work. Here is the part of the code that tries do it: movq $0, %rdi movq $12, %rax syscall movq %rax, InicialHeap movq InicialHeap, %rsi mov $str,…
0
votes
1 answer

Where does my allocated memory actually start from when i use brk system call

I am trying to allocate some memory using sys_brk in NASM/x86 assembly. sys_break returns the new address of break, which is the end of the data segment right? So where does my newly allocated memory reside? I assumed that it is in between the old…
omerfirmak
  • 163
  • 1
  • 11
0
votes
1 answer

using brk to get brk_start

Manual for int brk(void *end_data_segment); says: "brk() sets the end of the data segment to the value specified by end_data_segment" On Success it returns 0 else -1. But how to I get the init value of my break (like sbrk(0))? best regards,
user1735225
  • 113
  • 10
0
votes
1 answer

Kernel Source -- In which file is brk() defined

I would like to know in kernel source version >= 2.6 where brk is defined. That is which c file contains its definition? grep is not revealing much. Also sbrk is implemented in glibc correct?
Matthew Hoggan
  • 7,402
  • 16
  • 75
  • 140
-1
votes
1 answer

Raising BRK in assembly on i386 Linux

I found and studied x86 memory access segmentation fault and it won't work in my code. The difference perhaps being that I don't use separate .text and .data segments but keep all in a single segment by creating a custom ELF header. Does that…
LNoor
  • 91
  • 4
-2
votes
1 answer

Linux: brk() error 'Cannot allocate memory'

I'm trying to use the brk() function in a C program. My goal is to use it directly (as part of a larger test) by checking the current program break (pb) with void *current_break = sbrk(0); executing a malloc (for testing as malloc should sometimes…
MajorasKid
  • 733
  • 2
  • 5
  • 24