信号驱动应用程序中的睡眠功能奇怪行为

问题描述 投票:0回答:0

对于我的实时系统类,我必须做一个基于信号的日志记录应用程序。我有一个线程,它在无限循环 (sem_wait) 中等待接收特定信号,然后它应该创建一个转储文件。在主线程中,我使用睡眠函数给我一些时间进行测试,直到我的线程被取消并且应用程序终止(我知道我可以使用 pthread_join 而不是 pthread_cancel - 它仅用于测试)。

显然,当主线程进入睡眠状态并且我收到第二个转储信号时,看起来睡眠结束并且应用程序终止(在第一个信号之后它没问题)。当不使用睡眠功能时,应用程序工作正常。现在我想知道睡眠会发生什么,或者我应该在其他地方寻找错误?

我的应用程序示例代码:

int main(){
   lib_init(); //here are the threads initialized
   sleep(60);
   lib_close(); //there we have pthread_cancel;
}
void dump_handler(){
   sem_post(&sem);
}

void* dump_thread_fun(void*){
   while(1){
   sem_wait(&sem);
   //dumping happens here   
   }
}
c linux signals
© www.soinside.com 2019 - 2024. All rights reserved.