快速文件复制并取得进展

发布于 2024-09-26 21:57:34 字数 330 浏览 8 评论 0原文

我正在为 Linux 编写一个 SDL 应用程序,它可以从控制台运行(无 X 服务器)。我拥有的一个功能是文件复制机制,它将特定文件从 HDD 复制到 USB 闪存设备,并在 UI 中显示此复制的进度。为此,我使用简单的 while 循环并按 8kB 块复制文件来获取复制进度。问题是,它很慢。我在近 10 分钟内复制了一个 100 MB 的文件,这是不可接受的。

如何实现更快的文件复制?我正在考虑一些异步 API,它将文件从 HDD 读取到缓冲区,并在单独的线程中将数据存储到 USB,但我不知道是否应该自己实现这个,因为它看起来不是一件容易的任务。也许你知道一些可以帮助我的 C++ API/库?或者也许还有其他更好的方法?

I'm writing an SDL application for Linux, that works from the console (no X server). One function I have is a file copy mechanism, that copies specific files from HDD to USB Flash device, and showing progress of this copy in the UI. To do this, I'm using simple while loop and copying file by 8kB chunks to get copy progress. The problem is, that it's slow. I get to copy a 100 MB file in nearly 10 minutes, which is unacceptable.

How can I implement faster file copy? I was thinking about some asynchronous API that would read file from HDD to a buffer and store the data to USB in separate thread, but I don't know if I should implement this myself, because it doesn't look like an easy task. Maybe you know some C++ API/library that can that for me? Or maybe some other, better method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

嗼ふ静 2024-10-03 21:57:34

不要与复制进度同步更新您的 UI,这会大大减慢速度。您应该在与主 UI 线程不同的单独线程上运行文件复制,以便文件复制可以尽可能快地进行,而不会影响应用程序的响应能力。然后,UI 可以以自然速率(例如以显示器的刷新速率)进行自我更新。

您还应该使用大于 8 KB 的缓冲区大小。尝试一下,但我认为使用更大的缓冲区大小(例如在 64-128 KB 范围内)您会获得更快的结果。

因此,它可能看起来像这样:

#define BUFSIZE (64*1024)

volatile off_t progress, max_progress;

void *thread_proc(void *arg)
{
    // Error checking omitted for expository purposes
    char buffer[BUFSIZE];
    int in = open("source_file", O_RDONLY);
    int out = open("destination_file", O_WRONLY | O_CREAT | O_TRUNC);

    // Get the input file size
    struct stat st;
    fstat(in, &st);

    progress = 0;
    max_progress = st.st_size;

    ssize_t bytes_read;
    while((bytes_read = read(in, buffer, BUFSIZE)) > 0)
    {
        write(out, buffer, BUFSIZE);
        progress += bytes_read;
    }

    // copy is done, or an error occurred
    close(in);
    close(out);

    return 0;
}

void start_file_copy()
{
    pthread_t t;
    pthread_create(&t, NULL, &thread_proc, 0);
}

// In your UI thread's repaint handler, use the values of progress and
// max_progress

请注意,如果您将文件发送到套接字而不是另一个文件,则应该使用sendfile(2) 系统调用,直接在内核空间复制文件,无需往返用户空间。当然,如果您这样做,您将无法获得任何进度信息,因此这可能并不总是理想的。

对于 Windows 系统,您应该使用 CopyFileEx,它既高效又为您提供进度回调例程。

Don't synchronously update your UI with the copy progress, that will slow things down considerably. You should run the file copy on a separate thread from the main UI thread so that the file copy can proceed as fast as possible without impeding the responsiveness of your application. Then, the UI can update itself at the natural rate (e.g. at the refresh rate of your monitor).

You should also use a larger buffer size than 8 KB. Experiment around, but I think you'll get faster results with larger buffer sizes (e.g. in the 64-128 KB range).

So, it might look something like this:

#define BUFSIZE (64*1024)

volatile off_t progress, max_progress;

void *thread_proc(void *arg)
{
    // Error checking omitted for expository purposes
    char buffer[BUFSIZE];
    int in = open("source_file", O_RDONLY);
    int out = open("destination_file", O_WRONLY | O_CREAT | O_TRUNC);

    // Get the input file size
    struct stat st;
    fstat(in, &st);

    progress = 0;
    max_progress = st.st_size;

    ssize_t bytes_read;
    while((bytes_read = read(in, buffer, BUFSIZE)) > 0)
    {
        write(out, buffer, BUFSIZE);
        progress += bytes_read;
    }

    // copy is done, or an error occurred
    close(in);
    close(out);

    return 0;
}

void start_file_copy()
{
    pthread_t t;
    pthread_create(&t, NULL, &thread_proc, 0);
}

// In your UI thread's repaint handler, use the values of progress and
// max_progress

Note that if you are sending a file to a socket instead of another file, you should instead use the sendfile(2) system call, which copies the file directly in kernel space without round tripping into user space. Of course, if you do that, you can't get any progress information, so that may not always be ideal.

For Windows systems, you should use CopyFileEx, which is both efficient and provides you a progress callback routine.

戏剧牡丹亭 2024-10-03 21:57:34

让操作系统完成所有工作:

  1. 将文件映射到内存:mmap,将大大提高速度提升阅读过程。
  2. 使用 msync 将其保存到文件中。

Let the OS do all the work:

  1. Map the file to memory: mmap, will drastically speed up the reading process.
  2. Save it to a file using msync.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文