c - 将有关信号的数据保存在文件中?

标签 c linux file signals

我正在尝试将有关信号的某些信息保存在文件中,例如它在信号处理程序中被捕获的时间等。

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/

相关文章:

c - 对函数调用的 undefined reference ?

c++ - 如何在 C 文件中访问 C++ 枚举

linux - 使用 nc 传输大文件

java - PushbackReader 没有 EOF?

c - 使用 C 替换文本文件中的行

c - strptime 不适用于时区格式说明符

objective-c - 设置记录数组数据时要调用的类标识符

linux - 大多数 Linux 应用程序的笨拙外观是 gtk+ 造成的吗?

linux - Linux 中如何从 MSI 中获取文件的真实名称(或者,如何读取 MSI 数据库)?

Java从jar文件导入类