简体   繁体   中英

I want to know which a signal is arrived when system call() is interrupted

My application has two threads. Each threads recevive some data from the server via each sockets. Threads wait to return epoll_wait(). Sometimes epoll_wait() returns -1 and errno is EINTR. EINTR means that system call() is interrupted by a signal. I added to process EINTR. However I do not know what a signal is arrived and why a signal is arrived. I wonder it.

Method 1.

I created a thread.

sigset_t sMaskOfSignal;                                               
sigset_t sOldMaskOfSignal;                                            
sigfillset(&sMaskOfSignal);                                           
sigprocmask(SIG_UNBLOCK, &sMaskOfSignal, &sOldMaskOfSignal)

while(1)
{                                                                                        
    sigwait(&sMaskOfSignal, &sArrivedSignal);                                            

    fprintf(stdout, "%d(%s) signal caught\n", sArrivedSignal, strsignal(sArrivedSignal));
}                                                                                        

I could not catch a signal when epoll_wait() is interrupted.

Method 2

When I execute my application in strace tool, epoll_wait() never be interrupted.

My problem is reproduced very well in GDB tool. I need helps....

You can try to implement your own signal handler. If you application gets interrupted by a signal again, your own signal-handler will be called and you can see, what kind of signal has been raised.

void
signal_callback_handler(int signum)
{
  printf("Caught signal %d\n",signum);
  exit(signum); // terminate application
}

int main()
{
  // Register signal handler for all signals you want to handle
  signal(SIGINT, signal_callback_handler);
  signal(SIGABRT, signal_callback_handler);
  signal(SIGSEGV, signal_callback_handler);
  // .. and even more, if you want to
}

Not a very handy-method, but this should (hopefully) enable you to find out, what signal has been raised. Take a look here to see the different signals, that can be handled (note: not all signals can be handled in your own signal-handler(!)).

May be you should try setting signal handler for catching all signals and set your signal flags to SA_SIGINFO

something like this

struct sigaction act;
sigemptyset(&act.sa_mask);
act.sa_flags = SA_SIGINFO;
act.sa_sigaction = <handler>;

sigaction(SIGFPE, &act, 0);
sigaction(SIGHUP, &act, 0);
sigaction(SIGABRT, &act, 0);
sigaction(SIGILL, &act, 0);
sigaction(SIGALRM, &act, 0);
sigaction(SIGALRM, &act, 0);
.
.
.

//and your handler looks like

void handle_sig (int sig, siginfo_t *info, void *ptr)
{
     printf ("Signal is %d\n",sig);
}

Resgister the handler in your main program and ignore EINTR in epoll.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM