Commit 394ad96c by Francois Gygi

Change to C library calls to use fsync()


git-svn-id: http://qboxcode.org/svn/qb/trunk@1912 cba15fb0-1239-40c8-b417-11db7ca47a34
parent 72577540
...@@ -15,7 +15,9 @@ ...@@ -15,7 +15,9 @@
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <vector>
#include <cstdlib> #include <cstdlib>
#include <unistd.h> // stat()
#include <time.h> #include <time.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
...@@ -47,18 +49,22 @@ int main(int argc, char** argv) ...@@ -47,18 +49,22 @@ int main(int argc, char** argv)
cout << " qb lock file: " << lockfilename[i] << endl; cout << " qb lock file: " << lockfilename[i] << endl;
} }
ofstream qb_infile[2]; vector<FILE *> qb_infile(ns);
ifstream qb_outfile[2]; ifstream qb_outfile[2];
// send commands to servers to execute the init.i script // send commands to servers to execute the init.i script
for ( int i = 0; i < ns; i++ ) for ( int i = 0; i < ns; i++ )
{ {
wait_for_file(lockfilename[i]); wait_for_file(lockfilename[i]);
qb_infile[i].open(qb_infilename[i].c_str(),ios_base::trunc); qb_infile[i] = fopen(qb_infilename[i].c_str(),"w");
qb_infile[i] << "init.i" << endl; //qb_infile[i].open(qb_infilename[i].c_str(),ios_base::trunc);
fprintf(qb_infile[i],"init.i\n");
//qb_infile[i] << "init.i" << endl;
cout << " sent init.i cmd to server " << i << endl; cout << " sent init.i cmd to server " << i << endl;
qb_infile[i].close(); fclose(qb_infile[i]);
sync(); //qb_infile[i].close();
fsync(fileno(qb_infile[i]));
//sync();
remove(lockfilename[i].c_str()); remove(lockfilename[i].c_str());
cout << " lock file of server " << i << " removed" << endl; cout << " lock file of server " << i << " removed" << endl;
} }
...@@ -127,33 +133,48 @@ int main(int argc, char** argv) ...@@ -127,33 +133,48 @@ int main(int argc, char** argv)
double dz = amplitude * (2.0*drand48()-1.0); double dz = amplitude * (2.0*drand48()-1.0);
// prepare next commands // prepare next commands
// send next command to server // send next commands to server
// write all commands into ostringstream os
ostringstream os;
os << "move C by " << dx << " " << dy << " " << dz << endl;
os << "run 0 30" << endl;
// write ostringstream to file
qb_infile[i] = fopen(qb_infilename[i].c_str(),"w");
fprintf(qb_infile[i],"%s",os.str().c_str());
fclose(qb_infile[i]);
fsync(fileno(qb_infile[i]));
qb_infile[i].open(qb_infilename[i].c_str(),ios_base::trunc);
qb_infile[i] << "move C by " << dx << " " << dy << " " << dz << endl;
qb_infile[i] << "run 0 30" << endl;
qb_infile[i].close();
sync();
remove(lockfilename[i].c_str()); remove(lockfilename[i].c_str());
cout << " lock file " << i << " removed" << endl; cout << " lock file " << i << " removed" << endl;
} }
usleep(100000); usleep(100000);
} }
// send quit command to all servers
for ( int i = 0; i < ns; i++ ) for ( int i = 0; i < ns; i++ )
{ {
wait_for_file(lockfilename[i]); wait_for_file(lockfilename[i]);
qb_infile[i].open(qb_infilename[i].c_str(),ios_base::trunc); qb_infile[i] = fopen(qb_infilename[i].c_str(),"w");
qb_infile[i] << "quit" << endl; fprintf(qb_infile[i],"quit\n");
qb_infile[i].close();
sync();
cout << " sent quit cmd to server " << i << endl; cout << " sent quit cmd to server " << i << endl;
fclose(qb_infile[i]);
fsync(fileno(qb_infile[i]));
remove(lockfilename[i].c_str()); remove(lockfilename[i].c_str());
cout << " lock file " << i << " removed" << endl; cout << " lock file " << i << " removed" << endl;
} }
return 0; return 0;
} }
////////////////////////////////////////////////////////////////////////////////
void sendCmd(const char *filename, const char *str)
{
FILE *fp = fopen(filename,"w");
fprintf(fp,str);
fclose(fp);
fsync(fileno(fp));
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void wait_for_file(string& lockfilename) void wait_for_file(string& lockfilename)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment