What resources are shared between threads?
Recently, I have been asked a question in an interview what's the difference between a process and a thread. Really, I did not know the answer. I thought for a minute and gave a very weird answer.
Threads share the same memory, processes do not. After answering this, the interviewer gave me an evil smile and fired the following questions at me:
Q. Do you know the segments in which a program gets divided?
My answer: yep (thought it was an easy one) Stack, Data, Code, Heap
Q. So, tell me: which segments do threads share?
I could not answer this and ended up in saying all of them.
Please, can anybody present the correct and impressive answers for the difference between a process and a thread?
You're pretty much correct, but threads share all segments except the stack. Threads have independent call stacks, however the memory in other thread stacks is still accessible and in theory you could hold a pointer to memory in some other thread's local stack frame (though you probably should find a better place to put that memory!).
From Wikipedia (I think that would make a really good answer for the interviewer :P)
Threads differ from traditional multitasking operating system processes in that:
Something that really needs to be pointed out is that there are really two aspects to this question - the theoretical aspect and the implementations aspect.
First, let's look at the theoretical aspect. You need to understand what a process is conceptually to understand the difference between a process and a thread and what's shared between them.
We have the following from section 2.2.2 The Classical Thread Model in Modern Operating Systems 3e by Tanenbaum:
The process model is based on two independent concepts: resource grouping and execution. Sometimes it is useful to separate them; this is where threads come in....
He continues:
One way of looking at a process is that it is a way to group related resources together. A process has an address space containing program text and data, as well as other resources. These resource may include open files, child processes, pending alarms, signal handlers, accounting information, and more. By putting them together in the form of a process, they can be managed more easily. The other concept a process has is a thread of execution, usually shortened to just thread. The thread has a program counter that keeps track of which instruction to execute next. It has registers, which hold its current working variables. It has a stack, which contains the execution history, with one frame for each procedure called but not yet returned from. Although a thread must execute in some process, the thread and its process are different concepts and can be treated separately. Processes are used to group resources together; threads are the entities scheduled for execution on the CPU.
Further down he provides the following table:
Per process items | Per thread items
------------------------------|-----------------
Address space | Program counter
Global variables | Registers
Open files | Stack
Child processes | State
Pending alarms |
Signals and signal handlers |
Accounting information |
The above is what you need for threads to work. As others have pointed out, things like segments are OS dependant implementation details.
链接地址: http://www.djcxy.com/p/55256.html上一篇: 线和光纤有什么区别?
下一篇: 线程之间共享什么资源?