c++ - 在 FreeBSD 下用 c/c++ 复制大文件会卡住系统

标签 c++ freebsd

此代码似乎在 Windows(具有意外结果)和 Ubuntu 下工作。但是当我在 FreeBSD 9.0 AMD 64 下运行它时,它会导致系统卡住。我收到这样的错误消息:
ahcich0:插槽 28 端口 0 超时
有人知道问题出在哪里吗?
谢谢。

#include <cmath>
#include <cstdlib>
#include <sys/time.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main(int argc, char *argv[])
{
    const string FILENAME = "testfile";
    const string COPYNAME = "copy";
    const int FILES = 5;
    const int SIZE_MULTIPLIER = 6;
    const int BUFFER_SIZE = pow(2.0, 16);

    time_t times[2][FILES];

    srand (time(NULL));

    // create test files
    for (int i = 1; i < FILES + 1; i++){
        ofstream os;
        string filename(FILENAME);
        filename += (char)i + 48;
        os.open(filename.c_str(), ios::binary);
        if (os.is_open()){
            cout << "Writing file " << i << " of " << FILES;
            long filesize =pow(2.0, i * SIZE_MULTIPLIER);
            cout << " (" << filesize << " bytes)" <<  endl;

            while(filesize--){
                os << (char)(rand() % 256);
            }
            cout << os.tellp() << " bytes written.\n";
            os.close();
        }else{
            cerr << "Could not create file " << filename;
            cerr << endl;
        }
    }

    // copy the files
    timeval tv;
    time_t start;
    char buffer[BUFFER_SIZE];
    char ci;
    for (int i = 0; i < FILES; i++){
        ci = (char)i + 49;
        string filename(FILENAME);
        filename += ci;
        string copyname("c");
        copyname += COPYNAME;
        copyname += ci;

        cout << "Copying file " << filename.c_str() << endl;

        cout << "the c way: "; 
        cout.flush();

        start = time(NULL);

        FILE *pFile = fopen(filename.c_str(), "rb");
        FILE *pCopy = fopen(copyname.c_str(), "wb");
        if (!(pFile == NULL || pCopy == NULL)){
            do{
                int bytesRead = fread(
                    buffer, 1, BUFFER_SIZE, pFile);

                fwrite(buffer, 1, bytesRead, pCopy);
            }while(!feof(pFile));
            fclose(pFile);
            fclose(pCopy);

            cout << " Done.\n";
        }else{
            cerr << "Could not open either " << filename;
            cerr << " or " << copyname << endl;
        }

        times[0][i] = time(NULL) - start;
        remove(copyname.c_str());

        copyname = "cpp";
        copyname += COPYNAME;
        copyname += ci;

        cout << "the c++ way: ";
        cout.flush();

        start = time(NULL);

        ifstream in;
        in.open(filename.c_str(), ios::binary);
        in.rdbuf()->pubsetbuf(buffer, BUFFER_SIZE);
        ofstream out;
        out.open(copyname.c_str(), ios::binary);
        char copyBuffer[BUFFER_SIZE];
        out.rdbuf()->pubsetbuf(copyBuffer, BUFFER_SIZE);

        if (in.is_open() && out.is_open()){
            out << in.rdbuf();
            in.close();
            out.close();
            cout << " Done.\n";
        }else{
            cerr << "Could not open either " << filename;
            cerr << " or " << copyname << endl;
        }

        times[1][i] = time(NULL) - start ;
        remove(copyname.c_str());
    }

    cout << "Summary:\n";
    cout << "\tc\tc++\n";
    for (int i = 0; i < FILES; i++){
        ci = (char)i + 49;
        cout << "copy" << ci << "\t" << times[0][i];
        cout << "\t" << times[1][i] << endl;
    }

    return 0;
}

最佳答案

将 FILES 更改为 4 后(因为否则会花费很长时间),您的程序在这里运行得很好:

Writing file 1 of 4 (64 bytes)
64 bytes written.
Writing file 2 of 4 (4096 bytes)
4096 bytes written.
Writing file 3 of 4 (262144 bytes)
262144 bytes written.
Writing file 4 of 4 (16777216 bytes)
16777216 bytes written.
Copying file testfile1
the c way:  Done.
the c++ way:  Done.
Copying file testfile2
the c way:  Done.
the c++ way:  Done.
Copying file testfile3
the c way:  Done.
the c++ way:  Done.
Copying file testfile4
the c way:  Done.
the c++ way:  Done.
Summary:
        c       c++
copy1   0       0
copy2   0       0
copy3   0       0
copy4   0       0

(FreeBSD 9.0-RELEASE-p3 amd64,用clang++编译)

关于c++ - 在 FreeBSD 下用 c/c++ 复制大文件会卡住系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11657830/

相关文章:

php - 如何同步本地目录和远程ftp目录?

svn - svn 的默认提交消息存储在哪里?

c++ - 如何从 C++ 中的文本文件中获取特定行?

c++ - 滚动一个非常大的 GtkDrawingArea

c++ - 使用 C++ 查找特定软件是否安装在 Windows 中的最合适方法是什么?

Vim 重新映射 F10 键

c - 尝试阅读 cp.c 代码但不理解

ruby - 在 FreeBSD 上安装 gosu

c++ - 根据模板枚举参数更改行为

c++ - 为什么指向 NULL 字符的指针未转换为 false