From owner-freebsd-questions Wed Jan 12 5:16:53 2000 Delivered-To: freebsd-questions@freebsd.org Received: from mx2.x-treme.gr (mx2.x-treme.gr [212.120.192.15]) by hub.freebsd.org (Postfix) with ESMTP id D3F4E15021 for ; Wed, 12 Jan 2000 05:16:44 -0800 (PST) (envelope-from keramida@diogenis.ceid.upatras.gr) Received: from localhost.hell.gr (pat41.x-treme.gr [212.120.197.233]) by mx2.x-treme.gr (8.9.3/8.9.3/IPNG-ADV-ANTISPAM-0.1) with SMTP id PAA12747 for ; Wed, 12 Jan 2000 15:16:40 +0200 Received: (qmail 76410 invoked by uid 1001); 12 Jan 2000 02:51:21 -0000 Date: Wed, 12 Jan 2000 04:51:21 +0200 From: Giorgos Keramidas To: Laurence Berland Cc: freebsd-questions@FreeBSD.ORG Subject: Volatile variables (was: Giving a sighandler more information) Message-ID: <20000112045121.A74078@hades.hell.gr> Reply-To: keramida@ceid.upatras.gr References: <200001111215.NAA22385@dorifer.heim3.tu-clausthal.de> <387BC2C7.44B12247@confusion.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Mailer: Mutt 1.0pre3i In-Reply-To: <387BC2C7.44B12247@confusion.net> Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG On Tue, Jan 11, 2000 at 06:54:47PM -0500, Laurence Berland wrote: > > > Oliver Fromme wrote: > > > > Laurence Berland 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