UNIX / Linux Signal Processing: SIGEV_THREAD

I put a simple signal handler in my code. I initialized the sigevent structure, with a handler function to catch the signal.

Can anyone pay attention to why the code is not working? Ideally, if there is a signal, my handler should be called. But this is not so.

Please help me, thanks Kingsmasher1

enter code here #include <stdlib.h> #include <unistd.h> #include <stdio.h> #include <signal.h> #include <time.h> void my_handler(int sival_int, void* sival_ptr) { printf("my_handler caught\n"); signal(sig,my_handler); } int main() { struct sigevent sevp; sevp.sigev_notify=SIGEV_THREAD; sevp.sigev_signo=SIGRTMIN; sevp.sigev_value.sival_ptr=NULL; sevp.sigev_notify_function=(void*)my_handler; kill(0,SIGRTMIN); // This should invoke the signal and call the function } 
+4
source share
3 answers

struct sigevent is not about defining how the signal is processed - struct sigaction and sigaction() - how you do it. Instead, struct sigevent used to indicate how your process will be informed of any asynchronous event — for example, asynchronous I / O completion or timer expiration.

The sigev_notify field indicates how the event should be notified:

  • SIGEV_NONE - no notification at all. The remaining fields are ignored.
  • SIGEV_SIGNAL - a signal is sent to the process. The sigev_signo field indicates the signal, the sigev_value field contains additional data that is passed to the signal processing function, and the remaining fields are ignored.
  • SIGEV_THREAD - the function is called in a new thread. The sigev_notify_function field indicates the function that is being called, sigev_value contains additional data that is passed to the function, and sigev_notify_attributes indicates the attributes of the streams to use to create the stream. The remaining fields are ignored.

Note, in particular, that if you set SIGEV_THREAD , the sigev_signo field sigev_signo ignored - struct sigevent indicates neither a stream nor a signal as a notification method, but not as a stream as the way the signal should be processed.

struct sigevent should also be passed to a function - like timer_create() - that sets an asynchronous event to be notified. Just creating a struct sigevent object does nothing special.

If you want to use a dedicated stream to process the signal, create a stream up and run it by blocking sigwaitinfo() . Use sigprocmask() to block the signal in each other thread.

+13
source

I think you mix your idioms of signal processing here, you create a sentiment structure, and then do nothing with it, and then use signal () in the signal handler. The following code shows a very simple signal processing procedure based on your code; Notice that I changed the definition of my_handler. If you need more complex processing then sigaction (), if you probably need a system call you need to learn.

 #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! } 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 cygwin on my window window (without access to the Linux port per minute).

+2
source

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
source

Source: https://habr.com/ru/post/1341796/


All Articles