Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 12 Jan 2000 04:51:21 +0200
From:      Giorgos Keramidas <charon@hades.hell.gr>
To:        Laurence Berland <stuyman@confusion.net>
Cc:        freebsd-questions@FreeBSD.ORG
Subject:   Volatile variables (was: Giving a sighandler more information)
Message-ID:  <20000112045121.A74078@hades.hell.gr>
In-Reply-To: <387BC2C7.44B12247@confusion.net>
References:  <200001111215.NAA22385@dorifer.heim3.tu-clausthal.de> <387BC2C7.44B12247@confusion.net>

next in thread | previous in thread | raw e-mail | index | archive | help
On Tue, Jan 11, 2000 at 06:54:47PM -0500, Laurence Berland wrote:
> 
> 
> Oliver Fromme wrote:
> > 
> > Laurence Berland <stuyman@confusion.net> wrote in list.freebsd-questions:
> >  > Oliver Fromme wrote:
> >  >>
> >  >> I'm afraid there is no other way than using global variables.
> >  >> Be sure to declare them as ``volatile sig_atomic_t''.
> >  >>
> >  > What does this do as compared to declaring them normally?
> > 
> > It makes them work, as opposed to not work.  :-)
> > 
> > Seriously.  You _must_ declare global variables which are
> > accessed from a signal handler as ``volatile sig_atomic_t''.
> > Everything else is _not_ guaranteed to work (and if it works,
> > then it's just pure luck).
> > 
> > Regards
> >    Oliver
> > 
> 
> Does this have something to do with the signal being caught while
> we're in the signal routine?  Am I on the right track?

Almost.  AFAIK, when you use `volatile' each time the variable is
referenced, the compiler will generate a memory reference, not relying
on the value being saved in a register, etc.  The same if true for when
the variable is `written', i.e. the memory copy is kept up to date.

For the simple program:

	volatile int k;

	int main (void)
	{
		k = 0;
		k = 1;
		return 0;
	}

the command `cc -S' will generate the following (slightly edited)
assembler code.  The comments are there to help you understand which
lines of the assembler code correspond to C code.

	.text
	        .p2align 2
	.globl main
	        .type    main,@function
	main:
	        pushl %ebp		/*	{			*/
	        movl %esp,%ebp

	        movl $0,k		/*		k = 0;		*/

	        movl k,%eax		/*		k = 1;		*/
	        incl %eax
	        movl %eax,k

	        xorl %eax,%eax		/*		return 0;	*/

	        leave			/*	}			*/
	        ret
	.Lfe1:
	        .size    main,.Lfe1-main
	        .comm   k,4,4		/*	volatile int k;		*/

You can see that although %eax is used as an intermediate register for
increasing the C variable called `k', the value of %eax is saved to the
memory location of `k' before the end of the k = 1; statement.

This is exactly what volatile means.  The memory value is always
updated to reflect changes to the value of every volatile identifier.

-- 
Giorgos Keramidas, < keramida @ ceid . upatras . gr >
"What we have to learn to do, we learn by doing." [Aristotle]


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20000112045121.A74078>