打开与在C程序的fopen(多线程) [英] open versus fopen in C program (with multi-threading)

查看:1221
本文介绍了打开与在C程序的fopen(多线程)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于未知的原因,似乎FOPEN无法打开文本文件时它被关闭之前调用重复次数。

For an unknown reason, it seems that fopen fails to open text files when it is called repeated times before closing.

我的C程序使用多线程,每个线程处理一种类型的输出文本文件(此处为十每型),每种类型在一个分离的文件夹中。我需要为了写大量的数据,以十个分量这些文件中较长的执行,并在同一个线程中打开。

My C program uses multithreading, and each thread handles one type of output text files (eleven per type), each type in a separated folder. I need to mantain these files opened during a long execution and at the same thread in order to write a lot of data.

要更好地解释它,这个过程是这样的:

To explain it better, the process is the following:

1线程#1开始并创建和一个文件夹中写道:11档。

1- Thread #1 starts and creates and writes 11 files in one folder.

2 - 不关闭在不同的文件夹previous文件,线程#2开始,然后创建并写入另一个11个文件。

2- Without closing previous files, Thread #2 starts and creates and writes another 11 files in a different folder.

3与其他两个线程一样的。

3- The same with other two Threads.

4-最后,当所有需要的已创建和文件中的所有线程都完成,除非主要的一个,所有的文件被关闭。

4- In the end, when all the files needed have been created and all the Threads have finished unless the main one, all the files are closed.

然而,令人惊讶的,开放的是做能够处理所有这文件在同一时间。是否有人有一个关于这个问题有FOPEN小费?我不知道这是否是与多线程或文件的最大数量的fopen可以同时处理相关

However and surprisingly, open is do able to handle all this files at the same time. Does anybody have a tip about this issue with fopen? I don't know if it is related with the multi-threading or with a max number of files that fopen can handle at the same time.

我在Windows平台上用Borland的编译器。

I am in Windows platform with Borland compiler.

推荐答案

C运行时(CRT)库是线程从Windows XP及以上安全。另外,如果你相信一些在CRT的意见,包括文件,它有20打开文件的限制。

The C Run Time (CRT) library is thread safe from Windows XP and above. Also, if you believe some of the comments in the CRT include files, it has a limit of 20 open files.

所以,在你的应用程序。什么是单线程的原因有11个文件打开?你是不会被同时写入他们获得可观的性能增益。事实上,你可以使情况变得更糟,因为C盘将捶打,因为它绕着试图写入每个文件。

So, in your application. What is the reason for a single thread to have 11 files open? You are NOT going to get a measurable performance gain by writing them simultaneously. In fact, you could make it worse because the C drive will thrash as it moves around trying to write each file.

根据您的应用程序如何设计:打开一个文件,写它满为止,关闭它,打开第二个文件,等等。

Depending on how your application is designed: open one file, write to it until full, close it, open second file, etc.

现在,乘以4盘抖动的可能,因为每个线程试图做I / O相同的C盘!所以,如果序列化可能的磁盘访问。得到真正的改善增加了缓冲区的大小。

Now, multiply the possibility of disk thrashing by four as each thread is trying to do I/O to the same C drive! So, serialize disk accesses if possible. to get a real improvement increase the buffer sizes.

这篇关于打开与在C程序的fopen(多线程)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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