Advertisement
Caio_25

jantar filosofos 3

Jun 16th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.55 KB | None | 0 0
  1. #include<Windows.h>
  2. #include<process.h>
  3. #include<stdio.h>
  4. #include<stdlib.h>
  5. #include<time.h>
  6. #include<iostream>
  7. #include<vector>
  8.  
  9. using namespace std;
  10.  
  11. #define threads 5
  12. #define tempmax 60
  13.  
  14. typedef struct{
  15.    
  16.     int id, garfoesquerda, garfodireita, numgarfos;
  17. }parametro;
  18.  
  19. int garfo[threads] = { 1, 1, 1, 1, 1};
  20. int jantou[threads];
  21.  
  22. HANDLE mutexGarfo[threads];
  23. HANDLE mutexVideo;
  24.  
  25.  
  26. void criarMutex();
  27. void setParametros(parametro par[]);
  28. void criarThreads(HANDLE idThread[], DWORD threadIdentifier[], parametro par[]);
  29. void iniciarThreads(HANDLE idThread[]);
  30. void filosofos(void *par);
  31. void pensa(int id);
  32. void pega_garfo_esquerda(parametro *par);
  33. void pega_garfo_direita(parametro *par);
  34. void come(int id);
  35. void devolver_garfo_esquerda(parametro *par);
  36. void devolver_garfo_direita(parametro *par);
  37.  
  38.  
  39. int main()
  40. {
  41.         srand(time(NULL));
  42.     parametro par[threads];
  43.     HANDLE idThread[threads];
  44.     DWORD threadIdentifier[threads];
  45.    
  46.    
  47.     criarMutex();
  48.     setParametros(par);
  49.     criarThreads(idThread, threadIdentifier, par);
  50.    
  51.     WaitForSingleObject(mutexVideo, INFINITE);
  52.         cout << "Iniciar threads?" << endl;
  53.     system("pause");
  54.     ReleaseMutex(mutexVideo);
  55.    
  56.     iniciarThreads(idThread);
  57.    
  58.     WaitForMultipleObjects(threads, idThread, TRUE, INFINITE);
  59.    
  60.     /*while(1)
  61.     {
  62.         fim = clock();
  63.         tempo = ((double) (fim - inicio)) / CLOCKS_PER_SEC;
  64.        
  65.         if(tempo > 30)
  66.         {
  67.             for(int i = 0; i < threads; i++)
  68.             {
  69.                 TerminateThread(idThread[i], TRUE);
  70.             }
  71.             break;
  72.         }
  73.        
  74.         WaitForSingleObject(mutexVideo, INFINITE);
  75.             printf("%lf", tempo);
  76.         ReleaseMutex(mutexVideo);
  77.     }*/
  78.        
  79.     for(int i = 0; i < threads; i++)
  80.     {
  81.         CloseHandle(idThread[i]);
  82.     }
  83.    
  84.     for(int i = 0; i < threads; i++)
  85.     {
  86.         printf("jantou filosofo %d = %d\n", i, jantou[i]);
  87.     }
  88. }
  89.  
  90. void criarMutex()
  91. {
  92.     for(int i = 0; i < threads; i++)
  93.     {
  94.    
  95.         mutexGarfo[i] = CreateMutex(NULL, FALSE, NULL);
  96.     }
  97.     mutexVideo = CreateMutex(NULL, FALSE, NULL);
  98. }
  99.  
  100. void setParametros(parametro par[])
  101. {
  102.     for(int i = 0; i < threads; i++)
  103.     {
  104.         par[i].id = i;
  105.         par[i].garfoesquerda = i;
  106.         par[i].garfodireita = (i + 1) % threads;
  107.         par[i].numgarfos = 0;
  108.         jantou[i] = 0;
  109.     }
  110.  
  111. }  
  112.  
  113. void criarThreads(HANDLE idThread[], DWORD threadIdentifier[], parametro par[])
  114. {
  115.     for(int i  = 0; i < threads; i++)
  116.     {
  117.         idThread[i] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)filosofos, &par[i], CREATE_SUSPENDED, &threadIdentifier[i]);
  118.     }
  119.    
  120.    
  121.  
  122.    
  123. }
  124.  
  125. void iniciarThreads(HANDLE idThread[])
  126. {
  127.     for(int i = 0; i < threads; i++)
  128.         {
  129.             ResumeThread(idThread[i]);
  130.         }
  131. }
  132.  
  133. void filosofos(void *par)
  134. {
  135.     parametro *filosofopar = (parametro *) par;
  136.     clock_t inicio = clock(), fim;
  137.     double tempo;
  138.    
  139.     WaitForSingleObject(mutexVideo, INFINITE);
  140.         printf("Filosofo %d iniciado \n", filosofopar->id );
  141.     ReleaseMutex(mutexVideo);
  142.    
  143.    
  144.     while(1) {
  145.        
  146.         pensa(filosofopar->id);
  147.         do{
  148.        
  149.         pega_garfo_esquerda(filosofopar);
  150.         if(filosofopar->numgarfos == 1)
  151.         {
  152.             pega_garfo_direita(filosofopar);
  153.         }
  154.         WaitForSingleObject(mutexVideo, INFINITE);
  155.             printf("Filosofo %d numgarfos %d\n", filosofopar->id, filosofopar->numgarfos);
  156.         ReleaseMutex(mutexVideo);
  157.         }while(filosofopar->numgarfos != 2);
  158.         come(filosofopar->id);
  159.         devolver_garfo_esquerda(filosofopar);
  160.         devolver_garfo_direita(filosofopar);
  161.        
  162.         fim = clock();
  163.         tempo = ((double) (fim - inicio)) / CLOCKS_PER_SEC;
  164.         if(tempo > tempmax)
  165.         {
  166.            
  167.             break;
  168.         }
  169.        
  170.         printf("tempo %lf\n", tempo);
  171.     }
  172. }
  173.  
  174.  
  175. void pensa(int id)
  176. {
  177.         srand(time(NULL));
  178.     int t = rand() % 1 + 4;
  179.     WaitForSingleObject(mutexVideo, INFINITE);
  180.        
  181.         printf("filosofo %d vai pensar por %d segundos\n", id, t );
  182.     ReleaseMutex(mutexVideo);
  183.    
  184.     Sleep(t * 1000);
  185. }
  186.  
  187. void pega_garfo_esquerda(parametro *par)
  188. {
  189.     WaitForSingleObject(mutexGarfo[par->id], INFINITE);
  190.    
  191.         if(garfo[par->garfoesquerda] != 1)
  192.         {
  193.            
  194.         }
  195.    
  196.    
  197.         garfo[par->garfoesquerda] = 0;
  198.         par->numgarfos += 1;
  199.        
  200.     ReleaseMutex(mutexGarfo[par->id]);
  201.    
  202.    
  203.     WaitForSingleObject(mutexVideo, INFINITE);
  204.         printf("filosofo %d pegou garfo esquerdo %d\n", par->id, par->garfoesquerda);
  205.     ReleaseMutex(mutexVideo);
  206.    
  207. }
  208.  
  209. void pega_garfo_direita(parametro *par)
  210. {
  211.    
  212.     WaitForSingleObject(mutexGarfo[par->id], INFINITE);
  213.    
  214.     if(garfo[par->garfodireita] != 1)
  215.     {
  216.         devolver_garfo_esquerda(par);
  217.     }
  218.    
  219.     else
  220.     {
  221.         garfo[par->garfodireita] = 0;
  222.         par->numgarfos += 1;
  223.     }
  224.     ReleaseMutex(mutexGarfo[par->id]);
  225.    
  226.     WaitForSingleObject(mutexVideo, INFINITE);
  227.         printf("filosofo %d pegou garfo direito %d\n", par->id, par->garfodireita);
  228.     ReleaseMutex(mutexVideo);
  229.    
  230. }
  231.  
  232. void come(int id)
  233. {
  234.     srand(time(NULL));
  235.     int t = rand() % 1 + 1;
  236.     WaitForSingleObject(mutexVideo, INFINITE);
  237.         cout << "filosofo " << id << " vai comer por " <<  t <<  "segundos" << endl;
  238.     ReleaseMutex(mutexVideo);
  239.     jantou[id] += 1;
  240.     Sleep(t * 1000);
  241. }
  242.  
  243. void devolver_garfo_esquerda(parametro *par)
  244. {
  245.     WaitForSingleObject(mutexGarfo[par->id], INFINITE);
  246.         garfo[par->garfoesquerda] = 1;
  247.         par->numgarfos = par->numgarfos - 1;
  248.     ReleaseMutex(mutexGarfo[par->id]);
  249.    
  250.     WaitForSingleObject(mutexVideo, INFINITE);
  251.         cout << "filosofo" << par->id <<  " devolve garfo esquerda " << par->garfoesquerda  << endl;
  252.        
  253.     ReleaseMutex(mutexVideo);
  254.    
  255. }
  256.  
  257.  
  258. void devolver_garfo_direita(parametro *par)
  259. {
  260.     WaitForSingleObject(mutexGarfo[par->id], INFINITE);
  261.         garfo[par->garfodireita] = 1;
  262.         par->numgarfos = par->numgarfos - 1;
  263.     ReleaseMutex(mutexGarfo[par->id]);
  264.    
  265.     WaitForSingleObject(mutexVideo, INFINITE);
  266.         cout << "filosofo" << par->id <<  " devolve garfo direita " << par->garfodireita  << endl;
  267.        
  268.     ReleaseMutex(mutexVideo);
  269.    
  270. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement