UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2008-06-03 09:35 AM

shivamasam
Member
Registered: 2007-03-13
Posts: 8

Re: alarm() not working.

Hi,
I am trying to write a simple sample program for a loopy alarm....it's not working...catching the signal for the first time...but the next time it says "Alarm Clock"..and stops.. it's said in some web site that it's a bug on sun's part. is it so?

here is the code:

#include<stdio.h>
#include<stdlib.h>
#include<signal.h>
#include<unistd.h>

static void sig_Handler(int);

int main()
{
pid_t pid;
int sta;

signal(SIGALRM,sig_Handler);

alarm(5);

for(;;);

exit(0);
}

static void sig_Handler(int sigtype)
{
if(sigtype == SIGALRM)
{
printf("\n alarm expired");
alarm(5);
}
return;
}

Offline

#2 2008-06-03 12:59 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,847
Website

Re: alarm() not working.

Not really a "bug" per se; more a "feature"... ;-)  That's just one of the many subtle
behavior differences that vary among Unix-like OS's...  On some, signal() registers a
one-shot handler, and you must re-register it after every triggering; on others, it
registers a permanent handler that never gets reset...  You can't really rely on one
behavior or the other, unless you know for sure how the system you're working on
behaves...  Your best bet is to use sigaction() instead of signal(), and then you can
control the behavior via the SA_RESETHAND flag (as well as controlling other similar
non-standard behavior, such as SA_RESTART)...

Offline

#3 2008-06-03 01:08 PM

shivamasam
Member
Registered: 2007-03-13
Posts: 8

Re: alarm() not working.

thanks... I got it...

Offline

#4 2008-07-09 06:13 PM

Xoote
Guest

Re: alarm() not working.

thanks for the tip

Board footer

Powered by FluxBB