Re: Stack among threads

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Mulyadi,

--- Mulyadi Santosa <mulyadi.santosa@xxxxxxxxx> escreveu:

> Hi...
> 
> > I have some threads program and the /proc/pid/maps output show the
> > same stack (initial and final addresses) for all running threads of
> > an application. According to the definition of threads, each thread
> > should contain its own stack, but how Linux can identify it?
> 
> I think, even the addresses seem equal, physically they are different
> 
> pages. IMHO this is done so that the kernel doesn't need to "tangle" 
> with SP (stack pointer).

So you are saying that each thread belongs to an application contains
its own stack in terms of physical pages and not virtual memory, right?

If a CLONE_VM is used as a flag for clone() function, the stack area
pointed by memory descriptor "mm" just means the threads share
virtually the same stack.

> 
> Hm, about how to identify, IMHO there are several possibilities:
> 1. The threads explicitly create a memory region (VMA) and ask the 
> kernel to assign it as the stack area. Since this is just another VMA
> 
> in the same process address space, the kernel just needs to update
> the 
> SP(Stack Pointer) to initially point to this VMA when the thread is 
> selected to run

According to your explanation, each thread uses a VMA as a stack area,
but the maps entry under proc filesystem shows an unique VMA considered
as stack for all threads, since their virtual addresses are equal. This
happens with Firefox web browser, for instance the threads with PID
4274 and 4275 have the same VMA according to maps output:

bfd47000-bfd5b000 rwxp bfd47000 00:00 0          [stack].

Any comments?

Or there is just one common VMA considered as a stack and all threads
point in different location in this virtually shared stack along the
time?

> 
> 2. The thread doesn't explicitly ask for a thread stack. Here, the 
> kernel marks the parent process's stack as COW. As soon as the new 
> thread uses (i.e write) the stack, copy-on-write happens and the
> kernel 
> automatically setups new page frames as the stack. Of course, PTE
> (Page 
> Table Entry)-s are updated as well. Here, I think SP is still the
> same 
> since basically the virtual address doesn't change.

If COW happens to stack, it means that CLONE_VM was not set during the
creation of child process, so the parent and the child will not share
the same address space. My question is related when VM_CLONE is set, so
the parent and child will use the same memory space, but how stack is
used among many threads in this situation?

> 
> > Does thread in Linux contain its own stack?
> 
> IMHO yes, because if it doesn't, local variables and stack frames
> will 
> mix and that's bad.

I agree with you that each thread should contain its own stack, but
again the maps output just show one VMA related to stack for all
threads.

BR,

Mauricio Lin.

> 
> > Since Linux kernel does not provide an explicit support for
> threads,
> > the stack can be a shared resource among threads, right?
> 
> I think, theoritically it can, as long as it doesn't either declare
> or 
> allocate local variables or do function call, since both utilize
> stack. 
> Ehm wait, maybe if the thread uses TLS (Thread Local Storage), then  
> stack won't be needed. But, I am not sure.
> 
> regards,
> 
> Mulyadi
> 
> 



	
	
		
_______________________________________________________ 
O Yahoo! está de cara nova. Venha conferir! 
http://br.yahoo.com

--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux