Discussion:
processes have memory divided
(too old to reply)
G G
2019-07-15 04:50:16 UTC
Permalink
i'm reading operating systems design and implementation 3rd edition
minix 3.

processes, minix, have memory divided into segment: text, data, and stack
text is the code
data are the variables
stack ?

my question is about the data segment and the stack segment

the data segment are the variables that are global, static in the
program?

the stack are variables automatic, local in a program?

no instructions are ever placed on the stack right? they are
all in the text segment.

question example: if a C program has 5 functions and one main(),
each function was two logical variables and one static variable, main() has two
logical variables and the program has one global variable

then the instructions of what to do are all in the text segment?

the data segment has the global variable, and 1 static variable
from each functions?

the stack grows and shrinks as functions are called and the logical
variables of each function is placed on the stack as function is called
and removed from the stack when the function finishes( exits or return)
control to the calling function?

once the program is compiled and running does the data stack grow any more?


does UNIX have more segments?
Helmut Waitzmann
2019-07-15 08:55:47 UTC
Permalink
Post by G G
i'm reading operating systems design and implementation 3rd edition
minix 3.
processes, minix, have memory divided into segment: text, data, and stack
text is the code
data are the variables
In the data segment, there are the global variables and maybe the
heap (variables get by the malloc() library call).
Post by G G
stack ?
In the stack segment, there are the call frames (i. e. the return
addresses, parameters, and automatic variables) of the invoked
functions or procedures.
Post by G G
my question is about the data segment and the stack segment
the data segment are the variables that are global, static in the
program?
Yes.
Post by G G
the stack are variables automatic, local in a program?
Yes.
Post by G G
no instructions are ever placed on the stack right? they are
all in the text segment.
In the prevailing number of cases, yes. (There might be some
exceptions, though, if it's necessary to create machine code at
runtime.)
Post by G G
question example: if a C program has 5 functions and one main(),
each function was two logical variables and one static variable,
main() has two logical variables and the program has one global
variable
then the instructions of what to do are all in the text segment?
Yes (with the exception above).
Post by G G
the data segment has the global variable, and 1 static variable
from each functions?
Yes.
Post by G G
the stack grows and shrinks as functions are called and the logical
variables of each function is placed on the stack as function is called
and removed from the stack when the function finishes( exits or return)
control to the calling function?
Yes.
Post by G G
once the program is compiled and running does the data stack grow any more?
It might grow (and shrink) by functions using the heap like
malloc(), calloc(), free(), and realloc().
Post by G G
does UNIX have more segments?
For example the mmap(), as well as the malloc(), calloc(), and
realloc() functions might create segments.
Keith Thompson
2019-07-15 20:20:05 UTC
Permalink
G G <***@gmail.com> writes:
[...]
question example: if a C program has 5 functions and one main(), each
function was two logical variables and one static variable, main() has
two logical variables and the program has one global variable
[...]

You used the phrase "logical variables" several times. I think
you meant "local variables".

More formally, in C or C++ objects with automatic storage duration
(local variables) have lifetimes that follow a stack-like pattern,
with newly allocated local variables being the first to be deallocated.

In C or C++, a variable can be local but not automatic if it's
defined with the "static" keyword. Static variables (either defined
inside a function with the "static" keyword or defined outside
any function, at file scope) are created on program startup, and
continue to exist until the program terminates.

Local variables are local to the smallest enclosing block, not to
a function (blocks can appear only with a function definition).
For example, in C:

void func(void) {
int outer_local = some_value;
static int this_is_static;
if (outer_local > 0) {
int inner_local = some_other_value;
}
}

The lifetime of outer_local ends when execution reaches the "}"
that ends the function. The lifetime of inner_local ends when
execution reaches the inner "}". That doesn't mean they have to
be physically allocated and deallocated at the beginning and end
of their lifetimes; a compiler can simplify things by allocating
both at once on entry to the function. The variable this_is_static
exists during the entire execution of the program.
--
Keith Thompson (The_Other_Keith) kst-***@mib.org <http://www.ghoti.net/~kst>
Will write code for food.
void Void(void) { Void(); } /* The recursive call of the void */
G G
2019-07-15 21:06:50 UTC
Permalink
good grief!

lol you are right. i did mean local.

oh, man i need sleep.

thanks everybody for understanding
Barry Margolin
2019-07-15 15:11:11 UTC
Permalink
Post by Keith Thompson
Local variables are local to the smallest enclosing block, not to
a function (blocks can appear only with a function definition).
void func(void) {
int outer_local = some_value;
static int this_is_static;
if (outer_local > 0) {
int inner_local = some_other_value;
}
}
The lifetime of outer_local ends when execution reaches the "}"
that ends the function. The lifetime of inner_local ends when
execution reaches the inner "}". That doesn't mean they have to
be physically allocated and deallocated at the beginning and end
of their lifetimes; a compiler can simplify things by allocating
both at once on entry to the function. The variable this_is_static
exists during the entire execution of the program.
The compiler can also use the same memory for local variables whose
lifetimes never overlap.

void func(unsigned int param) {
for (int i = 0; i < param; i++) {
if (i % 2 == 0) { // even
int local1 = some_function(i);
// code that uses local1
} else {
int local2 = some_other_function(i);
// code that uses local2
}
}
}

The compiler can allocate the stack space for local1 and local2 when the
function is entered, and it only needs to allocate space for one
integer, since it can use the same memory for both of them (they're like
Superman and Clark Kent -- they never appear side by side).
--
Barry Margolin, ***@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
m***@gmail.com
2020-09-21 11:42:55 UTC
Permalink
Post by G G
i'm reading operating systems design and implementation 3rd edition
minix 3.
processes, minix, have memory divided into segment: text, data, and stack
text is the code
data are the variables
stack ?
my question is about the data segment and the stack segment
the data segment are the variables that are global, static in the
program?
the stack are variables automatic, local in a program?
no instructions are ever placed on the stack right? they are
all in the text segment.
question example: if a C program has 5 functions and one main(),
each function was two logical variables and one static variable, main() has two
logical variables and the program has one global variable
then the instructions of what to do are all in the text segment?
the data segment has the global variable, and 1 static variable
from each functions?
the stack grows and shrinks as functions are called and the logical
variables of each function is placed on the stack as function is called
and removed from the stack when the function finishes( exits or return)
control to the calling function?
once the program is compiled and running does the data stack grow any more?
does UNIX have more segments?
Loading...