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
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
“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
CTRL-C is SIGINT, kill 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
\
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.
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
“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.
“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
\
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
\
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.
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.
Mario Charest <goto@nothingness.com> wrote:
“Bernard Leclerc” <> whittom-leclerc@sympatico.ca> > wrote in message
news:abs785$17a$> 1@inn.qnx.com> …
…
#include <signal.hstatic 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.
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.
Wojtek Lerch QNX Software Systems Ltd.