473,396 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Shared Data Problem

Apologies for the last post; got the topic wrong.

What is the current thinking on the best way to solve the shared data
problem i.e. accesssing a shared variable in a non atomic way can give
erroneous results if an interrupt service routine that also changes it
occurs part way through the access.
--
Ian Bell
Nov 14 '05 #1
11 2546
Ian Bell <ru*********@yahoo.com> scribbled the following:
Apologies for the last post; got the topic wrong. What is the current thinking on the best way to solve the shared data
problem i.e. accesssing a shared variable in a non atomic way can give
erroneous results if an interrupt service routine that also changes it
occurs part way through the access.


The C standard doesn't specify anything about shared data or interrupt
services either. As far as the C standard is concerned, a C program runs
in completer isolation from the rest of the world. Please ask in a
newsgroup about your own implementation.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"I wish someone we knew would die so we could leave them flowers."
- A 6-year-old girl, upon seeing flowers in a cemetery
Nov 14 '05 #2
On Sat, 19 Mar 2005 17:31:58 +0000, Ian Bell
<ru*********@yahoo.com> wrote:
Apologies for the last post; got the topic wrong.

What is the current thinking on the best way to solve the shared data
problem i.e. accesssing a shared variable in a non atomic way can give
erroneous results if an interrupt service routine that also changes it
occurs part way through the access.


Standard C doesn't know anything about interrupt routines either, but
you're getting closer because it does know anout signals, which may be
asynchronous.

I suspect what you are looking for is the 'volatile' keyword, that says
that the variable must be re-read every time it is accessed in case it
has changed since the last time. However, the only type which is
guaranteed to be truly 'atomic' in C (in the sense that no other code
can access it in the middle of another access) is char, which is defined
as the smallest addressable element. In practice, most platforms will
probably treat int as atomic, but you really need to look at your
specific hardware.

For anything else, use a volatile char as a lock and use volatile for
any other shared area to disable optimisations based on prior values.
That will slow it down, of course, so keep such accesses to a minimum if
that's a concern.

Or, in the extreme, use system-specific constructs to disable interrupts
(the specific interrupt if you can) round the accesses. You may be able
to do that with inline assembler, for instance (but wrap it in macros in
a header file so it can be replaced for porting).

Chris C
Nov 14 '05 #3
Joona I Palaste wrote:

The C standard doesn't specify anything about shared data or interrupt
services either. As far as the C standard is concerned, a C program runs
in completer isolation from the rest of the world. Please ask in a
newsgroup about your own implementation.


Is this a newsgroup about C or the C standard?? The standard does not
mention applications but they are discussed here all the time.

Ian

--
Ian Bell
Nov 14 '05 #4
Ian Bell wrote:
.... snip ...
Is this a newsgroup about C or the C standard?? The standard does
not mention applications but they are discussed here all the time.


No, we don't need another long discourse on what the subject of the
group really is. Just read the references below.

--
Some useful references:
<http://www.ungerhu.com/jxh/clc.welcome.txt>
<http://www.eskimo.com/~scs/C-faq/top.html>
<http://benpfaff.org/writings/clc/off-topic.html>
<http://anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/> (C99)
<http://www.dinkumware.com/refxc.html> C-library

Nov 14 '05 #5
"Ian Bell" <ru*********@yahoo.com> wrote in message
Apologies for the last post; got the topic wrong.

What is the current thinking on the best way to solve the shared data
problem i.e. accesssing a shared variable in a non atomic way can give
erroneous results if an interrupt service routine that also changes it
occurs part way through the access.


Objects you can access in an atomic way, must have type

sig_atomic_t

Even in case of asynchronous interrupts, the standard guarantee atomic
access, if the object is declared with the volatile qualifier:

volatile .sig_atomic_t shared_data;

Not much else you can safely do in a signal handler...

--
Tor <torust AT online DOT no>

Nov 14 '05 #6
"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message

<snip>
The C standard doesn't specify anything about shared data or interrupt
services either. As far as the C standard is concerned, a C program
runs in completer isolation from the rest of the world.


