[ACCEPTED]-UNIX/Linux signal handling: SIGEV_THREAD-signals
struct sigevent
is not about specifying how the process 19 will handle a signal - struct sigaction
and sigaction()
are how you 18 do that. Instead, struct sigevent
is used to specify how 17 your process will be informed of some asychronous 16 event - like the completion of asychronous 15 IO, or a timer expiring.
The sigev_notify
field specifies 14 how the event should be notified:
SIGEV_NONE
- no notification at all. The remainder of the fields are ignored.SIGEV_SIGNAL
- a signal is sent to the process. Thesigev_signo
field specifies the signal, thesigev_value
field contains supplementary data that is passed to the signal handling function, and the remainder of the fields are ignored.SIGEV_THREAD
- a function is called in a new thread. Thesigev_notify_function
field specifies the function that is called,sigev_value
contains supplementary data that is passed to the function, andsigev_notify_attributes
specifies thread attributes to use for the thread creation. The remainder of the fields are ignored.
Note in 13 particular that if you set SIGEV_THREAD
, the sigev_signo
field 12 is ignored - the struct sigevent
is about specifying either a 11 thread or a signal as a notification method, not 10 about specifying a thread as the way that 9 a signal should be handled.
The struct sigevent
must also 8 be passed to a function - like timer_create()
- that sets 7 up the asychronous event that will be notified. Simply 6 creating a struct sigevent
object does not do anything 5 special.
If you wish to use a dedicated thread 4 to handle a signal, create the thread up 3 front and have it loop around, blocking 2 on sigwaitinfo()
. Use sigprocmask()
to block the signal in every 1 other thread.
I think you are mixing up your signal handling 10 idioms here, you create a sigevent
structure and 9 then do nothing with it and then use signal()
within 8 the signal handler. The following code shows 7 a very simple signal handling routine based 6 on your code; note that I have changed the 5 definition of my_handler
. If you need more sophisticated 4 handling then sigaction()
is probably the system call 3 you need to look into.
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <time.h>
void my_handler(int sig)
{
printf("my_handler caught\n");
signal(sig,my_handler);
}
int main()
{
signal(SIGRTMIN,my_handler);
kill(0,SIGRTMIN); // This should invoke the signal and call the function
while(1) ; // Infinite loop in case the program ends before the signal gets caught!
}
This works under 2 cygwin
on my windows box (no access to a linux 1 box at the minute).
I hope this works.
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <time.h>
void
my_handler (int sig)
{
printf ("my_handler caught\n");
signal (sig, my_handler);
}
int
main ()
{
int signo;
struct sigevent sevp;
sigset_t set;
if (sigemptyset (&set) == -1)
perror ("sigemptyset");
if (sigaddset (&set, SIGRTMIN) == -1)
perror ("sigaddset");
if (sigprocmask (SIG_BLOCK, &set, NULL) == -1)
perror ("sigprocmask");
sevp.sigev_notify = SIGEV_THREAD;
sevp.sigev_signo = SIGRTMIN;
sevp.sigev_value.sival_ptr = NULL;
kill (0, SIGRTMIN);
if (sigwait (&set, &signo) == 0)
my_handler (signo);
else
perror ("sigwait");
}
0
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.