In a Linux process, each thread has its own stack. Besides that, what else is local to each thread. I have read things such as file allocation table, etc... Can someone provide me a list of things which are specific to a thread and how they are arranged in the memory.
Secondly, I have noticed that when I allocate a stack to a thread (See code below), the address of the first variable in the thread function is somehow quite bytes after the stack address which I allocated (stackAddr). I think that is because the top of the stack is the end address of the allocated stack memory, as the difference in value of the address of the local variable and the allocated stack is approximately the size of the stack (STACKSIZE). In other words, it looks like its growing from bottom towards the top.
pthread_attr_init( &attr[tid] );
stackAddr = malloc(STACKSIZE);
pthread_attr_setstack( &attr, stackAddr, STACKSIZE );
At a minimum, a thread's stack is used to store the location of a return address provided by the caller in order to allow return statements to return to the correct location. The stack is often used to store variables of fixed length local to the currently active functions.
Threads are sometimes called lightweight processes because they have their own stack but can access shared data. Because threads share the same address space as the process and other threads within the process, the operational cost of communication between the threads is low, which is an advantage.
Stack - Since each thread can have its own execution sequence/code, it must have its own stack on which it might push/pop its program counter contents (when say function calls and returns happen). So threads of same process do not share stack.
Note: stack and registers can't be shared among the threads. Each thread has its own stack and registers.
For the first question I can think of:
pthread_setspecific
and __thread
storage class)Second, yes, you are right, on x86 the stack grows to lower addresses. So, if you're using pthread_attr_setstack
the area will begin to be used from the end.
Per POSIX XBD 3.396
A single flow of control within a process. Each thread has its own thread ID, scheduling priority and policy, errno value, thread-specific key/value bindings, and the required system resources to support a flow of control. Anything whose address may be determined by a thread, including but not limited to static variables, storage obtained via malloc(), directly addressable storage obtained through implementation-defined functions, and automatic variables, are accessible to all threads in the same process.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With