Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is there to a thread beside a stack

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 );
like image 950
MetallicPriest Avatar asked Nov 14 '11 15:11

MetallicPriest


People also ask

What is a thread stack?

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.

What does thread stack contain?

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.

Why do threads not share stack?

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.

What does a thread not share?

Note: stack and registers can't be shared among the threads. Each thread has its own stack and registers.


2 Answers

For the first question I can think of:

  • thread id
  • Stack
  • machine registers
  • threads-specific variables (both key/value pairs set by pthread_setspecific and __thread storage class)
  • signal mask
  • set of pending signals
  • errno value

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.

like image 130
chill Avatar answered Sep 29 '22 11:09

chill


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.

like image 41
R.. GitHub STOP HELPING ICE Avatar answered Sep 29 '22 12:09

R.. GitHub STOP HELPING ICE