线程除了栈之外还有什么
在Linux进程中,每个线程都有自己的堆栈。除此之外,还有什么是每个线程本地的。我已经阅读了诸如文件分配表等内容...有人可以为我提供特定于线程的内容列表以及它们在内存中的排列方式。
其次,我注意到,当我向线程分配堆栈时(参见下面的代码),线程函数中第一个变量的地址在我分配的堆栈地址之后不知何故有相当多的字节(stackAddr )。我认为这是因为堆栈顶部是分配的堆栈内存的结束地址,因为局部变量的地址和分配的堆栈的值的差值大约是堆栈的大小(STACKSIZE)。换句话说,它看起来像是从下往上生长的。
pthread_attr_init( &attr[tid] );
stackAddr = malloc(STACKSIZE);
pthread_attr_setstack( &attr, stackAddr, STACKSIZE );
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 );
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于我能想到的第一个问题:
pthread_setspecific
和__thread
存储类设置的键/值对)其次,是的,你是对的,在 x86 上,堆栈增长到较低的地址。因此,如果您使用
pthread_attr_setstack
该区域将从末尾开始使用。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.根据 POSIX XBD 3.396
Per POSIX XBD 3.396
在 Linux 上,如果应用程序程序员选择使用本机
clone()
API 而不是 pthreads 线程,那么他们可以很好地控制哪些资源是每个线程私有的,哪些资源与其他线程共享执行。这意味着不可能给出明确的答案 - 线程特定的资源取决于创建线程时传递给
clone()
的标志。另请注意,其中许多资源根本不存在于用户空间内存中 - 像信号掩码之类的东西保存在内核中。
On Linux the application programmer has a great deal of control over which resources are private to each thread and which are shared with other threads, if they choose to use the native
clone()
API rather than the pthreads threading implementation.This means that it's impossible to give a definitive answer - the resources which are thread-specific depend on which flags were passed to
clone()
when the thread was created.Note also that many of these resources do not live in userspace memory at all - things like the signal mask are kept within the kernel.