在系统崩溃之前写入文件 [英] writing to a file prior to system crash

查看:87
本文介绍了在系统崩溃之前写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在调试一个C程序的问题导致计算机发生
崩溃,我正试图在

崩溃之前立即记录信息发生。我使用我的BKprintLog函数(见下文)将

信息写入日志文件。问题是,我不相信我写入日志的
信息会在发生崩溃之前保存到硬盘驱动器上。


我对硬盘和操作系统的理解是因为硬盘驱动器很慢,所以当你写入硬盘驱动器时,这些信息就是这样的。

进入缓冲区,控制权返回给程序,然后

信息在不久的将来保存到永久存储器中。这个

应用程序在Windows上运行。


有没有办法保证在我的函数返回之前,有什么东西

写到硬盘实际上是在硬盘上吗?


我得到的数据似乎支持我的担忧,因为根据

日志,每次都会在不同的地方发生崩溃,而且有时,日志文件甚至没有写入任何内容

在崩溃之前。

ben


void BKprintLog(char * line){

FILE * file;

const char * FILENAME =" ; C:\\log_file.txt" ;;

SYSTEMTIME systime;


GetSystemTime(& systime);

file = fopen(FILENAME," a +");

fprintf(文件,"%2d:%02d:%06.3lf ::

%s \\ \\ n,systime.wHour,systime.wMinute,systime.wSecon d + systime.wMilliseconds / 1000.0,line);

fclose(file);

}

I''m debugging an issue with a C program that causes the computer to
crash, and I''m attempting to log information immediately before the
crash occurs. I us my BKprintLog function (see below) to write
information into a log file. The problem is, i''m not confident that
information i write to the log gets saved onto the hard drive before
the crash occurs.

My understanding of hard drives and OS are that because hard drives are
so slow, that when you write to the harddrive, that information is put
into a buffer, control is returned to the program, and then the
information is saved to permanent storage in the near future. This
application is running on Windows.

Is there any way to guarantee that before my function returns, anything
written to the hard drive is Actually on the hard drive?

The data I''m getting seems to support my concern, as according to the
logs, the crash is occuring at different places each time, and
sometimes, the log file doesn''t even get anything written into it
before a crash.
ben

