public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Compiling differences?
@ 2004-05-12 22:25 Matias Bordese
  2004-05-13  2:15 ` Muthukumar Ratty
  0 siblings, 1 reply; 2+ messages in thread
From: Matias Bordese @ 2004-05-12 22:25 UTC (permalink / raw)
  To: gcc, gcc-help

[-- Attachment #1: Type: TEXT/PLAIN, Size: 409 bytes --]


I was using the setitimer function (libc 2.2.4) and according to the 
options I compile with (with gcc 2.96), I got different behaviors 
(besides, the binaries differ too):

gcc -o timer -Wall -ansi -pedantic timers.c
(I get the message: "Alarm clock", and the program finishes)

and

gcc -o timer2 timers.c
(I get the expected behavior)

	Matias

PD.: The file is attached.
PD2.: Sorry for my poor English.

[-- Attachment #2: Type: TEXT/PLAIN, Size: 577 bytes --]

#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include <signal.h>

void s_handler (int signum);

int main ()
{
	struct itimerval timer;

	timer.it_value.tv_sec = 2;      /* Timer actual, segundos */
	timer.it_value.tv_usec = 0;     /* Timer actual, microsegundos */

	timer.it_interval.tv_sec = 1;
	timer.it_interval.tv_usec = 0;

	signal(SIGALRM, s_handler);

	setitimer(ITIMER_REAL, &timer, NULL);

	while(1){

	}

	return 0;

}

void s_handler (int signum)
{
	if (signum==SIGALRM){
		printf("\nPlazo vencido\n");
	}
}




^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Compiling differences?
  2004-05-12 22:25 Compiling differences? Matias Bordese
@ 2004-05-13  2:15 ` Muthukumar Ratty
  0 siblings, 0 replies; 2+ messages in thread
From: Muthukumar Ratty @ 2004-05-13  2:15 UTC (permalink / raw)
  To: Matias Bordese; +Cc: gcc, gcc-help


Hi,
This is due to the ansi signal specification. It sets the signal handler
to SIG_DFL before executing the current handler( your). So the next signal
terminates the process (so you see "Alarm clock" message). If you dont
specify ansi standard then your handler is always executed.

The right way to do this is to use signal in your handler again
i.e

void s_handler (int signum)
{
        signal(SIGALRM, s_handler);
        if (signum==SIGALRM){
                printf("\nPlazo vencido\n");
        }
}


I would like to know where this magic is happening. I guess libc calls
differ but not sure :)

Muthu.



On Wed, 12 May 2004, Matias Bordese wrote:

>
> I was using the setitimer function (libc 2.2.4) and according to the
> options I compile with (with gcc 2.96), I got different behaviors
> (besides, the binaries differ too):
>
> gcc -o timer -Wall -ansi -pedantic timers.c
> (I get the message: "Alarm clock", and the program finishes)
>
> and
>
> gcc -o timer2 timers.c
> (I get the expected behavior)
>
> 	Matias
>
> PD.: The file is attached.
> PD2.: Sorry for my poor English.
>




^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2004-05-13  2:15 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-05-12 22:25 Compiling differences? Matias Bordese
2004-05-13  2:15 ` Muthukumar Ratty

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).