views:

807

answers:

7
+14  Q: 

Process vs Thread

Recently I have been asked question in the interview whats the difference between process and thread. Really I did not know answer. I thought for a minute and gave very weird answer.

Threads share same memory.. processes do not. After answering this interviewer gave a evil smile and fired following questions at me:

Q. Do you know segments in which program gets divided?

my answer: yep(thought it was easy one) Stack, Data, Code, Heap

Q. So tell me which segment thread share?

I could not answer this ended up in saying all of them.

please can any body have correct and impressive answers for difference between process and thread

+2  A: 

Threads share the code and data segments and the heap, but they don't share the stack.

Kevin Peterson
+10  A: 

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!).

Greg Hewgill
+1  A: 

Thread share the heap (there is a research about thread specific heap) but current implementation share the heap. (and of course the code)

Dani
+2  A: 

Threads share data and code while processes do not. The stack is not shared for both.

Processes can also share memory, more precisely code, for example after a Fork(), but this is an implementation detail and (operating system) optimization. Code shared by multiple processes will (hopefully) become duplicated on the first write to the code - this is known as copy-on-write. I am not sure about the exact semantics for the code of threads, but I assume shared code.

           Process   Thread

   Stack   private   private
   Data    private   shared
   Code    private1  shared2

1 The code is logically private but might be shared for performance reasons. 2 I am not 100% sure.

Daniel Brückner
I'd say code segment (text segment), unlike data, is almost always readonly on most architectures.
Jorge Córdoba
+4  A: 

From the wikipedia (I think that would make a really good answer for the interviewer :P)

Threads differ from traditional multitasking operating system processes in that:

  • processes are typically independent, while threads exist as subsets of a process
  • processes carry considerable state information, whereas multiple threads within a process share state as well as memory and other resources
  • processes have separate address spaces, whereas threads share their address space
  • processes interact only through system-provided inter-process communication mechanisms.
  • Context switching between threads in the same process is typically faster than context switching between processes.
Jorge Córdoba
+1  A: 

Threads share everything [1]. There is one address space for the whole process.

Each thread has its own stack and registers, but all threads' stacks are visible in the shared address space.

If one thread allocates some object on its stack, and sends the address to another thread, they'll both have equal access to that object.


Actually, I just noticed a broader issue: I think you're confusing two uses of the word segment.

The file format for an executable (eg, ELF) has distinct sections in it, which may be referred to as segments, containing compiled code (text), initialized data, linker symbols, debug info, etc. There are no heap or stack segments here, since those are runtime-only constructs.

These binary file segments may be mapped into the process address space seperately, with different permissions (eg, read-only executable for code/text, and copy-on-write non-executable for initialized data).

Areas of this address space are used for different purposes, like heap allocation and thread stacks, by convention (enforced by your language runtime libraries). It is all just memory though, and probably not segmented unless you're running in virtual 8086 mode. Each thread's stack is a chunk of memory allocated at thread creation time, with the current stack top address stored in a stack pointer register, and each thread keeps its own stack pointer along with its other registers.


[1] OK, I know: signal masks, TSS/TSD etc. The address space, including all its mapped program segments, are still shared though.

Useless
+9  A: 

Tell the interviewer that it depends entirely on the implementation of the OS.

Take Windows x86 for example. There are only 2 segments [1], Code and Data. And they're both mapped to the whole 2GB (linear, user) address space. Base=0, Limit=2GB. They would've made one but x86 doesn't allow a segment to be both Read/Write and Execute. So they made two, and set CS to point to the code descriptor, and the rest (DS, ES, SS, etc) to point to the other [2]. But both point to the same stuff!

The person interviewing you had made a hidden assumption that he/she did not state, and that is a stupid trick to pull.

So regarding

Q. So tell me which segment thread share?

The segments are irrelevant to the question, at least on Windows. Threads share the whole address space. There is only 1 stack segment, SS, and it points to the exact same stuff that DS, ES, and CS do [2]. I.e. the whole bloody user space. 0-2GB. Of course, that doesn't mean threads only have 1 stack. Naturally each has its own stack, but x86 segments are not used for this purpose.

Maybe *nix does something different. Who knows. The premise the question was based on was broken.


  1. At least for user space.
  2. From ntsd notepad: cs=001b ss=0023 ds=0023 es=0023
Alex
Yep... Segments depends on the OS and the compiler/linker. Sometimes there is a separate BSS segment from the DATA segment. Sometimes there is RODATA (Data like constant strings that can be in pages marked Read Only). Some systems even break DATA into SMALL DATA (accessible from a base + 16-bit offset) and (FAR) DATA (32-bit offset required to access). It's also possible that there is an extra TLS DATA (Thread Local Store) Segment which is generated on a per-thread basis
Adisak
Ah, no! You are confusing segments with sections!Sections are how the linker divides the module into parts (data, rdata, text, bss, etc..) as you described. But I'm talking about segments, as specified in intel/amd x86 hardware. Not related at all to compilers/linkers.Hope that makes sense.
Alex