The program discontinuity, which marks the termination of the process’s data segment, i.e., the program breakdown is the first position following the completion of the uninitialized data segment, can be changed using brk(). Increasing the program break allows memory to the process; reducing the break allows memory to the process. When that amount is sensible, the system would have enough memory, and the process does not surpass its maximum data size, brk() sets the ending of the data segment to the value supplied by the addr. Brk() returns zero if it succeeds. If there comes an error, -1 is returned. The system functions brk(), and sbrk() is utilized to regulate the amount of memory allotted towards the process’s data segment. Usually, these functions are invoked from a larger memory management library function like malloc. The program break, which identifies the end of the process’s heap section, is moved about with brk() and sbrk(). brk() assigns the value of addr to the ending of the heap segment. sbrk() increases the heap space of the program by increment bytes. It takes you back to the earlier program break. The present location of the program break can be found by calling sbrk() with just a raise of 0.

Syntax

#include

int brk(void *end_data_segment);

void *sbrk(intptr_t increment);

Explanation of syntax

The brk() system call causes the program to break at the end data segment position. The end data segment is rounded up to the next page boundaries since virtual memory is allocated in page units. Attempts to lower the program break value below its initial value will certainly result in unexpected behavior, such as a segmentation fault when attempting to access data in now-inaccessible parts of the initialized or uninitialized data segments. Several factors determine the exact upper limit for where the program break can be placed.

Why use brk() system call in C

The brk() function is used to modify the amount of memory allocated to the caller process. The modification is made by altering the break value of the process to addr and providing the necessary number of space. The brk() function is used to modify the amount of memory allocated to the caller process. As the break value rises, so does the quantity of allocated space. The newly allocated space has a value of 0 assigned to it. The values of the reallocated space are not emptied if the program first decrements and afterwards increments the break value.

Pre-requisites

  • Access to Ubuntu 20.04 Operating system
  • Installation of the gcc compiler

Example to explain brk() in C

We have designed the simplest example to elaborate the concept for using the brk() system call in C. Let’s create a file with the .c extension by using the nano command in the shell. Execute the appended below command in the shell:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Brk-system-call-C-01.png" data-lazy- height="29" src="data:image/svg xml,” width=”974″>

In Linux operating systems, GNU nano is a simple command-line text editor. It has all of the fundamental features you’d assume from a text editor, as well as a few more. Upon execution, the file will be opened in GNU editor. Write the displayed code in your file with few amendments in it depending on your work requirements.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Brk-system-call-C-02.png" data-lazy- height="766" src="data:image/svg xml,” width=”974″>

In the code attached in the above screenshot, Argument count is abbreviated as argc, and argument values are abbreviated as argv. These are the variables that are passed to the main function when it begins to run. At runtime, the sysconf function retrieves configuration information. sysconf with the argument _SC PAGESIZE is the correct interface to enquire about page size. sbrk() increases the program’s storage space by increasing bytes. The present location of the program break can be found by calling sbrk() with an incremental of 0. The brk() system call causes the program to break at the end data segment position. The end data segment is rounded up to the next page boundary because virtual memory is assigned in page units.

The output of the code can be displayed by executing the cited below instruction in the Ubuntu 20.04 Linux operating system terminal.

In case GCC is not installed in your Ubuntu 20.04 operating system, install it by running the appended below command in the shell:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Brk-system-call-C-03.png" data-lazy- height="162" src="data:image/svg xml,” width=”974″>

The output is displayed in the above-attached screenshot presenting page size, program break address, size of char, etc.

Conclusion

This article was about brk() system call in C programming language. We have tried our best to explain the concept of brk() system call in C programming language and its usage. One of the examples has been explained in detail for your understanding. Implementing this example with the required changes will teach you the brk() system call usage in the C programming language.

About the author

<img alt="" data-lazy-src="https://secure.gravatar.com/avatar/d014e3711df41253029f4d4199698df8?s=112&r=g" data-lazy- height="112" src="data:image/svg xml,” width=”112″>

Kalsoom Akhtar

Hello, I am a freelance writer and usually write for Linux and other technology related content