Advertisement
Caio_25

Jantar Filosofos starvation

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