The signal() function is there to let us handle interrupts and
exceptions. Interrupts can viewed as as asynchronous events,
while exceptions can be viewed as synchronous events.

Floating-point exceptions (SIGFPE) is an example of synchronous
events. SIGINT is asynchronous event, that can be generated with
an interactive action e.g. by pressing Ctrl-C.

Hence, a C program doesn't run that isolated, SIGTERM may
come from a user or another program...

--
Tor <torust AT online DOT no>
"To this day, many C programmers believe that 'strong typing' just means
pounding extra hard on the keyboard". PvdL
Nov 14 '05 #7
In article <__******************@news4.e.nsc.no>,
Tor Rustad <to****@online.no.spam> wrote:
:The signal() function is there to let us handle interrupts and
:exceptions. Interrupts can viewed as as asynchronous events,
:while exceptions can be viewed as synchronous events.

:Floating-point exceptions (SIGFPE) is an example of synchronous
:events.

Is there an expressible upper-bound on the reception of a SIGFPE?
Same statement? Same block? Same routine?

Is there a way of expressing that "This section of code might
generate a SIGFPE signal, and if it does then use this signal
handler, but if the signal hasn't been generated by the time
the last instruction of the code has had its results 'fully
graduated', then ignore that handler" ?

Is there a way of expressing 'fully graduated' in C?
There are "sequence points", but the "as if" qualifiers
with respect to sequence points deliberately leave a lot
of leeway so that optimization can be use -- is there a way
to be -sure- that a particular computation has fully finished
in all math co-processors and internal CPU conditional computation
registers... or is assigning to a volatile variable the closest
such mechanism?

I'm just not sure that C has any provision for "synchronous events"
in any meaningful way.
--
Studies show that the average reader ignores 106% of all statistics
they see in .signatures.
Nov 14 '05 #8
In article <d1**********@canopus.cc.umanitoba.ca>
Walter Roberson <ro******@ibd.nrc-cnrc.gc.ca> wrote:
... Is there a way of expressing that "This section of code might
generate a SIGFPE signal, and if it does then use this signal
handler, but if the signal hasn't been generated by the time
the last instruction of the code has had its results 'fully
graduated', then ignore that handler" ?
No.
Is there a way of expressing 'fully graduated' in C?
There are "sequence points", but the "as if" qualifiers
with respect to sequence points deliberately leave a lot
of leeway so that optimization can be use -- is there a way
to be -sure- that a particular computation has fully finished
in all math co-processors and internal CPU conditional computation
registers... or is assigning to a volatile variable the closest
such mechanism?
Volatile is all you get, and it is not sufficient (as you observe).

In general, when attacking this problem, people just invent quickie
ad-hoc solutions, such as using inline assembly or a call to a short
assembly-language routine. Obviously these are machine- and even
compiler-dependent.
I'm just not sure that C has any provision for "synchronous events"
in any meaningful way.


Indeed. The situation is even worse when working with real shared
memory on real systems that share memory. In particular, one has
to choose a memory model, and on CPUs with instruction reordering,
synchronization primitives. In general, systems that offer
thread-handling routines (such as POSIX threads) will take care of
all the synchronization required at thread-mutex points, so that
the threads will work on multi-CPU boxes. I imagine there are
plenty of exceptions to this rule.

(There is a whole newsgroup, comp.programming.threads, for
thread-related discussion. POSIX particulars belong in
comp.unix.programmer -- a newsgroup whose "improper" hierarchical
name is a historical artifact, something like the way "comp.lang.c"
really means "comp.lang.iso-standard-c". :-) )
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #9
In article <sl******************@ccserver.keris.net>
Chris Croughton <ch***@keristor.net> wrote:
I suspect what you are looking for is the 'volatile' keyword, that says
that the variable must be re-read every time it is accessed in case it
has changed since the last time. However, the only type which is
guaranteed to be truly 'atomic' in C (in the sense that no other code
can access it in the middle of another access) is char, which is defined
as the smallest addressable element. ...
Actually, you need "volatile sig_atomic_t" in signal handlers. It
is so restricted as to be worthless for most "real work" though.
(You *can* use it, and a "real" solution to a real problem might
well incorporate "volatile sig_atomic_t", but that solution will
probably depend on things not guaranteed by the ISO C standards.)
For anything else, use a volatile char as a lock and use volatile for
any other shared area to disable optimisations based on prior values.


