How to catch the signal ?

bridged with qdn.public.qnxrtp.applications
Post Reply
Belinda

How to catch the signal ?

Post by Belinda » Tue May 14, 2002 4:23 am

Hi, everybody


There is a process consists of mutltihreads. When pressing "Ctrl-C", the
process is terminated and exit. Who knows how to catch this signal and stop
all threads to work?

Thanks,

Belinda

Mario Charest

Re: How to catch the signal ?

Post by Mario Charest » Tue May 14, 2002 11:43 am

"Belinda" <yye@is2.dal.ca> wrote in message news:abq2tb$b6v$1@inn.qnx.com...
Hi, everybody


There is a process consists of mutltihreads. When pressing "Ctrl-C", the
process is terminated and exit. Who knows how to catch this signal and
stop
all threads to work?
You can catch the signal by setting a signal handler with the function
signal. The CTRL-C is signal SIGTERM or SIGINT, don't remember,
cut & paste makes you dump ;-)

In the signal handler you can call exit() or do what ever you want to do
to clean up gracefully. Beware though not all functions can be using
inside signal handler.
Thanks,

Belinda

Kris Warkentin

Re: How to catch the signal ?

Post by Kris Warkentin » Tue May 14, 2002 1:39 pm

CTRL-C is SIGINT, kill <pid> will send a SIGTERM.

Note also that if you want a specific thread to handle certain signals, you
can block them for all threads except the one you want using sigprocmask().

I don't know how much you know but the function you're looking for is
signal(). It will register a signal handler that will be called once.
After it is handled, the default handler is restored. If you want more
robust (or at least finer control), you'll want to look at the documentation
for sigaction(). It will do everything you want and more.

cheers,

Kris

"Mario Charest" <goto@nothingness.com> wrote in message
news:abqskk$hr$1@inn.qnx.com...
"Belinda" <yye@is2.dal.ca> wrote in message
news:abq2tb$b6v$1@inn.qnx.com...
Hi, everybody


There is a process consists of mutltihreads. When pressing "Ctrl-C", the
process is terminated and exit. Who knows how to catch this signal and
stop
all threads to work?

You can catch the signal by setting a signal handler with the function
signal. The CTRL-C is signal SIGTERM or SIGINT, don't remember,
cut & paste makes you dump ;-)

In the signal handler you can call exit() or do what ever you want to do
to clean up gracefully. Beware though not all functions can be using
inside signal handler.


Thanks,

Belinda



Wojtek Lerch

Re: How to catch the signal ?

Post by Wojtek Lerch » Tue May 14, 2002 2:18 pm

Mario Charest <goto@nothingness.com> wrote:
In the signal handler you can call exit() or do what ever you want to do
to clean up gracefully. Beware though not all functions can be using
inside signal handler.
Um... Actually, exit() is one of them. :-)

But _exit() is OK.

--
Wojtek Lerch QNX Software Systems Ltd.

Bernard Leclerc

Re: How to catch the signal ?

Post by Bernard Leclerc » Tue May 14, 2002 11:51 pm

Belinda wrote:
There is a process consists of mutltihreads. When pressing "Ctrl-C",
the process is terminated and exit. Who knows how to catch this
signal and stop all threads to work?
A little example is all you need:

#include <signal.h>

static bool alive = true;

static void sigHandler(int signo, siginfo_t* info, void* other)
{
printf("Received signal %i\n", signo);
alive = false;
}

int main(int argc, char *argv[])
{
struct sigaction act;
sigset_t set, emptySet;

/* Install a signal handler to catch SIGTERM and SIGINT. */
sigemptyset(&set);
sigaddset(&set, SIGTERM);
sigaddset(&set, SIGINT);
act.sa_flags = 0;
act.sa_mask = set;
act.sa_sigaction = sigHandler;
sigaction(SIGTERM, &act, NULL);
sigaction(SIGINT, &act, NULL);

/* Do your stuff here. */
while (alive)
{
...
}
}

-- bl

Mario Charest

Re: How to catch the signal ?

Post by Mario Charest » Wed May 15, 2002 1:38 am

"Wojtek Lerch" <wojtek_l@yahoo.ca> wrote in message
news:abr6c2$g16$1@nntp.qnx.com...
Mario Charest <goto@nothingness.com> wrote:
In the signal handler you can call exit() or do what ever you want to do
to clean up gracefully. Beware though not all functions can be using
inside signal handler.

Um... Actually, exit() is one of them. :-)
The doc says it not applicable ? (but warns POSIX says it's unsafe on other
platform...)

But _exit() is OK.

--
Wojtek Lerch QNX Software Systems Ltd.

Mario Charest

Re: How to catch the signal ?

Post by Mario Charest » Wed May 15, 2002 1:39 am

"Bernard Leclerc" <whittom-leclerc@sympatico.ca> wrote in message
news:abs785$17a$1@inn.qnx.com...
Belinda wrote:

There is a process consists of mutltihreads. When pressing "Ctrl-C",
the process is terminated and exit. Who knows how to catch this
signal and stop all threads to work?

A little example is all you need:

#include <signal.h

static bool alive = true;

