线程除了栈之外还有什么

发布于 2024-12-15 07:22:36 字数 461 浏览 1 评论 0原文

在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

土豪 2024-12-22 07:22:36

对于我能想到的第一个问题:

  • 线程id
  • 堆栈
  • 机注册
  • 线程特定的变量(由pthread_setspecific__thread存储类设置的键/值对)
  • 信号掩码
  • 待处理信号集
  • errno 值

其次,是的,你是对的,在 x86 上,堆栈增长到较低的地址。因此,如果您使用 pthread_attr_setstack 该区域将从末尾开始使用。

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.

薯片软お妹 2024-12-22 07:22:36

根据 POSIX XBD 3.396

进程内的单一控制流。每个线程都有自己的线程 ID、调度优先级和策略、errno 值、线程特定的键/值绑定以及支持控制流所需的系统资源。任何其地址可以由线程确定的东西,包括但不限于静态变量、通过 malloc() 获得的存储、通过实现定义的函数获得的直接可寻址存储以及自动变量,都可以被同一进程中的所有线程访问。< /p>

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.

空城缀染半城烟沙 2024-12-22 07:22:36

在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文