mcr/shared_memories/Producer_Diego.C

00001 #include <sys/types.h>
00002 #include <sys/stat.h>
00003 #include <sys/file.h>
00004 #include <fcntl.h>
00005 
00006 
00007 #include <Riostream.h>
00008 #include <stdio.h>
00009 #include "TH1F.h"
00010 #include "TH2F.h"
00011 #include "TProfile.h"
00012 #include "TRandom.h"
00013 #include "TMapFile.h"
00014 #include "TSystem.h"
00015 #include "TString.h"
00016 
00017 /*
00018 void HandleLockShFile(const Char_t *fnam,Bool_t lock){
00019   // lock file 'fnam' if 'lock' is true
00020   Int_t Fdescr = 0;
00021   cout << endl << " -- -- -- " << endl;
00022   
00023   if(lock){
00024     cout << " -> Locking shared file \"" << fnam << "\"" << endl;
00025     Fdescr = open(fnam,O_CREAT);
00026     Int_t Flock = flock(Fdescr,LOCK_SH);
00027     cout << "     lock output: " << Flock << endl;
00028   }
00029   else{
00030     cout << " -> Unlocking shared file \"" << fnam << "\"" << endl;
00031     Fdescr = open(fnam,O_RDONLY);
00032     Int_t Funlock = flock(Fdescr,LOCK_UN);
00033     cout << "     unlock output: " << Funlock << endl;
00034   }
00035     cout << "      shared file descriptor: " << Fdescr << endl; 
00036 
00037     Int_t FileDescr_close = close(Fdescr);
00038     cout << "File descriptor \"" << Fdescr << "\" closed (close returned: " << FileDescr_close << ")" << endl;
00039 }
00040 */
00041 
00042 void HandleConsumerActions(const Char_t *sh_msg_file, Bool_t allow){
00043   // the content of the shared text file 'shared_msg.txt' is
00044   // used by the consumer(s).
00045   // Consumer(s) can act (open, read...map-files) if 'allow' is true
00046 
00047   TString com = TString("echo ");
00048   Int_t size1 = com.Sizeof()-1;
00049   if(allow) com+="\"go\" > ";
00050   else com+="\"wait\" > ";
00051   com+=sh_msg_file;
00052   gSystem->Exec(com.Data());
00053 
00054   com.Resize(size1);
00055   com+="\"\" >> ";
00056   com+=sh_msg_file;
00057   gSystem->Exec(com.Data());
00058 
00059 }
00060 
00061 void Producer_Diego(){
00062 
00063   TString mnam = TString(gSystem->Getenv("FND_SHR"));
00064   mnam+="/hsimple.map";
00065   TString msgnam = TString(gSystem->Getenv("FND_SHR"));
00066   msgnam+="/shared_msg.txt";
00067 
00068   TString comm1 = TString("touch ");
00069   comm1+=msgnam.Data();
00070   gSystem->Exec(comm1.Data());
00071 
00072   HandleConsumerActions(msgnam.Data(),0); // deny consumer(s) actions
00073   //  TMapFile::SetMapAddress(0x40028000);
00074   // --- remove mapped file
00075   TString command_rm = TString("rm -f ");
00076   command_rm+=mnam.Data();
00077   gSystem->Exec(command_rm.Data());
00078   // --- recreate mapped file
00079   gSystem->Sleep(300); 
00080   TMapFile *mfile = TMapFile::Create(mnam.Data(),"RECREATE", 100000,"Demo memory mapped file");
00081   gSystem->Sleep(300); 
00082   
00083   cout << "mfile is: " << mfile << endl;
00084   mfile->Print();
00085 
00086   TH1F *h1    = new TH1F("h1","Histogram 1",100,-4,4);
00087   h1->SetFillColor(48);
00088   mfile->Update();
00089   HandleConsumerActions(msgnam.Data(),1); // allow consumer(s) actions
00090 
00091   mfile->ls();
00092   
00093   
00094   TString command_touch = TString("touch ");
00095   command_touch+=mnam.Data();
00096   int ev = 0;
00097   while (1) {
00098     cout << "Filling" << endl;
00099     h1->Fill(3);
00100     mfile->Update();
00101     gSystem->Exec(command_touch.Data());
00102     gSystem->ProcessEvents();
00103     mfile->ls();
00104     Char_t a[10];
00105     cout << "press any key to continue (fill and update)" << endl;
00106     cin >> a;
00107     if(!strcmp(a,"q")) break;
00108     ev++;
00109   }
00110   HandleConsumerActions(msgnam.Data(),0); // deny consumer(s) actions
00111   cout << "Closing mapfile" << endl; 
00112   mfile->Close();
00113   cout << "done: setting pointer to zero" << endl; 
00114   mfile = 0;
00115   cout << "done." << endl; 
00116 
00117 }
00118 

Generated on Tue Oct 16 15:40:47 2007 by  doxygen 1.5.2