Program quit after catching the sigal SIGUSR1

Why my program quit after catching the first SIGUSR1 signal send by a
periodic timer at 1.5 second intervals. Is there any special thing to do to
return from a signal handler function.
Thanks for any help.
Rejean Senecal

include <…>
using namespace std;
#define MEG (1024*1024)

void SignalHandler( int sig_no)
{
cout << "signal catched ! no " << sig_no << endl;
return ;
}

int main(int argc, char *argv)
{
int iSize=10;
struct _clockperiod period;
int ret;

signal(SIGUSR1 ,SignalHandler);

ClockPeriod(CLOCK_REALTIME,NULL,&period,0);

cout << "Clock Resolution " << period.nsec << " " << period.fract <<
endl;
cout << “Max priority” << sched_get_priority_max(SCHED_RR) << endl;
cout << “Min priority” << sched_get_priority_min(SCHED_RR) << endl;
timer_t tempTimer = CreateTimer();
sleep(60);
ret = timer_delete(tempTimer);
if (ret) {
perror(“TimerDestroy”);
}
cout << “End” << endl;

return(0);
}

/Create the periodic timer/
timer_t CreateTimer(){
int ret;
timer_t aTimer;
sigevent event;
struct itimerspec itime;
SIGEV_SIGNAL_INIT(&event,SIGUSR1);

ret = timer_create(CLOCK_MONOTONIC,&event,&aTimer);
if (ret) { perror(“Timer_create”); }


itime.it_value.tv_sec = 1;
/* 500 million nsecs = .5 secs /
itime.it_value.tv_nsec = 500000000;
itime.it_interval.tv_sec = 1;
/
500 million nsecs = .5 secs */
itime.it_interval.tv_nsec = 500000000;
ret = timer_settime(aTimer, 0, &itime, NULL);
if (ret) { perror(“timer_settime”);
}else{
cout << “Timer started” << endl;
}

/*

  • As of the timer_settime(), we will receive our pulse
  • in 1.5 seconds (the itime.it_value) and every 1.5
  • seconds thereafter (the itime.it_interval)
    */

return aTimer;
}

If you check the docs for sleep, you’ll find that it will be interrupted by
a signal and will return the number of seconds not slept. Try:

int remaining = 60;
while(remaining > 0)
remaining = sleep(remaining);

cheers,

Kris

“Rejean Senecal” <rsenecal@oerlikon.ca-no-spam> wrote in message
news:agjpaj$4hk$1@inn.qnx.com

Why my program quit after catching the first SIGUSR1 signal send by a
periodic timer at 1.5 second intervals. Is there any special thing to do
to
return from a signal handler function.
Thanks for any help.
Rejean Senecal

include <…
using namespace std;
#define MEG (1024*1024)

void SignalHandler( int sig_no)
{
cout << "signal catched ! no " << sig_no << endl;
return ;
}

int main(int argc, char *argv)
{
int iSize=10;
struct _clockperiod period;
int ret;

signal(SIGUSR1 ,SignalHandler);

ClockPeriod(CLOCK_REALTIME,NULL,&period,0);

cout << "Clock Resolution " << period.nsec << " " << period.fract
endl;
cout << “Max priority” << sched_get_priority_max(SCHED_RR) << endl;
cout << “Min priority” << sched_get_priority_min(SCHED_RR) << endl;
timer_t tempTimer = CreateTimer();
sleep(60);
ret = timer_delete(tempTimer);
if (ret) {
perror(“TimerDestroy”);
}
cout << “End” << endl;

return(0);
}

/Create the periodic timer/
timer_t CreateTimer(){
int ret;
timer_t aTimer;
sigevent event;
struct itimerspec itime;
SIGEV_SIGNAL_INIT(&event,SIGUSR1);

ret = timer_create(CLOCK_MONOTONIC,&event,&aTimer);
if (ret) { perror(“Timer_create”); }


itime.it_value.tv_sec = 1;
/* 500 million nsecs = .5 secs /
itime.it_value.tv_nsec = 500000000;
itime.it_interval.tv_sec = 1;
/
500 million nsecs = .5 secs */
itime.it_interval.tv_nsec = 500000000;
ret = timer_settime(aTimer, 0, &itime, NULL);
if (ret) { perror(“timer_settime”);
}else{
cout << “Timer started” << endl;
}

/*

  • As of the timer_settime(), we will receive our pulse
  • in 1.5 seconds (the itime.it_value) and every 1.5
  • seconds thereafter (the itime.it_interval)
    */

return aTimer;
}

\

Rejean Senecal <rsenecal@oerlikon.ca-no-spam> wrote:
: void SignalHandler( int sig_no)
: {
: cout << "signal catched ! no " << sig_no << endl;
: return ;
: }

Today’s naive question: is it safe to call cout in a signal handler?


Steve Reid stever@qnx.com
TechPubs (Technical Publications)
QNX Software Systems

I was pondering that as well. Since the C stdio stuff isn’t interrupt
handler safe, I would expect that the C++ wouldn’t be either. We don’t have
any safety charts at all for C++ functions do we? Looks like something for
your to-do list…

Kris

“Steve Reid” <stever@qnx.com> wrote in message
news:agk51e$h24$1@nntp.qnx.com

Rejean Senecal <> rsenecal@oerlikon.ca-no-spam> > wrote:
: void SignalHandler( int sig_no)
: {
: cout << "signal catched ! no " << sig_no << endl;
: return ;
: }

Today’s naive question: is it safe to call cout in a signal handler?


Steve Reid > stever@qnx.com
TechPubs (Technical Publications)
QNX Software Systems

“Steve Reid” <stever@qnx.com> wrote in message
news:agk51e$h24$1@nntp.qnx.com

Rejean Senecal <> rsenecal@oerlikon.ca-no-spam> > wrote:
: void SignalHandler( int sig_no)
: {
: cout << "signal catched ! no " << sig_no << endl;
: return ;
: }

Today’s naive question: is it safe to call cout in a signal handler?

From memory in C the printf function is thread and signal safe
but not usage of the file handle. Since cout is kind of a file handler
I guess it’s not safe.


Steve Reid > stever@qnx.com
TechPubs (Technical Publications)
QNX Software Systems

Mario Charest postmaster@127.0.0.1 wrote:

“Steve Reid” <> stever@qnx.com> > wrote in message
Today’s naive question: is it safe to call cout in a signal handler?

From memory in C the printf function is thread and signal safe

Thread-safe, yes; but not signal-safe.

BTW By “in C”, I presume you mean in POSIX. The only functions the C
Standard requires to be signal-safe to any extent are abort(), _Exit(),
and signal(); and of course, the C Standard doesn’t even mention
threads…

\

Wojtek Lerch QNX Software Systems Ltd.

Steve Reid <stever@qnx.com> wrote:

Rejean Senecal <> rsenecal@oerlikon.ca-no-spam> > wrote:
: void SignalHandler( int sig_no)
: {
: cout << "signal catched ! no " << sig_no << endl;
: return ;
: }

Today’s naive question: is it safe to call cout in a signal handler?

I’d bet no. printf() isn’t, I can’t see cout being so.

But, it is also one of those things that won’t hurt you badly.

(Unlike, for instance, calling cout or printf() in, say, and
interrupt handler.)

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.

David Gibbs <dagibbs@qnx.com> wrote:

Steve Reid <> stever@qnx.com> > wrote:
Today’s naive question: is it safe to call cout in a signal handler?

I’d bet no. printf() isn’t, I can’t see cout being so.

But, it is also one of those things that won’t hurt you badly.

Unless, of course, you prefer a crash that happens every time and is
easy to debug, to a crash that only happens once every two months and
you wish you could blame it on flakey hardware or cosmic rays or
something…

Calling printf() from a signal handler works most of the time. But
many programs can’t guarantee that it’s impossible for the signal to
arrive when printf() or some other stdio function happens to be in the
middle of updating stdout. It is a very small window for most programs,
and you probably can run them successfully for months before they crash.

(Unlike, for instance, calling cout or printf() in, say, and
interrupt handler.)

Yeah, those are much easier to figure out…


Wojtek Lerch QNX Software Systems Ltd.

Wojtek Lerch <wojtek_l@yahoo.ca> writes:

| Calling printf() from a signal handler works most of the time. But
| many programs can’t guarantee that it’s impossible for the signal to
| arrive when printf() or some other stdio function happens to be in the
| middle of updating stdout. It is a very small window for most programs,
| and you probably can run them successfully for months before they crash.

Or (sometimes worse, depending on your environment)
deadlock because the stdio code that was interrupted
is holding a mutex.