Syntax
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:
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.
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.
$./a.out
In case GCC is not installed in your Ubuntu 20.04 operating system, install it by running the appended below command in the shell:
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.