我正在尝试将有关信号的某些信息保存在文件中,例如它在信号处理程序中被捕获的时间等。
void sig_handler(int signo){
curr_signal = time(NULL);
receivedtime[z] = curr_signal;
signumber = signo;
diff = curr_signal - receivedtime[z-1];
z++;
write(f, &diff, sizeof(diff));
}
我已经更新了我的程序以写入文件 (f)。我的问题是文件中没有写入任何内容。正在创建文件。
最佳答案
小心来自信号处理程序内部的调用。
来自 man 7 signal
:
Async-signal-safe functions
A signal handler function must be very careful, since processing else‐
where may be interrupted at some arbitrary point in the execution of
the program. POSIX has the concept of "safe function". If a signal
interrupts the execution of an unsafe function, and handler either
calls an unsafe function [...],
then the behavior of the program is undefined.
然后是异步信号安全的函数列表。
您的信号处理程序调用:
time()
:好的,它是异步信号安全的fprintf()
:不,不是。
关于c - 将有关信号的数据保存在文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43923513/