void BKprintLog(char * line) {
FILE * file;
const char * FILENAME = "C:\\log_file.txt";
SYSTEMTIME systime;

GetSystemTime(&systime);
file = fopen(FILENAME,"a+");
fprintf(file, "%2d:%02d:%06.3lf ::
%s\n",systime.wHour,systime.wMinute,systime.wSecon d+systime.wMilliseconds/1000.0,line);
fclose(file);
}

推荐答案

是***** ************ @ gmail.com 写道:

我正在调试一个C程序的问题导致计算机崩溃,我试图在发生

崩溃之前立即记录信息。
I''m debugging an issue with a C program that causes the computer to
crash, and I''m attempting to log information immediately before the
crash occurs.



许多操作系统(包括Windows)为

同步文件I / O提供了便利。在适合您的操作系统的新闻组中提出您的问题,您实际上可能有机会获得答案。

Many operating systems (including Windows) provide facilities for
synchronous file I/O. Ask your question in a newsgroup appropriate to
your OS and you may actually stand a chance of getting answers.


2006年10月17日14: 30:43-0700,成为************ *****@gmail.com 写在

comp.lang.c:
On 17 Oct 2006 14:30:43 -0700, be*****************@gmail.com wrote in
comp.lang.c:

我正在调试一个C程序的问题导致计算机崩溃,并且我试图在发生

崩溃之前立即记录信息。我使用我的BKprintLog函数(见下文)将

信息写入日志文件。问题是,我不相信我写入日志的
信息会在崩溃发生之前保存到硬盘驱动器上。
I''m debugging an issue with a C program that causes the computer to
crash, and I''m attempting to log information immediately before the
crash occurs. I us my BKprintLog function (see below) to write
information into a log file. The problem is, i''m not confident that
information i write to the log gets saved onto the hard drive before
the crash occurs.



您编程崩溃的事实电脑,几乎可以肯定

意味着程序本身会产生某种未定义的

行为。

The fact that you program "crashes" the computer, almost certainly
means that the program itself produces some sort of undefined
behavior.


My对硬盘驱动器和操作系统的理解是因为硬盘驱动器这么慢,当你写入硬盘驱动器时,将这些信息放入缓冲区,控制权返回到该程序,然后

信息在不久的将来保存到永久存储。这个

应用程序在Windows上运行。
My understanding of hard drives and OS are that because hard drives are
so slow, that when you write to the harddrive, that information is put
into a buffer, control is returned to the program, and then the
information is saved to permanent storage in the near future. This
application is running on Windows.



C标准完全没有规定硬盘驱动器,或任何其他物理设备的b
。输入和输出被抽象为FILE *

流。与实际磁盘或其他设备的所有接口完全取决于底层平台。

The C standard specifies nothing at all about hard drives, or any
other physical device. The input and output is abstracted into FILE *
streams. All the interface with the actual disks or other devices is
completely up to the underlying platform.


有没有办法保证之前我的函数返回,任何写入硬盘的
实际上都在硬盘上?
Is there any way to guarantee that before my function returns, anything
written to the hard drive is Actually on the hard drive?



C语言肯定没有办法做到这一点。

There is certainly no way defined by the C language to do this.


数据我'似乎支持我的关注,因为根据

日志,每次都会在不同的地方发生崩溃,有时候,b / b
,日志文件没有甚至在碰撞之前写入任何东西


The data I''m getting seems to support my concern, as according to the
logs, the crash is occuring at different places each time, and
sometimes, the log file doesn''t even get anything written into it
before a crash.


void BKprintLog(char * line){

FILE * file;

const char * FILENAME =" C:\\log_file.txt";

SYSTEMTIME systime;


GetSystemTime(& systime);

file = fopen(FILENAME," a +");

fprintf(file,"%2d:%02d:%06.3lf ::

%s \ n",systime.wHour,systime.wMinute,systime.wSecon d + systime.wMilliseconds / 1000.0,line);

fclose(file);

}
void BKprintLog(char * line) {
FILE * file;
const char * FILENAME = "C:\\log_file.txt";
SYSTEMTIME systime;

GetSystemTime(&systime);
file = fopen(FILENAME,"a+");
fprintf(file, "%2d:%02d:%06.3lf ::
%s\n",systime.wHour,systime.wMinute,systime.wSecon d+systime.wMilliseconds/1000.0,line);
fclose(file);
}



即使您的日志记录功能包含非标准函数和数据

类型。

事实中还存在一个潜在的标准C库问题,你不会检查fopen()是否成功。如果没有,那么你的指针''文件''是NULL,并将它传递给fprintf()会导致

未定义的行为,并可能导致你的问题。 />

如果有一个系统特定的扩展可以满足您的需求,那么您需要在平台特定的组中询问它。我会

建议新闻:comp.os.ms-windows.programmer.win32作为一个好的。


-

Jack Klein

主页: http://JK-Technology.Com

常见问题解答

comp.lang.c http://c-faq.com/

comp.lang.c ++ http://www.parashift.com/c++-faq-lite/

alt.comp.lang.learn.c- c ++
http:// www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html

Even your logging function contains non standard functions and data
types. There''s also a potential standard C library problem in the
fact that you don''t check to see if the fopen() succeeded. If it does
not, your pointer ''file'' is NULL, and passing it to fprintf() causes
undefined behavior and could be causing your problem.

If there is a system specific extension that does what you want, you
would need to ask in a platform specific groups about it. I would
suggest news:comp.os.ms-windows.programmer.win32 as a good one.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html



是******** @ gmail.com 写道:

我正在调试一个C程序的问题导致计算机发生b
崩溃,我正在尝试在发生

崩溃之前立即记录信息。
I''m debugging an issue with a C program that causes the computer to
crash, and I''m attempting to log information immediately before the
crash occurs.



对于像这样的实时情况,C标准主要是妈妈。


你说的文件往往是正确的要缓冲,所以你可能看不到实际文件中的所有内容。


通常它有助于写入没有缓冲的stderr。和/或

将stderr重定向到一个串口,这个口很少被缓存在那个

级别。或者在每次写入后调用fflush。


更好,如果可能的话,在更稳定的平台上运行程序,

如LInux或Windows NT--具有记忆保护的系统。

The C standard is mostly mum about real-time situations like this.

You''re correct that files tend to be buffered, so you may not see
everything in the actual file.

often it helps to write to stderr, which is not buffered. and/or
redirect stderr to a serial port, which are rarely buffered at that
level. or call fflush after each write.

better yet, run your program, if possible, on a more stable platform,
like LInux or Windows NT-- a system with memory protection.


这篇关于在系统崩溃之前写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