简体   繁体   中英

How Stack or memory is allocated for threads under the same process in Linux

For a normal function call, stack frame is created and stored in stack. But
How memory is allocated for two thread within one process and how stack frame is handled when thread call other function.

The current 'thread' concept in Linux is the NPTL one. NPTL uses clone() , which wraps sys_clone() . Allocating a stack for a new 'thread' is handled in the user space (ie. libc), not in kernel (ie. Linux). A library can allocate a stack using the allocation of choice (eg. malloc) and then call clone() passing this address as the stack (of course, needs to pass the top of the allocated region, since stacks grow downwards on most platforms):

Unlike fork(2), clone() allows the child process to share parts of its execution context with the calling process, such as the memory space, the table of file descriptors, and the table of signal handlers. ...

The main use of clone() is to implement threads: multiple threads of control in a program that run concurrently in a shared memory space.

When the child process is created with clone(), it executes the function fn(arg) ...

The child_stack argument specifies the location of the stack used by the child process ...

If you want to learn more specific details, open the source of your distro pthread_create implementation and get reading.

For example pthread_create.c :

int
__pthread_create_2_1 (newthread, attr, start_routine, arg)
  ...
  struct pthread *pd = NULL;
  int err = ALLOCATE_STACK (iattr, &pd);
  ...

and allocatestack.c :

 # define ALLOCATE_STACK(attr, pd) allocate_stack (attr, pd, &stackaddr)

 static int
 allocate_stack (const struct pthread_attr *attr, struct pthread **pdp,
    ALLOCATE_STACK_PARMS)
 ...

You'll see that stack allocation has some whistles and bells, like caching and reusing stack regions, guard pages , but in the end is just a memory region allocated in user space.

As someone commented each thread have it's own stack. When function gets called from that thread new stack frame is created in that stack.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM