在我的main.c文件中,我有以下代码。一切正常,但是当我使用 ctrl + c 创建信号时;执行暂停在sleep(1), intHandler 函数工作,然后程序在sleep(1)行中给出分段错误。
任何人都知道应该导致什么?我无法在RunMe()中执行最后两行,因此我无法正确关闭打开的套接字和线程。
volatile sig_atomic_t keepRunning = 1;
void intHandler(int exitCode)
{
keepRunning = 0;
}
int RunMe()
{
// some unrelated code runs in here
// preperation of sockets and threads etc.
while (keepRunning == 1)
{
printf("KR:%d\n",keepRunning);
sleep(1); // here produces the segmentation fault.
if (iLogger.stateRequested == ITRUE)
{
printState(&fp);
iLogger.stateRequested = IFALSE;
}
}
// both two below closes my sockets and threads
iLogger.Destroy();
fp.RemoveAll(&fp);
}
int main(int argc, char *argv [])
{
signal(SIGINT, intHandler);
RunMe();
}