Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C Moderated > Re: signal catc...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 9 of 10 Topic 1078 of 1133
Post > Topic >>

Re: signal catching not work

by Terry <cong818@[EMAIL PROTECTED] > Apr 21, 2008 at 01:19 PM

Thanks for everyone's advice. I 've modified the program as below and 
come to these conclusions:

1. signal handler seems not having to register for multiple times, 
though the manual sounds like it has to.
2. return from signal handler that catches SIGSEGV is undefined and 
cannot go back and recover normal control flow. If we really want to go 
back, use long jump.
3. in practice, a signal may be raised at anytime during the execution 
of the program, so the signal handler must not call any lib routines 
except abort() and _Exit() to keep asynchronous-safe.
reference: 
https://www.securecoding.cert.org/confluence/display/seccode/SIG30-C.+Call+only+asynchronous-safe+functions+within+signal
+handlers
4. The 'long jump in sighandler example' failing to catch the second 
SIGSEGV, even if the handler is registered for the second time, seems 
still weird to me. I guess this is beyond the explaination of ANSI C.


#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <setjmp.h>

#define MYSIG SIGINT
#define y main

jmp_buf buf;

volatile sig_atomic_t i=0;
void sighandler(int a){
     longjmp(buf, ++i);
}

int x(){
     if(signal(MYSIG, sighandler)==SIG_ERR){
         perror("error signal");
         abort();
     }
     int *a=NULL;
     switch(setjmp(buf)){
         case 0:
         case 1:
             puts("First time caught.");
#if 1
             if(SIG_ERR==signal(MYSIG, sighandler))
                 return;
#endif
             raise(MYSIG);
             getchar();
             *a=1;
             break;
         default:
             puts("Second time caught.");
     }
}


volatile sig_atomic_t j=0;
void sighandler2(int a){
     ++j;
}

int y(){
     if(signal(MYSIG, sighandler2)==SIG_ERR){
         perror("error signal");
         abort();
     }
     int *a=NULL;
     while(1){
         switch(j){
             case 0:
                 raise(MYSIG);
                 //getchar(); // press ctrl+c here
                 //*a=10;
                 break;
             case 1:
                 puts("First time caught.");
#if 0
                 if(SIG_ERR==signal(MYSIG, sighandler2))
                     return;
#endif
                 raise(MYSIG);
                 //getchar();
                 //*a=10;
                 break;
             default:
                 puts("Second time caught.");
                 return;
         }
     }
}
-- 
comp.lang.c.moderated - moderation address: clcm@[EMAIL PROTECTED]
 -- you must
have an appropriate newsgroups line in your header for your mail to be
seen,
or the newsgroup name in square brackets in the subject line.  Sorry.
 




 10 Posts in Topic:
signal catching not work
Terry <cong818@[EMAIL   2008-04-16 16:56:15 
Re: signal catching not work
jgd@[EMAIL PROTECTED] (J  2008-04-18 16:10:59 
Re: signal catching not work
Keith Thompson <kst-u@  2008-04-21 13:18:42 
Re: signal catching not work
Jack Klein <jackklein@  2008-04-18 16:11:11 
Re: signal catching not work
=?ISO-8859-1?Q?Hans-Bernh  2008-04-18 16:11:13 
Re: signal catching not work
Kenneth Brody <kenbrod  2008-04-18 16:11:30 
Re: signal catching not working
Markus Elfring <Markus  2008-04-18 16:11:26 
Re: signal catching not work
Thomas Richter <thor@[  2008-04-18 16:11:23 
Re: signal catching not work
Terry <cong818@[EMAIL   2008-04-21 13:19:41 
Re: signal catching not work
Terry <cong818@[EMAIL   2008-05-05 16:01:17 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu Jul 24 2:21:25 CDT 2008.