简体   繁体   English

我想知道系统调用()中断时到达哪个信号

[英]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(). 线程等待返回epoll_wait()。 Sometimes epoll_wait() returns -1 and errno is EINTR. 有时epoll_wait()返回-1,而errno是EINTR。 EINTR means that system call() is interrupted by a signal. EINTR表示系统调用()被信号中断。 I added to process EINTR. 我添加了处理EINTR。 However I do not know what a signal is arrived and why a signal is arrived. 但是我不知道什么信号到达,为什么信号到达。 I wonder it. 我不知道

Method 1. 方法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. 当epoll_wait()中断时,我无法捕捉到信号。

Method 2 方法2

When I execute my application in strace tool, epoll_wait() never be interrupted. 当我在strace工具中执行应用程序时,epoll_wait()永远不会被中断。

My problem is reproduced very well in GDB tool. 我的问题在GDB工具中很好地再现了。 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(!)). 在这里看看可以处理的不同信号(注意:并非所有信号都可以在您自己的signal-handler(!)中处理)。

May be you should try setting signal handler for catching all signals and set your signal flags to SA_SIGINFO 可能是您应该尝试设置信号处理程序以捕获所有信号并将信号标志设置为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. 在主程序中重新注册处理程序,并忽略epoll中的EINTR。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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