Advertisement
AbraaoAllysson

codigooABraao

Apr 12th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <thread>
  3. #include <fstream>
  4. #include <string.h>
  5. #include <dirent.h>
  6. #include <sstream>
  7. #include <vector>
  8. #include <thread>
  9. /*g++ -std=c++11 -pthread main.cpp */
  10. using namespace std;
  11.  
  12. void write(vector<string> filenames);
  13. void readFile(string filename);
  14. void* readFiles(char directory[], vector<string> *filenames);
  15.  
  16. volatile int lock = 0;
  17. int size =0;
  18.  
  19. class Requisition
  20. {
  21. string strGet_m;
  22. string strReq_m;
  23. public:
  24. Requisition(string strGet, string strReq)
  25. {
  26. this->strGet_m = strGet;
  27. this->strReq_m = strReq;
  28. }
  29. string getStrGet()
  30. {
  31. return strGet_m;
  32. }
  33. string getStrReq()
  34. {
  35. return strReq_m;
  36. }
  37. };
  38. /*void readReqs()
  39. {
  40. ifstream myFile(filename);
  41. cout << "\n\n\n*****************\n\n\n"<<endl;
  42. if(!myFile.is_open())
  43. {
  44. cout << "\n\n Unable to open file \n\n"<<endl;
  45. exit(0);
  46.  
  47. }
  48.  
  49. else
  50. {
  51. myFile.seekg(0,myFile.end);
  52. long size = myFile.tellg();
  53. myFile.seekg (0);
  54. char* buffer = new char[size];
  55. myFile.read(buffer,size);
  56. myFile.close();
  57. cout << "size is: " << size << " bytes.\n"<<endl;
  58. //writeReqs(filename,size);
  59. }
  60. } */
  61.  
  62. void readAnsw(string filename, string reqname)
  63. {
  64.  
  65. ifstream myFile(("/home/abraao/abraao/LP2/trabalho_2-Sublime/files/" +filename), ifstream::binary);
  66. cout << "\n\n\n*****************\n\n\n"<<endl;
  67. reqname.replace((reqname.size()-4), reqname.length(), "");
  68. // cout << filename << " " << reqname <<endl;
  69.  
  70. if(!myFile.is_open())
  71. {
  72. cout << "\n\n Unable to open file \n\n"<<endl;
  73. exit(0);
  74.  
  75. }
  76.  
  77. else
  78. {
  79.  
  80.  
  81. /* granvando na pasta*/
  82. string arqAll;
  83.  
  84. ofstream myfileWrite(( "/home/abraao/abraao/LP2/trabalho_2-Sublime/answ/" + reqname + ".ans") );
  85. while ( !myFile.eof() )
  86. {
  87. getline (myFile, arqAll);
  88. myfileWrite << arqAll;
  89. myfileWrite << "\n";
  90. // cout<<"ddfkdskjjdskfkjds" <<endl;
  91. }
  92.  
  93.  
  94. myFile.close();
  95. size += myfileWrite.tellp();
  96. /*int size = myfileWrite.tellp();
  97. cout << "size is: " << size << " bytes.\n"<<endl; */
  98. myfileWrite.close();
  99.  
  100.  
  101. //cout << "size is: " << size << " bytes.\n"<<endl;
  102. //write(string filename);
  103. }
  104.  
  105. }
  106.  
  107.  
  108. void* readFiles(char directory[], vector<string> *filenames)
  109. {
  110.  
  111. DIR *dir = NULL;
  112. struct dirent *drnt = NULL;
  113. string *filesArr, str;
  114. int aux = 0;
  115. int tamanho;
  116. dir = opendir(directory);
  117. char *diretorio = NULL;
  118. if(dir)
  119. {
  120.  
  121. while(drnt = readdir(dir)){aux++;}
  122. filesArr = new string[aux+1];
  123. aux = 0;
  124. seekdir(dir, 0);
  125. int tamanho;
  126. while(drnt = readdir(dir))
  127. {
  128.  
  129. str = (string)directory + "/" + (string)drnt->d_name;
  130. diretorio = (char *) malloc(sizeof(char)*30);
  131. strcpy(diretorio,drnt->d_name);
  132. tamanho = strlen(diretorio);
  133. if((diretorio[tamanho-4]=='.')&&(diretorio[tamanho-3]=='t')&&(diretorio[tamanho-2]=='x')&&(diretorio[tamanho-1]=='t'))
  134. {
  135. filenames->push_back( (string)drnt->d_name );
  136.  
  137. }
  138. if((diretorio[tamanho-4]=='.')&&(diretorio[tamanho-3]=='j')&&(diretorio[tamanho-2]=='p')&&(diretorio[tamanho-1]=='g'))
  139. {
  140. filenames->push_back( (string)drnt->d_name );
  141.  
  142. }
  143. if((diretorio[tamanho-4]=='.')&&(diretorio[tamanho-3]=='p')&&(diretorio[tamanho-2]=='n')&&(diretorio[tamanho-1]=='g'))
  144. {
  145. filenames->push_back( (string)drnt->d_name );
  146.  
  147. }
  148. if((diretorio[tamanho-4]=='.')&&(diretorio[tamanho-3]=='r')&&(diretorio[tamanho-2]=='e')&&(diretorio[tamanho-1]=='q'))
  149. {
  150. filenames->push_back( (string)drnt->d_name );
  151.  
  152. }
  153.  
  154.  
  155.  
  156. }
  157.  
  158. }
  159. else
  160. {
  161.  
  162. cout << "Não foi possível abrir essa pasta " << directory << "\n";
  163.  
  164. }
  165.  
  166.  
  167. closedir(dir);
  168.  
  169. }
  170.  
  171.  
  172. void write(vector<string> filenames)
  173. {
  174. ofstream myFile;
  175. for(int i = 0; i < filenames.size(); i++)
  176. {
  177. stringstream indice;
  178. indice << i;
  179. string s1;
  180. s1 = indice.str();
  181.  
  182. string strFile = "/home/abraao/abraao/LP2/trabalho_2-Sublime/reqs/req" + s1 + ".req";
  183. myFile.open(strFile);
  184. myFile << "GET " + filenames[i];
  185. //cout << "Saida:" << strFile << endl;
  186. myFile.close();
  187. }
  188. }
  189.  
  190.  
  191. vector <Requisition> readReqs(vector<string> filenamesReqs, vector<string> filenames)
  192. {
  193.  
  194. vector <Requisition> vR;
  195. string strGet,strReq,strFile;
  196. int i=0;
  197. for (int i = 0; i < filenamesReqs.size(); ++i)
  198. {
  199. strFile = "/home/abraao/abraao/LP2/trabalho_2-Sublime/reqs/" + filenamesReqs[i];
  200. //cout<< strFile <<endl;
  201. ifstream myfile(strFile);
  202. while (getline( myfile, strGet, ' ') )
  203. {
  204.  
  205. getline ( myfile, strReq);
  206. //cout << "GET: " << strGet <<endl;;
  207. // getline(myfile, strReq) ;
  208. //cout << "requisition " << strGet <<" ---"<< strReq << " FIM"<<endl;
  209. vR.push_back(Requisition(strGet,strReq ));
  210.  
  211. }
  212. myfile.close();
  213.  
  214. }
  215.  
  216. return vR;
  217.  
  218.  
  219.  
  220. }
  221.  
  222. void _unlock()
  223. {
  224. __sync_synchronize();
  225. lock = 0;
  226. }
  227.  
  228. void _lock(volatile int lock2)
  229. {
  230. while(__sync_lock_test_and_set(&lock2, 1));
  231. }
  232.  
  233.  
  234. volatile int arrive[10] = {0,0,0,0,0,0,0,0,0,0};
  235. volatile int proceed[10] = {0,0,0,0,0,0,0,0,0,0};
  236.  
  237. void* monitor(int NUM_THREADS)
  238. {
  239. cout<< "FUNCAO --- [MONITOR] --- inicializando monitor..."<<endl;
  240. for (int i=0;i<NUM_THREADS;i++)
  241. {
  242. while(arrive[i]==0);
  243. arrive[i] = 0;
  244. }
  245. for (int i=0;i<NUM_THREADS;i++) {
  246. __sync_fetch_and_add(&proceed[i],1);
  247. printf("[MONITOR] valor de proceed [%d] = %d - alterado por thread monitor\n",i,proceed[i]);
  248. }
  249.  
  250.  
  251. }
  252.  
  253. void openFile(std::vector<Requisition> vR, vector<string> filenames,vector<string> filenamesReqs, int i)
  254. {
  255. int p = i + 1;
  256. _lock(lock);
  257. int c = 0;
  258. cout<<" executando funcao tarefas - thread= "<< p <<endl;
  259. while (c<10) c++;
  260. __sync_fetch_and_add(&arrive[p],1);
  261. cout << " valor de arrive " << p <<" "<< arrive[p] <<" alterado por thread " << endl;
  262.  
  263. for (int j = 0; j < filenames.size(); ++j)
  264. {
  265. if(vR[i].getStrGet()=="GET")
  266. {
  267. if(vR[i].getStrReq()== filenames[j])
  268. {
  269. //cout<< "i= " <<i << "j= " << j <<vR[i].getStrGet()<< "FIM" <<endl;
  270. //cout<< "i= " <<i << "j= " << j <<vR[i].getStrReq()<< "FIM" <<endl;
  271. readAnsw( filenames[j], filenamesReqs[i]);
  272. break;
  273. }
  274. }
  275.  
  276. else
  277. {
  278. cout<< i << " " << j <<"ERROR BAD COMMAND" <<endl;
  279. break;
  280. }
  281.  
  282. }
  283.  
  284. while(proceed[p]==0);
  285. cout<< p << "sou a thread num= " << p << "e estou na barreira" << endl;
  286.  
  287. cout<< p << "sou a thread num= " << p << " e passei da barreira" << endl;
  288.  
  289. proceed[p] = 0;
  290.  
  291. _unlock();
  292. }
  293.  
  294.  
  295.  
  296. int main(int argc, char const *argv[])
  297. {
  298. /* code */
  299. //readFile("/home/abraao/abraao/LP2/Trabalho2/files/Homem-de-Ferro-1920x1080.jpg");
  300. char files[] = "files";
  301. vector<string> filenames,filenamesReqs;
  302. readFiles(files,&filenames);
  303. /*for (int i = 0; i <filenames.size(); ++i)
  304. {
  305. cout<< filenames[i] <<endl;
  306. } */
  307. char reqs[] = "reqs";
  308. readFiles(reqs,&filenamesReqs);
  309. /*for (int i = 0; i <filenamesReqs.size(); ++i)
  310. {
  311. cout <<filenamesReqs[i] <<endl;
  312. }*/
  313. cout <<"\n********************"<<endl;
  314. vector<Requisition> vR = readReqs(filenamesReqs, filenames);
  315. /*for (int i = 0; i <vR.size(); ++i)
  316. {
  317. cout << vR[i].getStrGet() << " " <<vR[i].getStrReq()<< " dasas" << endl;
  318. }*/
  319.  
  320. //openFile(vR, filenames,filenamesReqs);
  321.  
  322. thread *threadReqs = new thread[vR.size()];
  323. thread threadMonitor;
  324. threadMonitor = thread(monitor,vR.size());
  325.  
  326. //cout<< "Processando" << vR.size() <<"Requisition";;
  327. for (int i = 0; i < vR.size(); ++i)
  328. {
  329. threadReqs[i] = thread(openFile, vR,filenames,filenamesReqs, i);
  330. // threadReqs[i].join();
  331.  
  332. }
  333.  
  334. threadMonitor.join();
  335. for(int j = 0; j < vR.size(); j++)
  336. {
  337.  
  338. threadReqs[j].join();
  339. }
  340. cout<< "\n Numeros de Bytes processado " << size << endl;
  341.  
  342.  
  343. //openFile(vR,filenames);
  344.  
  345.  
  346. //readReqs(filename[0]);
  347. // Constructs the new thread and runs it. Does not block execution.
  348. //vector<thread> vT1;
  349. //vT1.push_back( thread(r, files, &filenames) );
  350. // Makes the main thread wait for the new thread to finish execution, therefore blocks its own execution.
  351. //vT1[0].join();
  352.  
  353.  
  354.  
  355. return 0;
  356. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement