用 C 语言创建光盘映像(iso 9660)非常慢
作为一个有趣的项目,我想我应该编写一个程序来制作 iso 文件。据我所知它可以工作,但每 30 秒只能读取 4KB。我使用弹出 -x 11 将我的 cdrom 驱动器减慢到合理的速度。如果没有它,驱动器就会全速运行并很快终止进程。任何使这更快/更好的建议将不胜感激。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define BUFFSIZE 4092
int main(int argc, char **argv)
{
FILE *fp = fopen("/dev/cdrom", "r");
FILE *file = fopen(strcat(argv[1], ".iso"), "w");
printf("Copying...\n");
while(!feof(fp))
{
char *line=(char *)malloc(sizeof(char) * BUFFSIZE);
fgets(line, BUFFSIZE, fp);
fprintf(file, "%s",line);
free(line);
}//end while
fclose(fp);
fclose(file);
printf("Done!\n");
return 0;
}//end main
As a fun project I thought I'd write a program to make iso files. As far as I can tell it works, but it reads only 4KB every 30 seconds. I used eject -x 11 to slow my cdrom drive to a reasonable speed. Without it the drive runs at full speed and kills the process pretty quickly. Any suggestions to make this faster/better will be much appreciated.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define BUFFSIZE 4092
int main(int argc, char **argv)
{
FILE *fp = fopen("/dev/cdrom", "r");
FILE *file = fopen(strcat(argv[1], ".iso"), "w");
printf("Copying...\n");
while(!feof(fp))
{
char *line=(char *)malloc(sizeof(char) * BUFFSIZE);
fgets(line, BUFFSIZE, fp);
fprintf(file, "%s",line);
free(line);
}//end while
fclose(fp);
fclose(file);
printf("Done!\n");
return 0;
}//end main
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
fgets()
处理文本并且是面向行的,并且浪费时间寻找换行符。此外,它和fprintf()
不处理NUL
字节,并且可能会被它们弄得很混乱。您想要使用二进制 IO,即fread()
和fwrite()
。也不需要不断地free()
并重新分配缓冲区。如果您想使用 Unix IO 原语而不是 C 包装器,则可以使用
read()
和write()
或mmap()
。fgets()
handles text and is line oriented, and wastes time looking for newlines. Furthermore, it andfprintf()
don't handleNUL
bytes, and can get very confused by them. You want to use binary IO, i.e.fread()
andfwrite()
. There is also no need to continuallyfree()
and reallocate your buffer.If you want to use Unix IO primitives rather than the C wrappers, you could use
read()
andwrite()
ormmap()
instead.在这种情况下, fgets 也不是
扫描输入中的换行符。看
进入 mmap。
取消/重新分配会减慢你的速度。
this scenario, neither is fgets which
scans the input for a newline. Look
into mmap.
de/re-allocation slows you down.
我不是 C 专家,但听起来您需要实现某种形式的数据缓冲读取器和写入器才能看到性能改进。
I am no C guru, but sounds like you need to implement some form of buffered readers and writers of data to see performance improvements.
首先,我不会使用格式化输入(
fgets
和文本模式),而是使用原始二进制输入(fopen
和b
> 标志,fread
和fwrite
用于写入)。这样,stdio 不需要执行文本模式所需的替换(如果您的平台需要的话),并且您有一系列固定大小的读取,其性能应该比等待的fgets
' 更好。\n
。然后,我会摆脱动态内存分配;连续的分配/释放可能会降低应用程序的性能。只需为堆栈上的所有静态缓冲区(例如 8192 字节)分配一次并一直使用它。
First of all, I wouldn't go with formatted input (
fgets
& text mode), but with raw binary input (fopen
with theb
flag,fread
andfwrite
for writing). This way stdio doesn't need to perform the substitutions needed for the text mode (if needed on your platform), and you have a series of fixed-size reads that should perform better thanfgets
' waiting for the\n
.Then, I would get rid of that dynamic memory allocation; the continuous allocation/deallocation is probably killing the performance of your application. Just allocate once for all a static buffer on the stack (e.g. 8192 bytes) and use it all the time.
将
malloc
和free
移到循环之外。Move the
malloc
andfree
outside the loop.