static void sigHandler(int signo, siginfo_t* info, void* other)
{
printf("Received signal %i\n", signo);
alive = false;
}
printf is not signal safe.
int main(int argc, char *argv[])
{
struct sigaction act;
sigset_t set, emptySet;

/* Install a signal handler to catch SIGTERM and SIGINT. */
sigemptyset(&set);
sigaddset(&set, SIGTERM);
sigaddset(&set, SIGINT);
act.sa_flags = 0;
act.sa_mask = set;
act.sa_sigaction = sigHandler;
sigaction(SIGTERM, &act, NULL);
sigaction(SIGINT, &act, NULL);

/* Do your stuff here. */
while (alive)
{
...
}
}

-- bl










Kris Warkentin

Re: How to catch the signal ?

Post by Kris Warkentin » Wed May 15, 2002 12:21 pm

Oops...someone better tell our docs group - that example is lifted right out
of the page for sigaction. DOH!

Kris

"Mario Charest" <goto@nothingness.com> wrote in message
news:absdja$5fu$1@inn.qnx.com...
"Bernard Leclerc" <whittom-leclerc@sympatico.ca> wrote in message
news:abs785$17a$1@inn.qnx.com...
Belinda wrote:

There is a process consists of mutltihreads. When pressing "Ctrl-C",
the process is terminated and exit. Who knows how to catch this
signal and stop all threads to work?

A little example is all you need:

#include <signal.h

static bool alive = true;

static void sigHandler(int signo, siginfo_t* info, void* other)
{
printf("Received signal %i\n", signo);
alive = false;
}


printf is not signal safe.

int main(int argc, char *argv[])
{
struct sigaction act;
sigset_t set, emptySet;

/* Install a signal handler to catch SIGTERM and SIGINT. */
sigemptyset(&set);
sigaddset(&set, SIGTERM);
sigaddset(&set, SIGINT);
act.sa_flags = 0;
act.sa_mask = set;
act.sa_sigaction = sigHandler;
sigaction(SIGTERM, &act, NULL);
sigaction(SIGINT, &act, NULL);

/* Do your stuff here. */
while (alive)
{
...
}
}

-- bl












Steve Reid

Re: How to catch the signal ?

Post by Steve Reid » Wed May 15, 2002 1:40 pm

Mario Charest <goto@nothingness.com> wrote:

: "Wojtek Lerch" <wojtek_l@yahoo.ca> wrote in message
: news:abr6c2$g16$1@nntp.qnx.com...
:> Um... Actually, exit() is one of them. :-)

: The doc says it not applicable ? (but warns POSIX says it's unsafe on other
: platform...)

I've recently fixed that in the docs.

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

Steve Reid

Re: How to catch the signal ?

Post by Steve Reid » Wed May 15, 2002 1:41 pm

Kris Warkentin <kewarken@qnx.com> wrote:
: Oops...someone better tell our docs group - that example is lifted right out
: of the page for sigaction. DOH!

No rest for the weary -- I've added it to my list of things to do.

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

Wojtek Lerch

Re: How to catch the signal ?

Post by Wojtek Lerch » Wed May 15, 2002 3:02 pm

Mario Charest <goto@nothingness.com> wrote:
"Bernard Leclerc" <whittom-leclerc@sympatico.ca> wrote in message
news:abs785$17a$1@inn.qnx.com...
....
#include <signal.h

static bool alive = true;

static void sigHandler(int signo, siginfo_t* info, void* other)
{
printf("Received signal %i\n", signo);
alive = false;
}


printf is not signal safe.
But that might not be a problem, provided that the while loop in main()
doesn't call anything signal-unsafe without masking SIGINT and SIGTERM
first. The rule is that it's illegal to call a signal-unsafe function
from a signal handler when the function that was interrupted is also
signal-unsafe.

BTW It also might be a good idea to declare "alive" as volatile.
int main(int argc, char *argv[])
{
struct sigaction act;
sigset_t set, emptySet;

/* Install a signal handler to catch SIGTERM and SIGINT. */
sigemptyset(&set);
sigaddset(&set, SIGTERM);
sigaddset(&set, SIGINT);
act.sa_flags = 0;
act.sa_mask = set;
act.sa_sigaction = sigHandler;
sigaction(SIGTERM, &act, NULL);
sigaction(SIGINT, &act, NULL);

/* Do your stuff here. */
while (alive)
{
...
}
}
--
Wojtek Lerch QNX Software Systems Ltd.

Wojtek Lerch

Re: How to catch the signal ?

Post by Wojtek Lerch » Wed May 15, 2002 3:22 pm

Steve Reid <stever@qnx.com> wrote:
Kris Warkentin <kewarken@qnx.com> wrote:
: Oops...someone better tell our docs group - that example is lifted right out
: of the page for sigaction. DOH!

No rest for the weary -- I've added it to my list of things to do.
But I don't think there's anything wrong with the example in the docs,
perhaps except that it may give some people the impression that it's
always safe to call printf() in a signal handler.

Since the sample program drops a signal on itself by calling kill()
(that's the example we're talking about, isn't it?), there's no way for
the signal to interrupt a signal-unsafe function.
--
Wojtek Lerch QNX Software Systems Ltd.

Post Reply

Return to “qdn.public.qnxrtp.applications”