简体   繁体   中英

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 OS 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.
  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.

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

POSIX compliant OSs' threads have to share local variables (aka automatic variables). From Volume XBD, Base Definitions, Chapter 3, Definitions, Entry 3.404, Thread of the Open Group Base Specifications Issue 7 (at the time of answering):

Anything whose address may be determined by a thread, including but not limited to static variables, storage obtained via malloc(), directly addressable storage obtained through implementation-defined functions, and automatic variables, are accessible to all threads in the same process.

The following code, with output 10 , should suffice for exemplifying my claim, if one assumes that static variables are indeed shared between threads:

#include <pthread.h>
#include <unistd.h>
#include <stdio.h>

static int* shared_pointer = NULL;

void* alter_thread_entry(void* arg) {
    // The following variable will be reachable,
    // while it exists, by both the main and the alter thread.
    int local_variable = 10;
    shared_pointer = &local_variable;
    sleep(2);
    return 0;
}

int main() {
    pthread_t alter_thread;
    pthread_create(&alter_thread, NULL, alter_thread_entry, NULL);
    sleep(1);
    printf("%i", *shared_pointer);
    fflush(stdout);
    pthread_join(alter_thread, NULL);
}

The call stack that traces control execution of threads is what is not shared among threads of a process. However, this point is implementation-dependent, as nothing is established about it in the aforementioned standard.

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 (eg, 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 OS 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 (eg, 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

I will try to explain in simple words. Keep three things in mind

Stack

Heap --GLOBAL variable

code segment - code and some static variables

Now I'll come to stack

Every thread has his own stack and thread release his stack after execution. And stack is personal to thread mean no one access it other than its owner thread.

So local variables and in the stack of the main thread and another thread will have their own stack so it is not possible that threads can share local variables.

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM