简体   繁体   English

哪个更适合从 C++、文件描述符上的 write() 或 FILE 上的 fprintf() 写入敏感日志文件?

[英]which is better for writing a sensitive log file from C++, write() on a file descriptor or fprintf() on a FILE?

My understanding is that write() is a system call, which is extremely slow.我的理解是write()是一个系统调用,非常慢。 (It's almost like a kind of inter-process communication.) Anything that works with FILE is buffered and only does a system call every 512 bytes or what have you. (这几乎就像一种进程间通信。)任何与FILE一起工作的东西都会被缓冲,并且每 512 个字节或你有什么系统调用一次。 So speed-wise FILE should be much faster.所以速度方面的FILE应该快得多。

But if you have a log message followed by something that causes a coredump, there is no mechanism I know of whereby the OS could go dig up the FILE 's buffer and write it do disk.但是,如果您有一条日志消息,然后是导致核心转储的内容,那么我不知道操作系统可以通过该机制 go 挖掘FILE的缓冲区并将其写入磁盘。 So, you could easily be missing the last 5-10 lines of your log file.因此,您很容易丢失日志文件的最后 5-10 行。 For software like finance software, where bugs can literally cost billions and you don't want to have any bug twice, and therefore need great logging, that would be bad.对于像金融软件这样的软件,错误可能会花费数十亿美元,而且您不希望任何错误出现两次,因此需要大量的日志记录,那就太糟糕了。

At least that was the stance 25 years ago.至少这是 25 年前的立场。 I'm curious whether things have changed these days?我很好奇这些天情况是否发生了变化? For instance is there a mechanism whereby OS's tell standard C libraries to flush output buffers even after a SEGV or something?例如,是否有一种机制让操作系统告诉标准 C 库即使在 SEGV 或其他东西之后刷新 output 缓冲区? If so then FILE suddenly looks a lot better: speed without the reliability.如果是这样,那么FILE突然看起来好多了:速度没有可靠性。 Plauger in his 1992 book on the Standard C Library mentioned that in theory write() could also be a buffered function of some sort instead of a system call. Plauger 在他 1992 年关于标准 C 库的书中提到,理论上 write() 也可以是某种缓冲的 function,而不是系统调用。

Part of the question is: are there any compilation options on Windows or Linux that affects this tradeoff?部分问题是:Windows 或 Linux 上是否有任何编译选项会影响这种权衡? Not just -O2 , say, but options that actually affect buffer flushing upon core dump?例如,不仅仅是-O2 ,还有在核心转储时实际影响缓冲区刷新的选项?

This isn't a theoretical question into what's allowed, but rather a practical question about today's and upcoming Linux and Windows.这不是关于什么是允许的理论问题,而是关于今天和即将推出的 Linux 和 Windows 的实际问题。 While I know the various standards in question will promise some or another level of reliability or performance, often these are weakened to the point of uselessness as they're trying to cover rare situations like a C interpreter, or code running on an 80s Cray, or something.虽然我知道有问题的各种标准将 promise 某种或其他级别的可靠性或性能,但通常这些标准被削弱到无用的地步,因为它们试图涵盖罕见的情况,例如 C 解释器或在 80 年代 Cray 上运行的代码,或者其他的东西。 Also, while I can (and am) testing this myself, my question is a bit broader than just what today's software gives me with default compilation options.此外,虽然我可以(并且正在)自己测试这个,但我的问题比今天的软件给我的默认编译选项要广泛一些。

Things has not changed.事情没有改变。 CRT and OS kernel still separate kingdoms. CRT 和 OS kernel 仍然是独立的王国。 And there are no such options that would affect this.并且没有这样的选项会影响这一点。 You have to take care yourself with special means.你必须用特殊的方式照顾好自己。 Hint: look into memory mapped files.提示:查看 memory 映射文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM