Are streams associated with local variables?

I read the concept of the Silberschatz 7th ed operating system, and he says that threads from the same process share a section of code, a section of data, and other OS resources, but have separate sets of stacks and registers. However, the problem is that I am working on the fact that threads share local variables, but are not local variables stored on the stack, so should separate threads have their own copies?

+8
source share
5 answers

Threads usually use the following.

  • Data segment (global variables, static data)
  • Address space
  • Code segment.
  • I / O, if the file is open, all streams can read / write to it.
  • Parent process id.
  • A bunch

But Threads keep their own copy of the stack , and local variables are stored on the stack, so you are right that each thread must have its own copy of the local variables.

Maybe his bad terminology is used, or maybe its something specific to a set of problems.

+14
source

I read that one process can have multiple threads. Several threads of the same process really share things. If you want to know what they share and what not. Consideration of the process consists of address space, stack, heap, global variables, code, data, OS resources, what is shared among them by threads? I have the following assumptions:

Global variables . I read the global thread stream variable. Also, during programming in Java and C #, I created threads for class sharing level variables. Therefore, I believe that threads are shared by global variables (although I’m not sure whether concepts are at a high level. Programming languages ​​translate as low level operating system facts).

Heap . Because the global variable is stored on the heap, the heap is shared between threads.

Stack . Since each thread can have its own execution sequence / code, it must have its own stack on which it could push / pop its contents of the program counter (when they say function calls and it happens sometimes). Thus, threads of one process do not share the stack. Now I'm not sure about sharing the following things

Address space Not sure what exactly counts in the address space. But, I think, address space is usually used in the context of processes, not threads. And since all the threads of one process are in the same address space as the parent process, it is said that Blockquote threads share the address space. (But then they support different stacks inside the same address space?)

OS resources . I think this can be very implementation specific. For example, a parent process may selectively return a handle to the same file for some of its threads, and not for all. Or am I mistaken and OS resources mean something other than files?

Code Themes may have different code, so code exchange is not always performed.

Data - not sure what to consider with data. But make sure that global variables are shared between threads. And I'm sure that local variables are not divided equally. In general, I am very confused because of vague terms, super-generalizations made in the Operating System books are presented and the details of the implementation online. Therefore, I am trying to find an answer that can satisfy me.

, so I came to the conclusion that threads support their own stack, and local variables are stored on the stack, so it would be impossible to use local variables in threads.

+1
source

Threads within a process use the same address space.

"variable" is the concept of a programming language. Variables disappear when the source code passes through the compiler (some can be reduced to characters).

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

The ease with which this is done depends on the programming language and basic linker support. There are very few programming languages ​​that support real streaming support (for example, Ada-called text). Ada has explicit mechanisms that allow threads to exchange data using variables.

So then:

I am reading the operating system concept from Silberschatz 7th ed,

This is the beginning of your problem.

he says that threads of the same process share a section of code, a section of data, and other OS resources,

There are all system concepts. Many systems do not have a “Code Section” or “Data” section. There is simply memory with certain attributes (for example, read only, read / write, read / execute).

but have separate sets of stacks and registers.

Registers are a system resource that is allocated when scheduling a stream. A thread does not have its own set of registers. Each thread has its own set of register values, which are loaded when the thread is active and saved when it becomes inactive.

Stacks are just read / write memory blocks.

However, the problem is that I am working on states that threads share local variables, but are not local variables stored on the stack, so should separate threads have their own copies?

Again, threads share memory. They use "local" variables only if the programming language used supports such an exchange, or such separation occurs through an "accident."

Mapping variables to use a memory allocation type is a compiler function. FORTRAN 66/77 and COBOL usually did not allocate any variables on the stack

0
source

POSIX-compatible OS threads must share local variables (aka automatic variables). From XBD Volume, Basic Definitions, Chapter 3, Definitions, Record 3.404, Topic Open basic specifications of the Issue 7 group (at the time of response):

Everything whose address can be determined by the stream, including, but not limited to, static variables, storage obtained through malloc (), directly addressed storage, obtained using functions defined for implementation, and automatic variables, is available for all flows in the same volume same process.

The following code with output 10 should be sufficient to illustrate my statement, assuming that the 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 tracks thread control is one that is not shared between process threads. However, this point is implementation dependent, as none of the above mentioned standards speak of this.

0
source

I will try to explain in simple words. Remember three things

stack

Heap Variable - GLOBAL

code segment - code and some static variables

Now i'm going to stack

Each thread has its own stack and the thread frees its stack after execution. And the stack is private to the thread, because no one has access to it except its owner thread.

Thus, local variables on and on the stack of the main thread and another thread will have their own stack, so it is not possible for threads to share local variables.

Hope it helps.

0
source

Source: https://habr.com/ru/post/1262732/


All Articles