On Thu, Sep 24, 2009 at 3:23 PM, CP YEH <[email protected]> wrote:

> Thank you very much for your answer. If you don't mind, let me ask you
> little bit further.
>
> I can see from the code that stack is being shared by different
> threads and what I am curious about is the way kernel manages this.
> In other words, if I have two threads sharing the same stack, how does
> kernel ensures the validity of the stack for each thread?
> In my mind, if there is only one stack for both threads, it is very
> possible that one thread ends us returning to the the address that the
> other thread was supposed to return.
> Could you please explain briefly how this is prevented? Thank you very
> much.
>
> YEH
>
> On Thu, Sep 24, 2009 at 2:03 AM, askb <[email protected]> wrote:
> > On Wed, 2009-09-23 at 23:28 -0400, CP YEH wrote:
> >
> > Hi,
> >
> > I am just wondering how the kernel manages user space stack for
> > different threads.
> >
> > I tried to follow the code and noticed that if CLONE_VM is specified,
> > the kernel simply points mm to parent's mm. I suppose pthread does
> > specify CLONE_VM so this true for pthread.
> >
> >
> > In this case, both parent and thread points to the same mm and
> > especially same stack and I just can't quite understand how this is
> > possible.
> > Could anyone give me some insight on this?
> >
> > Threads are created along side with other flags like - CLONE_VM CLONE_FS
> > CLONE_FILES CLONE_SIGHAND for sharing the resources belonging to the same
> > address space. Though each thread has its own stack within the process
> > address space, the stack is also shared among the other threads. So one
> > threads stack could be readable be the other threads.
> >
> > Thank you very much in advance.
> >
> > YEH <http://kernelnewbies.org/FAQ>
>
Each thread has its own kernel stack.
include/linux/sched.h:
union thread_union {
        struct thread_info thread_info;
        unsigned long stack[THREAD_SIZE/sizeof(long)];
};
If you look to the dup_task_struct() (from kernel/fork.c) function, you'll
see the following:
.............
ti = alloc_thread_info(tsk);
        if (!ti) {
                free_task_struct(tsk);
                return NULL;
        }

        err = arch_dup_task_struct(tsk, orig);
        if (err)
                goto out;

        tsk->stack = ti;
...............
Pointer to thread_info stored in tsk->stack



-- 
Regards,
Denis

Reply via email to