HP-UX HB v13.00 Ch-11 - Software Development

HP-UX Handbook Rev 13.00 Page 31 (of 101)
Chapter 11 Software Development
October 29, 2013
Process Private Memory
A process has three methods to store private data. It can store it on the process heap, save it on
the stack, or in privately mapped regions. All three methods use the second quadrant, as it is the
only quadrant for private memory, but they follow different allocation rules. Every program has
heap and stack, but it will only have mapped regions if it explicitly creates them.
The heap starts at the beginning of Q2. When executing a new program, the heap has a size of
zero, and to store data on the heap, the program must request memory from the kernel via the
brk(2) or sbrk(2) system calls. If a region of the heap is not needed anymore, it can be freed
(deallocated) again. This will not return the memory to the kernel, it is only marked to be free for
reuse within the program. Memory allocation on the heap is independent from the program flow.
Allocation and deallocation can be done at any time by means of the libc functions malloc(2)
and free(2), which in turn use the brk(2) or sbrk(2) system calls.
The stack is a reserved area at the end of Q2. Storing data on the stack follows the "last-in first-
out" scheme, just like a stack of paper. The last sheet you have put on the stack is on top and will
be the first you get if you take one off. The stack has a fixed size which is reserved when the
process is created. The program does not need to request memory from the kernel to store data
on the stack. But it also cannot request more memory for the stack if it is full.
There is a special processor register, the stack pointer, which always contains the next free
address on the top of the stack. Each time a function is called, memory for all local variables is
reserved automatically by simply adding the required size to the stack pointer. In the same way
the stack pointer is decreased when the program returns from the function, because the local data
is not needed anymore.
Besides the local variables, the so called procedure context is also stored on the stack. The
procedure context contains information about return addresses, function parameters, saved
processor registers and several other important things to continue program execution when the
current function returns. The whole program flow control information is stored on the stack.
Privately mapped regions can be allocated and deallocated in the address range between the
upper end of the heap and the lower end of the stack via the libc system calls mmap(2) and
munmap(2). Per default, new regions will be mapped at the highest possible address. The
memory allocated by a call to mmap(2) will be returned to the system after it has been unmapped
with munmap(2).
In a multithreaded program, each thread needs its own stack. The initial thread which was started
on process creation, the so called primordial thread, uses the process stack. Whenever a new
thread is started with pthread_create(3T), a new private region is mapped with mmap(2)
which is used as the thread stack. This area is unmapped when the thread exits.