用 C 语言创建光盘映像(iso 9660)非常慢

发布于 2024-10-28 06:53:17 字数 702 浏览 2 评论 0原文

作为一个有趣的项目,我想我应该编写一个程序来制作 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

我一向站在原地 2024-11-04 06:53:17

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 and fprintf() don't handle NUL bytes, and can get very confused by them. You want to use binary IO, i.e. fread() and fwrite(). There is also no need to continually free() and reallocate your buffer.

If you want to use Unix IO primitives rather than the C wrappers, you could use read() and write() or mmap() instead.

层林尽染 2024-11-04 06:53:17
  • 缓冲 IO 几乎不适合
    在这种情况下, fgets 也不是
    扫描输入中的换行符。看
    进入 mmap
  • 连续缓冲
    取消/重新分配会减慢你的速度。
  • Fprintf 不适合写入二进制数据。它也很慢。
  • Buffered IO is hardly appropriate in
    this scenario, neither is fgets which
    scans the input for a newline. Look
    into mmap.
  • Continuous buffer
    de/re-allocation slows you down.
  • Fprintf is not right for writing binary data. It's also slow.
你没皮卡萌 2024-11-04 06:53:17

我不是 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.

有深☉意 2024-11-04 06:53:17

首先,我不会使用格式化输入(fgets 和文本模式),而是使用原始二进制输入(fopenb > 标志,freadfwrite 用于写入)。这样,stdio 不需要执行文本模式所需的替换(如果您的平台需要的话),并且您有一系列固定大小的读取,其性能应该比等待的 fgets' 更好。 \n

然后,我会摆脱动态内存分配;连续的分配/释放可能会降低应用程序的性能。只需为堆栈上的所有静态缓冲区(例如 8192 字节)分配一次并一直使用它。

First of all, I wouldn't go with formatted input (fgets & text mode), but with raw binary input (fopen with the b flag, fread and fwrite 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 than fgets' 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.

倾`听者〃 2024-11-04 06:53:17

mallocfree 移到循环之外。

Move the malloc and free outside the loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文