Advertisement
Caio_25

Jantar Filosofos deadlock

Jun 16th, 2019
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.44 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.  
  13. typedef struct{
  14.    
  15.     int id, garfoesquerda, garfodireita, jantou;
  16. }parametro;
  17.  
  18. int garfo[threads] = { 1, 1, 1, 1, 1};
  19.  
  20. HANDLE mutexVideo;
  21. HANDLE mutexGarfo;
  22.  
  23. void criarMutex();
  24. void setParametros(parametro par[]);
  25. void criarThreads(HANDLE idThread[], DWORD threadIdentifier[], parametro par[]);
  26. void iniciarThreads(HANDLE idThread[]);
  27. void filosofos(void *par);
  28. void pensa(int id);
  29. void pega_garfo_esquerda(int id, int garfoesquerda);
  30. void pega_garfo_direita(int id, int garfodireita);
  31. void come(int id);
  32. void devolver_garfo_esquerda(int id, int garfoesquerda);
  33. void devolver_garfo_direita(int id, int garfodireita);
  34.  
  35.  
  36. int main()
  37. {
  38.         srand(time(NULL));
  39.     parametro par[threads];
  40.     HANDLE idThread[threads];
  41.     DWORD threadIdentifier[threads];
  42.    
  43.        
  44.     criarMutex();
  45.     setParametros(par);
  46.     criarThreads(idThread, threadIdentifier, par);
  47.    
  48.     WaitForSingleObject(mutexVideo, INFINITE);
  49.         cout << "Iniciar threads?" << endl;
  50.     system("pause");
  51.     ReleaseMutex(mutexVideo);
  52.    
  53.     iniciarThreads(idThread);
  54.    
  55.     WaitForMultipleObjects(threads, idThread, TRUE, INFINITE);
  56.     for(int i = 0; i < threads; i++)
  57.     {
  58.         CloseHandle(idThread[i]);
  59.     }
  60. }
  61.  
  62. void criarMutex()
  63. {
  64.     mutexGarfo = CreateMutex(NULL, FALSE, NULL);
  65.     mutexVideo = CreateMutex(NULL, FALSE, NULL);
  66. }
  67.  
  68. void setParametros(parametro par[])
  69. {
  70.     for(int i = 0; i < threads; i++)
  71.     {
  72.         par[i].id = i;
  73.         par[i].garfoesquerda = i;
  74.         par[i].garfodireita = (i + 1) % threads;
  75.         par[i].jantou = 0;
  76.     }
  77.  
  78. }  
  79.  
  80. void criarThreads(HANDLE idThread[], DWORD threadIdentifier[], parametro par[])
  81. {
  82.     for(int i  = 0; i < threads; i++)
  83.     {
  84.         idThread[i] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)filosofos, &par[i], CREATE_SUSPENDED, &threadIdentifier[i]);
  85.     }
  86.    
  87.    
  88.  
  89.    
  90. }
  91.  
  92. void iniciarThreads(HANDLE idThread[])
  93. {
  94.     for(int i = 0; i < threads; i++)
  95.         {
  96.             ResumeThread(idThread[i]);
  97.         }
  98. }
  99.  
  100. void filosofos(void *par)
  101. {
  102.     parametro *filosofopar = (parametro *) par;
  103.    
  104.    
  105.     WaitForSingleObject(mutexVideo, INFINITE);
  106.         printf("Filosofo %d iniciado \n", filosofopar->id );
  107.     ReleaseMutex(mutexVideo);
  108.    
  109.    
  110.     while(1) {
  111.         pensa(filosofopar->id);
  112.         pega_garfo_esquerda(filosofopar->id, filosofopar->garfoesquerda);
  113.         pega_garfo_direita(filosofopar->id, filosofopar->garfodireita);
  114.         come(filosofopar->id);
  115.         devolver_garfo_esquerda(filosofopar->id, filosofopar->garfoesquerda);
  116.         devolver_garfo_direita(filosofopar->id, filosofopar->garfodireita);
  117.        
  118.        
  119.        
  120.     }
  121. }
  122.  
  123.  
  124. void pensa(int id)
  125. {
  126.         srand(time(NULL));
  127.     int t = rand() % 1 + 1;
  128.     WaitForSingleObject(mutexVideo, INFINITE);
  129.        
  130.         printf("filosofo %d vai pensar por %d segundos\n", id, t );
  131.     ReleaseMutex(mutexVideo);
  132.    
  133.     Sleep(t * 1000);
  134. }
  135.  
  136. void pega_garfo_esquerda(int id, int garfoesquerda)
  137. {
  138.     WaitForSingleObject(mutexGarfo, INFINITE);
  139.    
  140.     while(garfo[garfoesquerda] != 1)
  141.     {
  142.        
  143.     }
  144.    
  145.    
  146.         garfo[garfoesquerda] = 0;
  147.     ReleaseMutex(mutexGarfo);
  148.    
  149.    
  150.     WaitForSingleObject(mutexVideo, INFINITE);
  151.         printf("filosofo %d pegou garfo esquerdo %d\n", id, garfoesquerda);
  152.     ReleaseMutex(mutexVideo);
  153.    
  154. }
  155.  
  156. void pega_garfo_direita(int id, int garfodireita)
  157. {
  158.    
  159.     WaitForSingleObject(mutexGarfo, INFINITE);
  160.    
  161.     while(garfo[garfodireita] != 1)
  162.     {
  163.        
  164.     }
  165.    
  166.    
  167.         garfo[garfodireita] = 0;
  168.     ReleaseMutex(mutexGarfo);
  169.    
  170.     WaitForSingleObject(mutexVideo, INFINITE);
  171.         printf("filosofo %d pegou garfo direito %d\n", id, garfodireita);
  172.     ReleaseMutex(mutexVideo);
  173.    
  174. }
  175.  
  176. void come(int id)
  177. {
  178.     srand(time(NULL));
  179.     int t = rand() % 1 + 1;
  180.     WaitForSingleObject(mutexVideo, INFINITE);
  181.         cout << "filosofo " << id << " vai comer por " <<  t <<  "segundos" << endl;
  182.     ReleaseMutex(mutexVideo);
  183.    
  184.     Sleep(t * 1000);
  185. }
  186.  
  187. void devolver_garfo_esquerda(int id, int garfoesquerda)
  188. {
  189.     WaitForSingleObject(mutexGarfo, INFINITE);
  190.         garfo[garfoesquerda] = 1;
  191.     ReleaseMutex(mutexGarfo);
  192.    
  193.     WaitForSingleObject(mutexVideo, INFINITE);
  194.         cout << "filosofo" << id <<  " devolve garfo esquerda " << garfoesquerda  << endl;
  195.        
  196.     ReleaseMutex(mutexVideo);
  197.    
  198. }
  199.  
  200.  
  201. void devolver_garfo_direita(int id, int garfodireita)
  202. {
  203.     WaitForSingleObject(mutexGarfo, INFINITE);
  204.         garfo[garfodireita] = 1;
  205.     ReleaseMutex(mutexGarfo);
  206.    
  207.     WaitForSingleObject(mutexVideo, INFINITE);
  208.         cout << "filosofo" << id <<  " devolve garfo direita " << garfodireita  << endl;
  209.        
  210.     ReleaseMutex(mutexVideo);
  211.    
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement