首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

关于进程与线程堆栈的有关问题

2013-09-05 
关于进程与线程堆栈的问题各位大神,能不能解说一下(1)当一个父进程创建多个子线程后,子线程的堆空间和栈空

关于进程与线程堆栈的问题
各位大神,能不能解说一下
(1)当一个父进程创建多个子线程后,子线程的堆空间和栈空间是在父进程的堆栈空间中开辟的呢,还是在父进程内存空间之外的内存空间为各个子线程开辟了空间呢?
(2)每个子线程都有一个对应自己的栈空间,这个栈空间与父进程的栈空间有什么关系啊?
(3)多个子线程是共享父进程的对空间的?怎么个共享方法呢?每个子线程共享同一首地址开始的内存空间,这不太可能吧?
(4)“进程可以创建多个子线程,这些子线程共享相同的内存地址空间”这是书上的一句话,能不能解释一哈,什么叫做共享相同的内存地址空间啊?
(5)“父进程的多个子线程从同一堆中分配对象”,什么意思啊?从同一堆中分配什么对象啊?

谢谢大神的解释,感激不尽! 线程 进程 栈 堆 内存
[解决办法]
1、进程的地址空间
每个进程的地址空间是独立的,是操作系统把物理内存映射到进程的虚拟地址上。
通俗的理解:如果有a.exe和b.exe同时在系统中运行,那么a和b都可以有0x00000000-0xffffffff的虚拟地址空间(不考虑操作系统占用等因素)。
假如a b都有1个变量在地址 0x12345678处,看起来地址一样,实际上在物理内存中不是一个地方。
进程只能访问自己的地址空间,不能访问别的进程的地址空间。
2、进程是线程的容器,windows调度运行的单位是线程,一个进程至少有1个主线程。一个进程内所有的线程都处于同一个虚拟地址空间。
3、进程初始化的时候,系统会在进程的地址空间中创建一个堆,叫进程默认堆。进程中所有的线程共用这一个堆。当然,可以增加1个或几个堆,给不同的线程共同使用或单独使用。
4、创建线程的时候,系统会在进程的地址空间中分配1块内存给线程栈,通常是1MB。线程栈是独立的,不共享。

搞清进程和线程、堆和栈的区别,再结合上面4点说明,你的5个问题很容易理解了。
[解决办法]
1 从创建一个新的进程开始,子进程就会复制一份父进程的内存空间过来的,
  如果是创建线程的话,就是共用了!没有拷贝!
  注意进程,和线程完全不同的概念!
2 栈空间都是自己分配管理的吧,没有什么联系!

看看进程和线程吧,然后看看内存管理!
感觉你是想说父子进程的关系,但是你又是说线程!

[解决办法]
这对不同操作系统是不同的,windows是基于线程的,进程不过是个线程和其他资源的容器,系统的调度单位是线程,每个线程有独立的栈!
对进程来说
堆属于进程这个容器!
栈属于线程这个运行单元

linux等系统,进程是系统的调度单元,线程不过是轻量级的进程而已!

[解决办法]
WinDbg

  
 Debugging Tools for Windows 
k, kb, kc, kd, kp, kP, kv (Display Stack Backtrace)
The k* commands display the stack frame of the given thread, together with related information..

Syntax
User-Mode

[~Thread] k[b
[解决办法]
p
[解决办法]
P
------解决方案--------------------


v] [c] [n] [f] [L] [FrameCount] 
[~Thread] k[b
[解决办法]
p
[解决办法]
P
[解决办法]
v] [c] [n] [f] [L] = BasePtr [FrameCount] 
[~Thread] k[b
[解决办法]
p
[解决办法]
P
[解决办法]
v] [c] [n] [f] [L] = BasePtr StackPtr InstructionPtr 
[~Thread] kd [WordCount] 



Kernel-Mode

[Processor] k[b
[解决办法]
p
[解决办法]
P
[解决办法]
v] [c] [n] [f] [L] [FrameCount] 
[Processor] k[b
[解决办法]
p
[解决办法]
P
[解决办法]
v] [c] [n] [f] [L] = BasePtr [FrameCount] 
[Processor] k[b
[解决办法]
p
[解决办法]
P
[解决办法]
v] [c] [n] [f] [L] = BasePtr StackPtr InstructionPtr 
[Processor] kd [WordCount] 





Parameters
Thread 
Specifies the thread whose stack is to be displayed. If you omit this parameter, the stack of the current thread is displayed. For more information about thread syntax, see Thread Syntax. You can specify threads only in user mode. 
Processor 
Specifies the processor whose stack is to be displayed. For more information about processor syntax, see Multiprocessor Syntax. You can specify processors only in kernel mode. 



Displays the first three parameters that are passed to each function in the stack trace. 

Displays a clean stack trace. Each display line includes only the module name and the function name. 

Displays all of the parameters for each function that is called in the stack trace. The parameter list includes each parameter's data type, name, and value. The p option is case sensitive. This parameter requires full symbol information. 

Displays all of the parameters for each function that is called in the stack trace, like the p parameter. However, for P, the function parameters are printed on a second line of the display, instead of on the same line as the rest of the data. 

Displays frame pointer omission (FPO) information. On x86-based processors, the display also includes calling convention information. 

Displays frame numbers. 

Displays the distance between adjacent frames. This distance is the number of bytes that separate the frames on the actual stack. 

Hides source lines in the display. L is case sensitive. 
FrameCount 
Specifies the number of stack frames to display. You should specify this number in hexadecimal format, unless you have changed the radix by using the n (Set Number Base) command. The default value is 20 (0x14), unless you have changed the default value by using the .kframes (Set Stack Length) command. 
BasePtr 
Specifies the base pointer for the stack trace. The BasePtr parameter is available only if there is an equal sign (=) after the command. On an x86-based processor, you can add one more parameter after BasePtr (which is interpreted as the FrameCount parameter) or two more parameters after BasePtr (which are interpreted as the StackPtr and InstructionPtr parameters). 


StackPtr 
(x86-based processor only) Specifies the stack pointer for the stack trace. If you omit StackPtr and InstructionPtr, the command uses the stack pointer that the esp register specifies and the instruction pointer that the eip register specifies. 
InstructionPtr 
(x86-based processor only) Specifies the instruction pointer for the stack trace. If you omit StackPtr and InstructionPtr, the command uses the stack pointer that the esp register specifies and the instruction pointer that the eip register specifies. 
WordCount 
Specifies the number of DWORD_PTR values in the stack to dump. The default value is 20 (0x14), unless you changed the default value by using the .kframes (Set Stack Length) command.

Environment
Modes User mode, kernel mode 
Targets Live, crash dump 
Platforms All  


Comments
When you issue the k, kb, kp, kP, or kv command, a stack trace is displayed in a tabular format. If line loading is enabled, source modules and line numbers are also displayed.

The stack trace includes the base pointer for the stack frame, the return address, and function names.

If you use the kp or kP command, the full parameters for each function that is called in the stack trace are displayed. The parameter list includes each parameter's data type, name, and value. 

This command might be slow. For example, when MyFunction1 calls MyFunction2, the debugger must have full symbol information for MyFunction1 to display the parameters that are passed in this call. This command does not fully display internal Microsoft Windows routines that are not exposed in public symbols. 

If you use the kb or kv command, the first three parameters that are passed to each function are displayed. If you use the kv command, FPO data is also displayed.



On an x86-based processor, the kv command also displays calling convention information.

On an Itanium-based processor, the kv command also causes nonvolatile registers to be displayed. This information enables you to trace the register stack.


When you use the kv command, the FPO information is added at the end of the line in the following format.

FPO text Meaning 
FPO: [non-Fpo]
 No FPO data for the frame. 
FPO: [N1,N2,N3]
 N1 is the total number of parameters. 

N2 is the number of DWORD values for the local variables. 

N3 is the number of registers that are saved.

 
FPO: [N1,N2] TrapFrame @ Address
 N1 is the total number of parameters. 

N2 is the number of DWORD values for the locals. 

Address is the address of the trap frame.

 
FPO: TaskGate Segment:0
 Segment is the segment selector for the task gate. 
FPO: [EBP 0xBase]
 Base is the base pointer for the frame. 


The kd command displays the raw stack data. Each DWORD value is displayed on a separate line. Symbol information is displayed for those lines together with associated symbols. This format creates a more detailed list than the other k* commands. The kd command is equivalent to a dds (Display Memory) command that uses the stack address as its parameter.

If you want a stack trace that begins somewhere other than the current stack location, you can use the BasePtr parameter to specify the base pointer value. If you are specifying the base pointer value on an x86-based processor, you should specify BasePtr, StackPtr, and InstructionPtr. These parameters should be the values of ebp, esp, and eip that correspond to the stack trace that you want. If you specify BasePtr and omit StackPtr and InstructionPtr, you might receive incorrect results if there are FPO frames present.



If you use the k command at the beginning of a function (before the function prolog has been executed), you receive incorrect results. The debugger uses the frame register to compute the current backtrace, and this register is not set correctly for a function until its prolog has been executed.

In user mode, the stack trace is based on the stack of the current thread. For more information about threads, see Controlling Processes and Threads.

In kernel mode, the stack trace is based on the current register context. You can set the register context to match a specific thread, context record, or trap frame. 

Additional Information
For more information about stack traces and other ways to display stack traces, see Viewing the Call Stack. For more information about the register context and other context settings, see Changing Contexts.



? 2009 Microsoft Corporation
Send feedback on this topic
Debugging Tools for Windows
January 17, 2009

Build machine: CAPEBUILD
Additional Information
For more information about stack traces and other ways to display stack traces, see Viewing the Call Stack. For more information about the register context and other context settings, see Changing Contexts.

热点排行