用信号处理函数处理ctrl+C和段错误
C++ #信号处理 #段错误2012-11-09 10:50
在使用C语言测试工具unity测试驱动开发dust的通信模块时,可能遇到人为中断测试或段错误,此时需要加载清理函数,以完成关闭socketFd等操作,但unity的tearDown()函数仅在每个测试用例结束后才加载,因此找到unity / src / unity.c的UnityBegin(),在其前加入信号处理函数,从而达到遇到中断时自动加载清理函数并退出的功能。
当然,需要在unity / src / unity.h中增加下列两个头文件的支持:
#include "signal.h"
#include "stdlib.h"
//add by http://yige.org
void procSigInt(void) {
printf("recv SIGINT or SIGSEGV\n");
tearDown();
UnityEnd();
exit(1);
}
//-----------------------------------------------
void UnityBegin(void) {
//add by bbdlg
signal(SIGINT, procSigInt);
signal(SIGSEGV, procSigInt);
Unity.NumberOfTests = 0;
Unity.TestFailures = 0;
Unity.TestIgnores = 0;
Unity.CurrentTestFailed = 0;
Unity.CurrentTestIgnored = 0;
}相关文章
- Qt中HMAC hash 算法 2012/11/09
- C++ 中KMP字符串匹配算法 2012/11/09
- c/c++编译笔记 2012/11/07