问题描述:

I'm reading Operating System Concepts by Silberschatz 7th ed, and it says that threads of the same process share the code section, data section, and other O.S. resources, but have separate sets of stacks and registers. However, the problem set I'm working on states that threads share local variables, but aren't local variables stored on the stack, so individual threads should have their own copies?

网友答案:

Threads usually share the following.

  1. Data Segment(global variables,static data)
  2. Address space.
  3. Code Segment.
  4. I/O, if file is open , all threads can read/write to it.
  5. Process id of parent.

But threads maintain their own copy of stack,and local variables are stored on the stack so yeah you are right that each thread should have its own copy of local variables.

May be its a bad terminology used or may be its something specific to problem set.

网友答案:

I read that single process can have multiple threads. Multiple threads of same process does share things among them. If you want to know what they share and what not. Considering process is comprised of address space, stack, heap, global variables, code, data, OS resources, what among them is shared by threads? I have following guessings:

Global variables - I have read thread shares global variable. Also while programming in Java and C#, I have made threads to share class level variables. So I am believing that the threads share global variables (though not sure whether the concepts in high level programming languages translates as is to low operating system level facts).

Heap - Since global variable is stored in the heap, heap is shared among threads.

Stack - Since each thread can have its own execution sequence/code, it must have its own stack on which it might push/pop its program counter contents (when say function calls and returns happen). So threads of same process do not share stack. Now I am unsure about the sharing of following things

Address space - Not sure what exactly counts under address space. But I guess address space is generally used in the context of processes, not threads. And since all threads of same process reside in the same address space as the parent process, it is said that Blockquote threads share address space. (But then they maintain different stack inside same address space?)

OS resources - I guess this can be very implementation specific. For example, parent process can selective give handle of same file to some of its threads and not to all. Or I am mistaking and OS resources means something other than files?

Code - Threads can have different code, so sharing code is not always the case.

Data - Unsure about what to consider under data. But sure that global variables are shared among threads. And sure that local variables are not similarly shared. Overall I am considerably confused due to vague terms, super-generalizations done in the Operating Systems books and extra-implementation specific details provided online. So I am trying to find some answer that can satisfy me.

so i came to conclude that,threads maintain their own stack,and local variables are stored on the stack so it might impossible sharing of local variables in threads.

网友答案:

The threads within a process share the same address space.

A "variable" is a programming language concept. Variables disappear when the source code goes through the compiler (some may be reduced to symbols).

Threads can share absolutely all memory. One thread can access any memory location of another.

The ease in which this is done depends upon the programming language and the underlying linker support. There are a very few programming languages that have real thread support (e.g., Ada—called text). Ada has explicit mechanisms for allowing threads to share data using variables.

So then:

I'm reading Operating System Concepts by Silberschatz 7th ed,

That's the start of your problem.

it says that threads of the same process share the code section, data section, and other O.S. resources,

There are all system specific concepts. In many systems there is not A "code section" and A "data section." There is simply memory with specific attributes (e.g., read only, read/write, read/execute).

but have separate sets of stacks and registers.

Registers are a system resource that are allocated when the thread is scheduled. A thread does not have its own set of registers. A each thread has its own distinct set of register values that are loaded when the thread is active and saved when it becomes inactive.

Stacks are just blocks of read/write memory.

However, the problem set I'm working on states that threads share local variables, but aren't local variables stored on the stack, so individual threads should have their own copies?

Again, threads share memory. They share "local" variables only if the programming language used supports such sharing or such sharing occurs by "accident."

The mapping of variables to memory allocation type usage is a function of the compiler. FORTRAN 66/77 and COBOL normally did not allocate any variables on the stack

相关阅读:
Top