Hardware Reference
In-Depth Information
At the lowest end of the address space is the “text” region containing the program
code. This region of virtual memory is read-only, containing read-only program constants
in addition to executable code.
The next region (in increasing address) contains blocks of uninitialized arrays,
buffers, static C variables, and extern storage.
At the high end of memory are environment variables for the program, like PATH . You
can easily check this yourself by using getenv("PATH") and printing the returned address
for it. Its address will likely be the highest address in your Raspberry Pi application, except
possibly for another environment variable.
Below that, your main program's stack begins and grows downward. Each function
call causes a new stack frame to be created below the current one.
If you now add a thread to the program, a new stack has to be allocated for it.
Experiments on the Pi show that the first thread stack gets created approximately 123 MB
below the main stack's beginning. A second thread has its stack allocated about 8 MB
below the first. Each new thread's stack (by default) is allocated 8 MB of stack space.
Dynamically allocated memory gets allocated from the heap , which sits between the
static / extern region and the bottom end of the stack.
Threads
Before threads were perfected under Linux, many application developers tended to avoid
them. Now, however, there is little reason to.
Every attempt was made to keep the project programs in this topic simple.
This usually meant also avoiding threads. Yet, a few projects would have been more
complicated without them. In the example using ØMQ, threads would have been present
behind the scenes, even if we didn't see them in our application code.
With that introduction, let's take a crash course on the pthread API as it applies to
Raspbian Linux.
pthread Headers
All pthread functions require the following header file:
#include <pthread.h>
When linking programs compiled to use pthreads, add the linker option:
-lpthread : Link with the pthread library.
pthread Error Handling
The pthread routines return zero when they succeed and return an error code when they
fail . The value errno is not used for these calls.
 
Search WWH ::




Custom Search