我在尝试处理信号时遇到问题...
我有一个多线程应用程序,它接收一个中断我正在使用的库中的系统调用的信号。经过一番研究,我发现如果一个信号没有被处理,它会被发送到应用程序中的一个随机线程。我已经从我正在使用的库中确认他们没有在他们的应用程序中使用任何信号。我也不是。这是我的主要驱动程序类:
void sig_handler(int signum)
{
cout << "Signal Handle: " << signum << endl;
signal(signum, SIG_IGN);
}
class Driver
{
public:
Driver();
void LaunchLog();
void logonListen();
};
Driver::Driver()
{
pthread_t logThread;
pthread_create(&logThread, NULL, LaunchLogThread, (void*) this);
pthread_detach(logThread);
pthread_t listenThread;
pthread_create(&listenThread, NULL, LaunchListenThread, (void*)this);
pthread_detach(listenThread);
bool running = true;
while(running)
{
//Simple loop to resemble a menu for the console
}
}
void Driver::logonListen()
{
char buffer[256];
int logonPort = BASEPORT;
int sockfd;
struct sockaddr_in serv_addr, cli_addr;
socklen_t clilen;
//initialize socket
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sockfd < 0){
perror("Error opening socket");
exit(1);
}
bzero((char*)&serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(logonPort);
//bind socket to our address
if(bind(sockfd,(struct sockaddr*) &serv_addr, sizeof(serv_addr)) < 0)
{
perror("Error on bind");
exit(1);
}
clilen = sizeof(struct sockaddr_in);
int n;
int nextUser = 2;
char reply[32];
while(1)
{
//Wait for incoming connection on a socket
}
}
void Driver::LaunchLog()
{
Logger::Instance()->WriteToFile();
}
void* LaunchListenThread(void* ptr)
{
Driver* driver = (Driver*) ptr;
driver->logonListen();
}
int main()
{
signal (SIGHUP, sig_handler);
signal (SIGINT, sig_handler);
signal (SIGQUIT, sig_handler);
signal (SIGILL, sig_handler);
signal (SIGTRAP, sig_handler);
signal (SIGFPE, sig_handler);
signal (SIGKILL, sig_handler);
signal (SIGUSR1, sig_handler);
signal (SIGSEGV, sig_handler);
signal (SIGUSR2, sig_handler);
signal (SIGPIPE, sig_handler);
signal (SIGALRM, sig_handler);
signal (SIGTERM, sig_handler);
signal (SIGCHLD, sig_handler);
signal (SIGCONT, sig_handler);
signal (SIGSTOP, sig_handler);
signal (SIGTSTP, sig_handler);
signal (SIGTTIN, sig_handler);
signal (SIGTTOU, sig_handler);
signal (SIGABRT, sig_handler);
Driver driver;
return 0;
}
我无法处理这些信号。中断的系统调用不断增加,我的信号处理程序永远不会被使用。即使我在控制台中按 CTRL+C,程序也会以中断而不是处理 SIGINT 结束。我是否错误地安装了处理程序?
有没有办法处理所有信号并在它们出现时忽略它们?
谢谢