task sharing global static variables

V

Victor Bazarov

Rahul said:
I was looking at the link which says the following,

http://www.netrino.com/Embedded-Systems/How-To/RTOS-Preemption-Multitasking

"When tasks share resources such as global variables, data structures,
or peripheral control and status registers, an operating system
primitive called a mutex must be used to prevent race conditions"

And i was wondering how a global variable could be shared by two
independent tasks developed in c++?

From the language point of view, there is no answer to your question
because the term "task" is not defined. However, from my experience,
a variable that resides in computer memory _can be_ just another
resource (like a device or a file), which can be shared by processes
on an operating system capable of running more than one process at
a time. If the OS/architecture supports memory addressing that does
not involve virtual space (like MS-DOS for example), then all memory
is addressible by any process (modulo some protection measures if
they exist).

The reason to pick a static/global variable for that is simple: its
address is predefined at the process start and does not change, and
its lifetime is the duration of the process. For comparison, any
automatic variable can have different place in computer memory every
time it's constructed, and the lifetime of an automatic variable is
limited to the block in which it's declared.

V
 
R

Rahul

From the language point of view, there is no answer to your question
because the term "task" is not defined. However, from my experience,
a variable that resides in computer memory _can be_ just another
resource (like a device or a file), which can be shared by processes
on an operating system capable of running more than one process at
a time. If the OS/architecture supports memory addressing that does
not involve virtual space (like MS-DOS for example), then all memory
is addressible by any process (modulo some protection measures if
they exist).

The reason to pick a static/global variable for that is simple: its
address is predefined at the process start and does not change, and
its lifetime is the duration of the process. For comparison, any
automatic variable can have different place in computer memory every
time it's constructed, and the lifetime of an automatic variable is
limited to the block in which it's declared.

V

Ok and what if two instance of the same program is executed?

int i = 0;

int main()
{
...
...
...
}

there would be two instance of the global variables right? each in the
separate process's data segment?
 
V

Victor Bazarov

Rahul said:
Ok and what if two instance of the same program is executed?

int i = 0;

int main()
{
...
...
...
}

there would be two instance of the global variables right? each in the
separate process's data segment?

Yes, but all those terms ("date segment", "two instance of the same
program") are outside of the C++ language terminology. It is of
course possible to make processes share segments, share objects,
whether using pointers or using some kind of dynamic name/address
binding provided by the OS.

V
 
J

Jim Langston

Rahul said:
Ok and what if two instance of the same program is executed?

int i = 0;

int main()
{
...
...
...
}

there would be two instance of the global variables right? each in the
separate process's data segment?

This is better asked in a threading newsgroup, such as
comp.programming.threads, but look at named mutexes.
 
J

James Kanze

[...]
Ok and what if two instance of the same program is executed?
int i = 0;
int main()
{
...
...
...
}
there would be two instance of the global variables right? each in the
separate process's data segment?

Usually, on most general purpose systems today, but it's really
implementation defined. I think Victor has already mentionned
cases where the OS doesn't support separate data segments per
process (e.g. MS-DOS). Of course, in general, such systems
don't allow executing two programs (or two instances of the same
program) at the same time, but I've used some that did.

A lot of linkers (especially linkers for embedded systems) also
have options to force specific symbols to specific addresses.
And in the past (before virtual memory and MMU's), a lot of
linkers would put something like i at a fixed address. If
you're code is running without memory mapping, two processes
which access the same address will access the same physical
memory.

But unless you're doing really low level coding or working with
very simple embedded processors, you can pretty much forget the
issue, and consider that your program is running in a world of
its own. (Unless, of course, you explicitly request otherwise
by explicitly requesting a shared resource from the system.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,016
Latest member
TatianaCha

Latest Threads

Top