在我的程序中,sigaddset()
失败并出现错误:
sigaddset:无效的搜索
对应于ESPIPE
in但在 的标准定义中errno.h
未提及此错误。sigaddset()
完整代码太长,我贴出所需部分:
信号处理例程:
void* Ctrl_C_handler(void *arg)
{
int *sock_fd_ptr = (int*)arg;
sigset_t set;
int err;
err = sigemptyset(&set); // Clear the signals holder set
if(!err) perror("sigemptyset(&set)");
err = sigaddset(&set, SIGINT); // Add the SIGINT: Ctrl+C
if(!err) perror("siaddset(&set, SIGINT)");
err = sigthreadmask(SIG_UNBLOCK, &set, NULL); // Set mask as to unblock SIGINT
if(!err) perror("sigthreadmask(SIG_SETMASK, &set, NULL)");
while(1) // Ctrl+C
{
sig = sigwait(&set); // Wait for signal to occur
if(sig == SIGINT)
{
printf("Ctrl+C detected by server !!\n");
printf("No more connections will be accepted!!");
if(*sock_fd_ptr > 0)
{
close(server_sock_fd);
*sock_fd_ptr = -1;
break;
}
}
else
{
printf("Error handling the signal, SigHandlerThread exiting..\n")
perror("sigwait(&set)");
break;
}
}
return NULL;
}
在 main() 内部:
/*********** Signal Handling *************/
sigset_t set; // declare a set to hold the signals
err = sigfillset(&set); // Fill the set with all the signals
if(!err) perror("sigfillset(&set)");
err = sigthreadmask(SIG_BLOCK, set, NULL); // Block/Mask the signals in the set
if(!err) perror("sigthreadmask(SIG_BLOCK, &set, NULL)");
err = pthread_create(&sig_handler_tid, NULL, Ctrl_C_handler, (void *)&sock_fd);
// Create a thread for handling signals
if(!err) perror("pthread_create");
我错过了什么或做错了什么或理解错误?
PS:我最近开始使用信号进行编程。
操作系统:SLES 10 SP3 x86-64