Do not do this: it will fail on some machines (e.g., SPARC, some
MIPS, some Alpha).

(One can only get atomic byte-wide access on SPARCs using the ldstub
instruction, which C compilers do not generally generate, not even
for "volatile char". Mutexes will generally be implemented with
the the V9-specific "cas" -- compare-and-swap -- instruction, which
operates on a 32-bit word.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #10
"Walter Roberson" <ro******@ibd.nrc-cnrc.gc.ca> wrote in message
news:d1**********@canopus.cc.umanitoba.ca...
In article <__******************@news4.e.nsc.no>,
Tor Rustad <to****@online.no.spam> wrote:
:The signal() function is there to let us handle interrupts and
:exceptions. Interrupts can viewed as as asynchronous events,
:while exceptions can be viewed as synchronous events.

:Floating-point exceptions (SIGFPE) is an example of synchronous
:events.

Is there an expressible upper-bound on the reception of a SIGFPE?
Same statement? Same block? Same routine?


No, from the C standard point of view, a program where a
computation raises a signal, has fallen into undefined behavior:

[#6.5]

"If an /exception/ occurs during the evaluation of an expression (that
is, if the result is not mathematically defined or not in the range of
representable values for its type), the behavior is undefined."
However, IF the abstract C machine is running on a CPU with
HW floating-point support, then I expect that the CPU
automatically suspend executing the current task (in the case
of a floating-point exception), until the exception handler
has finished...

--
Tor <torust AT online DOT no>

Nov 14 '05 #11
"Ian Bell" <ru*********@yahoo.com> wrote in message

Is this a newsgroup about C or the C standard?? The standard
does not mention applications but they are discussed here all
the time.


C code can easily be written in a non-portable way, but
c.l.c. on-topic discussions, are about portable C code.

However, given an _interesting_ question, many regulars
will respond anyway... the trick is to start with an on-topic
question and afterwards pull in the off-topic stuff! ;-)

--
Tor <torust AT online DOT no>

Nov 14 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Simon | last post by:
Hello. I wanna use my dynamic shared library(built with C language) with php script. (PHP version is 4.0.6) So I making a shared library(*.so) in Solaris environment(2.7) and it is successful....
11
by: Mike MacSween | last post by:
My client has an MS Access database application on her local machine. I have full access to that in terms of changing the design. I've got a simple PHP/MySql application on shared hosting, so no...
5
by: Claudio Grondi | last post by:
Background information: --------------------------------- in order to monitor mainboard sensory data as fan speeds, temperatures, applications like SpeedFan http://www.almico.com/speedfan.php or...
6
by: Paul | last post by:
Hi. Just trying to find out the best approach as I beleive it might give me problems later on down the road. I have an ASP.NET application which references a shared database class which...
10
by: John Brock | last post by:
I have a base class with several derived classes (I'm writing in VB.NET). I want each derived class to have a unique class ID (a String), and I want the derived classes to inherit from the base...
6
by: Daniel Fernandes | last post by:
Hi there Is there any difference I need to be aware when I create a class with only shared members & procedures when compared to a module (which is a shared class) ? I am asking this because...
5
by: Simon | last post by:
Hi all, We have an ASP.NET 1.1 application running on IIS6 on Server 2003. Most of the base objects we are using in this application are taken from a windows application also written by us. We...
7
by: akennis | last post by:
First of all, sorry for duplicating this post. I put it up in the alt.comp.lang.learn.c-c++ mistakenly. I'm investigating a problem whereby exceptions thrown from functions in a Shared Library...
10
by: tshad | last post by:
I have a Dll I created in VS 2000. The namespace is MyFunctions and the Class is CryptoUtil. I have a program that is using the Class but it can't access it directly. I have a class (below)...
11
by: eBob.com | last post by:
I have this nasty problem with Shared methods and what I think of as "global storage" - i.e. storage declared outside of any subroutines or functions. In the simple example below this "global"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.