1

我正在尝试隔离一个讨厌的错误,这会导致我的 linux 内核崩溃。我正在向 stderr 打印消息,并且 stderr 被重定向到日志文件。有没有办法禁用文件访问缓冲?当内核挂起时,我会丢失缓冲区中的消息。

4

3 回答 3

1

实际上,stderr默认情况下是无缓冲的,但我认为这只是在 C 运行时方面。我们之前已经解决了这个问题:

fflush (stderr); fsync (fileno (stderr));

(尽管我们实际上这样做了,stdout但适用相同的规则 - 实际fflush可能不是必需的,stderr但它没有害处)。

fflushC 运行时缓冲区刷新到操作系统,fsync强制写入磁盘。

请记住,这可能会严重影响您的表现。

于 2010-05-27T08:35:35.550 回答
0

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

于 2010-05-27T12:59:43.397 回答
0

您可以强制刷新缓冲区,使用fflush(stderr);

于 2010-05-27T08:28:02.780 回答