multithreadingoperating-system

Do threads share local variables?


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?


Solution

  • 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.
    6. The Heap

    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.

    Maybe it's a bad terminology used or maybe it's something specific to problem set.