【发布时间】:2019-05-10 06:53:45
【问题描述】:
目标:将This line must be printed写入日志文件mib_log_test,以防程序由于某些奇怪的原因挂起/卡住。
为简单起见,写了一个C程序如下:
#include <stdio.h>
#include <stdlib.h>
#define FILE_NAME "./mib_log_test"
FILE *fp = NULL;
int main()
{
fp = fopen(FILE_NAME, "w+");
if (fp == NULL) {
fprintf(stderr, "Unable to open %s file", FILE_NAME);
exit(EXIT_FAILURE);
}
fprintf(fp, "This line must be printed\n");
while(1);
return 0;
}
上述程序在编译运行后,永远不会因为无限循环而自行终止。所以我必须按ctrl + c 来终止它。与ctrl + c 我没有看到This line must be printed 被写入我的日志文件(mib_log_test)
如果我如下所示覆盖默认的 SIGINT 处理程序,This line must be printed 将写入我的日志文件 (mib_log_test)。
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#define FILE_NAME "./mib_log_test"
FILE *fp = NULL;
void sigint_handler(int sig_num)
{
exit(EXIT_FAILURE);
}
int main()
{
fp = fopen(FILE_NAME, "w+");
if (fp == NULL) {
fprintf(stderr, "Unable to open %s file", FILE_NAME);
exit(EXIT_FAILURE);
}
signal(SIGINT, sigint_handler);
fprintf(fp, "This line must be printed\n");
while(1);
return 0;
}
问题:在上述情况下,什么默认的 SIGINT 处理程序会导致不写入日志消息?
【问题讨论】: