| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Rahul" |
| Date: |
01 Jan 2008 10:52:10 AM |
| Object: |
task sharing global static variables |
Hi,
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++?
.
|
|
| User: "Victor Bazarov" |
|
| Title: Re: task sharing global static variables |
01 Jan 2008 11:02:12 AM |
|
|
Rahul wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
.
|
|
|
| User: "Rahul" |
|
| Title: Re: task sharing global static variables |
01 Jan 2008 12:42:37 PM |
|
|
On Jan 1, 10:02 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
Rahul wrote:
I was looking at the link which says the following,
http://www.netrino.com/Embedded-Systems/How-To/RTOS-Preemption-Multit...
"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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
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?
.
|
|
|
| User: "James Kanze" |
|
| Title: Re: task sharing global static variables |
03 Jan 2008 03:06:28 AM |
|
|
On Jan 1, 7:42 pm, Rahul <sam_...@yahoo.co.in> wrote:
[...]
Ok and what if two instance of the same program is executed?
int i =3D 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.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
.
|
|
|
|
| User: "Jim Langston" |
|
| Title: Re: task sharing global static variables |
02 Jan 2008 10:22:14 AM |
|
|
Rahul wrote:
On Jan 1, 10:02 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
Rahul wrote:
I was looking at the link which says the following,
http://www.netrino.com/Embedded-Systems/How-To/RTOS-Preemption-Multit...
"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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
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.
--
Jim Langston
tazmaster@rocketmail.com
.
|
|
|
|
| User: "Victor Bazarov" |
|
| Title: Re: task sharing global static variables |
01 Jan 2008 09:26:01 PM |
|
|
Rahul wrote:
On Jan 1, 10:02 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
Rahul wrote:
I was looking at the link which says the following,
http://www.netrino.com/Embedded-Systems/How-To/RTOS-Preemption-Multit...
"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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
.
|
|
|
|
|
|

|
Related Articles |
|
|