Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <thread>
- #include <fstream>
- #include <string.h>
- #include <dirent.h>
- #include <sstream>
- #include <vector>
- #include <thread>
- /*g++ -std=c++11 -pthread main.cpp */
- using namespace std;
- void write(vector<string> filenames);
- void readFile(string filename);
- void* readFiles(char directory[], vector<string> *filenames);
- volatile int lock = 0;
- int size =0;
- class Requisition
- {
- string strGet_m;
- string strReq_m;
- public:
- Requisition(string strGet, string strReq)
- {
- this->strGet_m = strGet;
- this->strReq_m = strReq;
- }
- string getStrGet()
- {
- return strGet_m;
- }
- string getStrReq()
- {
- return strReq_m;
- }
- };
- /*void readReqs()
- {
- ifstream myFile(filename);
- cout << "\n\n\n*****************\n\n\n"<<endl;
- if(!myFile.is_open())
- {
- cout << "\n\n Unable to open file \n\n"<<endl;
- exit(0);
- }
- else
- {
- myFile.seekg(0,myFile.end);
- long size = myFile.tellg();
- myFile.seekg (0);
- char* buffer = new char[size];
- myFile.read(buffer,size);
- myFile.close();
- cout << "size is: " << size << " bytes.\n"<<endl;
- //writeReqs(filename,size);
- }
- } */
- void readAnsw(string filename, string reqname)
- {
- ifstream myFile(("/home/abraao/abraao/LP2/trabalho_2-Sublime/files/" +filename), ifstream::binary);
- cout << "\n\n\n*****************\n\n\n"<<endl;
- reqname.replace((reqname.size()-4), reqname.length(), "");
- // cout << filename << " " << reqname <<endl;
- if(!myFile.is_open())
- {
- cout << "\n\n Unable to open file \n\n"<<endl;
- exit(0);
- }
- else
- {
- /* granvando na pasta*/
- string arqAll;
- ofstream myfileWrite(( "/home/abraao/abraao/LP2/trabalho_2-Sublime/answ/" + reqname + ".ans") );
- while ( !myFile.eof() )
- {
- getline (myFile, arqAll);
- myfileWrite << arqAll;
- myfileWrite << "\n";
- // cout<<"ddfkdskjjdskfkjds" <<endl;
- }
- myFile.close();
- size += myfileWrite.tellp();
- /*int size = myfileWrite.tellp();
- cout << "size is: " << size << " bytes.\n"<<endl; */
- myfileWrite.close();
- //cout << "size is: " << size << " bytes.\n"<<endl;
- //write(string filename);
- }
- }
- void* readFiles(char directory[], vector<string> *filenames)
- {
- DIR *dir = NULL;
- struct dirent *drnt = NULL;
- string *filesArr, str;
- int aux = 0;
- int tamanho;
- dir = opendir(directory);
- char *diretorio = NULL;
- if(dir)
- {
- while(drnt = readdir(dir)){aux++;}
- filesArr = new string[aux+1];
- aux = 0;
- seekdir(dir, 0);
- int tamanho;
- while(drnt = readdir(dir))
- {
- str = (string)directory + "/" + (string)drnt->d_name;
- diretorio = (char *) malloc(sizeof(char)*30);
- strcpy(diretorio,drnt->d_name);
- tamanho = strlen(diretorio);
- if((diretorio[tamanho-4]=='.')&&(diretorio[tamanho-3]=='t')&&(diretorio[tamanho-2]=='x')&&(diretorio[tamanho-1]=='t'))
- {
- filenames->push_back( (string)drnt->d_name );
- }
- if((diretorio[tamanho-4]=='.')&&(diretorio[tamanho-3]=='j')&&(diretorio[tamanho-2]=='p')&&(diretorio[tamanho-1]=='g'))
- {
- filenames->push_back( (string)drnt->d_name );
- }
- if((diretorio[tamanho-4]=='.')&&(diretorio[tamanho-3]=='p')&&(diretorio[tamanho-2]=='n')&&(diretorio[tamanho-1]=='g'))
- {
- filenames->push_back( (string)drnt->d_name );
- }
- if((diretorio[tamanho-4]=='.')&&(diretorio[tamanho-3]=='r')&&(diretorio[tamanho-2]=='e')&&(diretorio[tamanho-1]=='q'))
- {
- filenames->push_back( (string)drnt->d_name );
- }
- }
- }
- else
- {
- cout << "Não foi possível abrir essa pasta " << directory << "\n";
- }
- closedir(dir);
- }
- void write(vector<string> filenames)
- {
- ofstream myFile;
- for(int i = 0; i < filenames.size(); i++)
- {
- stringstream indice;
- indice << i;
- string s1;
- s1 = indice.str();
- string strFile = "/home/abraao/abraao/LP2/trabalho_2-Sublime/reqs/req" + s1 + ".req";
- myFile.open(strFile);
- myFile << "GET " + filenames[i];
- //cout << "Saida:" << strFile << endl;
- myFile.close();
- }
- }
- vector <Requisition> readReqs(vector<string> filenamesReqs, vector<string> filenames)
- {
- vector <Requisition> vR;
- string strGet,strReq,strFile;
- int i=0;
- for (int i = 0; i < filenamesReqs.size(); ++i)
- {
- strFile = "/home/abraao/abraao/LP2/trabalho_2-Sublime/reqs/" + filenamesReqs[i];
- //cout<< strFile <<endl;
- ifstream myfile(strFile);
- while (getline( myfile, strGet, ' ') )
- {
- getline ( myfile, strReq);
- //cout << "GET: " << strGet <<endl;;
- // getline(myfile, strReq) ;
- //cout << "requisition " << strGet <<" ---"<< strReq << " FIM"<<endl;
- vR.push_back(Requisition(strGet,strReq ));
- }
- myfile.close();
- }
- return vR;
- }
- void _unlock()
- {
- __sync_synchronize();
- lock = 0;
- }
- void _lock(volatile int lock2)
- {
- while(__sync_lock_test_and_set(&lock2, 1));
- }
- volatile int arrive[10] = {0,0,0,0,0,0,0,0,0,0};
- volatile int proceed[10] = {0,0,0,0,0,0,0,0,0,0};
- void* monitor(int NUM_THREADS)
- {
- cout<< "FUNCAO --- [MONITOR] --- inicializando monitor..."<<endl;
- for (int i=0;i<NUM_THREADS;i++)
- {
- while(arrive[i]==0);
- arrive[i] = 0;
- }
- for (int i=0;i<NUM_THREADS;i++) {
- __sync_fetch_and_add(&proceed[i],1);
- printf("[MONITOR] valor de proceed [%d] = %d - alterado por thread monitor\n",i,proceed[i]);
- }
- }
- void openFile(std::vector<Requisition> vR, vector<string> filenames,vector<string> filenamesReqs, int i)
- {
- int p = i + 1;
- _lock(lock);
- int c = 0;
- cout<<" executando funcao tarefas - thread= "<< p <<endl;
- while (c<10) c++;
- __sync_fetch_and_add(&arrive[p],1);
- cout << " valor de arrive " << p <<" "<< arrive[p] <<" alterado por thread " << endl;
- for (int j = 0; j < filenames.size(); ++j)
- {
- if(vR[i].getStrGet()=="GET")
- {
- if(vR[i].getStrReq()== filenames[j])
- {
- //cout<< "i= " <<i << "j= " << j <<vR[i].getStrGet()<< "FIM" <<endl;
- //cout<< "i= " <<i << "j= " << j <<vR[i].getStrReq()<< "FIM" <<endl;
- readAnsw( filenames[j], filenamesReqs[i]);
- break;
- }
- }
- else
- {
- cout<< i << " " << j <<"ERROR BAD COMMAND" <<endl;
- break;
- }
- }
- while(proceed[p]==0);
- cout<< p << "sou a thread num= " << p << "e estou na barreira" << endl;
- cout<< p << "sou a thread num= " << p << " e passei da barreira" << endl;
- proceed[p] = 0;
- _unlock();
- }
- int main(int argc, char const *argv[])
- {
- /* code */
- //readFile("/home/abraao/abraao/LP2/Trabalho2/files/Homem-de-Ferro-1920x1080.jpg");
- char files[] = "files";
- vector<string> filenames,filenamesReqs;
- readFiles(files,&filenames);
- /*for (int i = 0; i <filenames.size(); ++i)
- {
- cout<< filenames[i] <<endl;
- } */
- char reqs[] = "reqs";
- readFiles(reqs,&filenamesReqs);
- /*for (int i = 0; i <filenamesReqs.size(); ++i)
- {
- cout <<filenamesReqs[i] <<endl;
- }*/
- cout <<"\n********************"<<endl;
- vector<Requisition> vR = readReqs(filenamesReqs, filenames);
- /*for (int i = 0; i <vR.size(); ++i)
- {
- cout << vR[i].getStrGet() << " " <<vR[i].getStrReq()<< " dasas" << endl;
- }*/
- //openFile(vR, filenames,filenamesReqs);
- thread *threadReqs = new thread[vR.size()];
- thread threadMonitor;
- threadMonitor = thread(monitor,vR.size());
- //cout<< "Processando" << vR.size() <<"Requisition";;
- for (int i = 0; i < vR.size(); ++i)
- {
- threadReqs[i] = thread(openFile, vR,filenames,filenamesReqs, i);
- // threadReqs[i].join();
- }
- threadMonitor.join();
- for(int j = 0; j < vR.size(); j++)
- {
- threadReqs[j].join();
- }
- cout<< "\n Numeros de Bytes processado " << size << endl;
- //openFile(vR,filenames);
- //readReqs(filename[0]);
- // Constructs the new thread and runs it. Does not block execution.
- //vector<thread> vT1;
- //vT1.push_back( thread(r, files, &filenames) );
- // Makes the main thread wait for the new thread to finish execution, therefore blocks its own execution.
- //vT1[0].join();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement