简体   繁体   中英

How to use SIGALRM and SIGPOLL in asynchronous mode?

I have to work on both of the SIGALRM and SIGPOLL signals asynchronously. I have come up with the following. I am not sure if this is the correct approach to handle multiple signals asynchronously.

void handle_alarm( int sig ) {    
printf("Timeout!\n");   
printf("?\n");  
}


struct sigaction sa;    
sigemptyset(&sa.sa_mask);
sigaddset(&sa.sa_mask, SIGPOLL);
sigaddset(&sa.sa_mask, SIGALRM);
sa.sa_handler = &handle_alarm;
//sa.sa_handler = &handle_sigpoll;
sa.sa_flags = 0;
if (sigaction(SIGPOLL, &sa, 0) == -1) {
    perror(0);  
    printf("here\n");
    exit(1);
}       
if (sigaction(SIGALRM, &sa, 0) == -1) {
    perror(0);  
    printf("here\n");
    exit(1);
}   

// Setup UDP Socket
sock_id = socket(AF_INET, SOCK_DGRAM, 0);                   
if(sock_id<0)
    perror("Error opening Socket\n");   

fcntl(sock_id,F_SETOWN,getpid());
fcntl(sock_id, F_SETFL, FASYNC);

alarm(7);

How should I improve my code? Sorry for the brevity of my question details.

您可以在信号处理程序函数中创建一个线程,并使用pthread_detach将其关闭,而无需使用pthread_join。

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