我正在尝试隔离一个讨厌的错误,这会导致我的 linux 内核崩溃。我正在向 stderr 打印消息,并且 stderr 被重定向到日志文件。有没有办法禁用文件访问缓冲?当内核挂起时,我会丢失缓冲区中的消息。
3 回答
实际上,stderr
默认情况下是无缓冲的,但我认为这只是在 C 运行时方面。我们之前已经解决了这个问题:
fflush (stderr); fsync (fileno (stderr));
(尽管我们实际上这样做了,stdout
但适用相同的规则 - 实际fflush
可能不是必需的,stderr
但它没有害处)。
将fflush
C 运行时缓冲区刷新到操作系统,fsync
强制写入磁盘。
请记住,这可能会严重影响您的表现。
You can try to use setvbuf
when starting your app
setvbuf(stderr, NULL, _IONBF, 0);
However, you will get read of the stdio buffer, but still have the "in kernel" buffer problem, that won't disappear unless you fsync. However may be tracking a kernel bug from userspace isn't the best way to go at it.
Can you use a serial console and get the output on another machine ? This way you could get both the oops and the stderr messages
您可以强制刷新缓冲区,使用fflush(stderr);