Note that there are some explanatory texts on larger screens.

plurals
  1. POcopying large files in c/c++ under FreeBSD freezes system
    text
    copied!<p>this code seems to work under Windows (with unexpected results) and Ubuntu. But when I run it under FreeBSD 9.0 AMD 64 it causes the system to freeze. I get error messages like this:<br> ahcich0: Timeout on slot 28 port 0<br> Does anybody know what the problem could be?<br> Thanks.</p> <pre><code>#include &lt;cmath&gt; #include &lt;cstdlib&gt; #include &lt;sys/time.h&gt; #include &lt;iostream&gt; #include &lt;fstream&gt; #include &lt;string&gt; 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 &lt; FILES + 1; i++){ ofstream os; string filename(FILENAME); filename += (char)i + 48; os.open(filename.c_str(), ios::binary); if (os.is_open()){ cout &lt;&lt; "Writing file " &lt;&lt; i &lt;&lt; " of " &lt;&lt; FILES; long filesize =pow(2.0, i * SIZE_MULTIPLIER); cout &lt;&lt; " (" &lt;&lt; filesize &lt;&lt; " bytes)" &lt;&lt; endl; while(filesize--){ os &lt;&lt; (char)(rand() % 256); } cout &lt;&lt; os.tellp() &lt;&lt; " bytes written.\n"; os.close(); }else{ cerr &lt;&lt; "Could not create file " &lt;&lt; filename; cerr &lt;&lt; endl; } } // copy the files timeval tv; time_t start; char buffer[BUFFER_SIZE]; char ci; for (int i = 0; i &lt; FILES; i++){ ci = (char)i + 49; string filename(FILENAME); filename += ci; string copyname("c"); copyname += COPYNAME; copyname += ci; cout &lt;&lt; "Copying file " &lt;&lt; filename.c_str() &lt;&lt; endl; cout &lt;&lt; "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 &lt;&lt; " Done.\n"; }else{ cerr &lt;&lt; "Could not open either " &lt;&lt; filename; cerr &lt;&lt; " or " &lt;&lt; copyname &lt;&lt; endl; } times[0][i] = time(NULL) - start; remove(copyname.c_str()); copyname = "cpp"; copyname += COPYNAME; copyname += ci; cout &lt;&lt; "the c++ way: "; cout.flush(); start = time(NULL); ifstream in; in.open(filename.c_str(), ios::binary); in.rdbuf()-&gt;pubsetbuf(buffer, BUFFER_SIZE); ofstream out; out.open(copyname.c_str(), ios::binary); char copyBuffer[BUFFER_SIZE]; out.rdbuf()-&gt;pubsetbuf(copyBuffer, BUFFER_SIZE); if (in.is_open() &amp;&amp; out.is_open()){ out &lt;&lt; in.rdbuf(); in.close(); out.close(); cout &lt;&lt; " Done.\n"; }else{ cerr &lt;&lt; "Could not open either " &lt;&lt; filename; cerr &lt;&lt; " or " &lt;&lt; copyname &lt;&lt; endl; } times[1][i] = time(NULL) - start ; remove(copyname.c_str()); } cout &lt;&lt; "Summary:\n"; cout &lt;&lt; "\tc\tc++\n"; for (int i = 0; i &lt; FILES; i++){ ci = (char)i + 49; cout &lt;&lt; "copy" &lt;&lt; ci &lt;&lt; "\t" &lt;&lt; times[0][i]; cout &lt;&lt; "\t" &lt;&lt; times[1][i] &lt;&lt; endl; } return 0; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload