Memory Management and System calls



1 Introduction

Memory layout of a process is a very misunderstood and underemphasized topic. Although a programmer need not worry about how the memory of a process is organized but I feel that every programmer must have knowledge about the memory map of the process for which he/she is writing the code. This knowledge will aid the programmer to write space/time efficient code.

Another topic which is not very clear to startup programmers is difference between system calls and library calls. Many programmers think that library calls are always mapped to system calls and time taken by each is nearly same which is not completely correct. The second part of the article explains some differences between system calls and library calls.

1.1 Memory Map

Every running process requires some kind of memory for execution. In most of the computer system (with exception of some embedded systems), some kind of virtual memory is provided for processes. The amount of this memory that a process maps in depends upon the OS basic mode (which is mapped to basic machine integer size). It is either 2^32 i.e. 4GB or 2^64 i.e., 16TB. As to how much is used by the process, depends upon the nature of work done by it. The used up memory is mapped to actual physical memory by a memory translator oten called MMU.

The main ingredients of any generic process’s virtual memory map is as follows


1.1.1 OS Kernel

Lowermost portion in a map. This region is one to one mapping woth actual physical memory and most of kernel specific things lie here. Process could address it only via special system calls.

1.1.2 Stack

Process uses stack to store global and local variables. It is a modifiable segments whoose size depends upon the number of routines called from within the process. Its typically 8MB (or 16MB) and may reesuld in a system crash on overflow.

1.1.3 Heap

This is used by process to get some dynamic memory (calls like malloc, calloc, new allocates space from heap). It is usually placed above stack (where stack grows down).

1.1.4 Data

Data section contains global variables (initialized), constants, and static variables. Data section is further divided into a read only data and modifiable data section. Read only section contains constants and other non writable data. It is often implemented by a normal RAM with write input disabled.

1.1.5 Text

Program code in form of assembly instruction

1.1.6 BSS

This is called the Base Sub Segment. It contains all uinitialized variables (global as well as local).

1.1.7 Extra Segment

Contains additional things like meta ifo, string literals etc. It is optional.

The linux command pmap gives the memroy pmap - report memory map of a process


1.2 System calls vs Library calls

In case of time (and even space) critical code specially targeting embedded soft real time systems, for any application programmer it is extremely important to know that how much expensive is calling a system call as compared to calling a library call (if at all) and how big this difference is (within in limits of code security).

As a rule of thumb, the time taken by a API implemented as a syscall is lesser than the same API being implemented a library call. Most of the library call produce (or are mapped to) a corresponding syscall after more or less preprocessing. Some library calls (particularly string manipulation calls) e.g., strncmp()does not map to any system call.

System calls frequently use assembly language conventions for argument placement in machine registers and transfer of control, while library calls usually use the conventions of the ABI (Application Binary Interface) for the library, or the general argument and calling conventions established by the compiler for subroutine linkage.

1.2.1 Benchmarks

The best way to benchmark any code snippets is to time instructions from it preferably wall time with repeated MHz times. The overhead of a library call is moderate only when it is in RAM else it will be high due to copy and swap operations. For instance, on x86 the overhead of a system call is fairly high due to memory protection mechanism.


1.3 Conclusions

The main intend of this article is to give a preliminary description of memory layout of a process as well as throw some light execution time period of a syscall and libcall. Both the concepts find high applicability in embedded systems. Knowledge of memory map will help a programmer write compact and efficient code while syscall/libcall tradeoff will help him/her reduce time complexity of any code snippet though code complexity may increase. 1