Advertisement
Caio_25

Garçom/Cliente

Jun 16th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.39 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 r 30//número de rodadas
  12. #define x 4//número de garçons
  13. #define c 13//número de clientes
  14. #define k 2//capacidade dos garçons
  15.  
  16. typedef struct{
  17.     bool clienteEsperaPedido;
  18.     int id;
  19. }parametroCliente;
  20.  
  21. typedef struct{
  22.     int id;
  23. }parametroGarcon;
  24.  
  25. HANDLE mutexGarcon[x];
  26. HANDLE mutexVideo;
  27. HANDLE mutexRodada;
  28. HANDLE eventCliente[c];
  29. HANDLE mutexCriar;
  30.  
  31.  
  32. int numeroPedidos[x] = {0};
  33. int matrizCliente[x][k]; // gardar os ids dos clientes que pediram para determinado garcon
  34. int topo[x] = {0};
  35. int rodadas = 0;
  36.  
  37. void CriarThreadsGarcon(HANDLE idThread[], parametroGarcon parametro[], DWORD ThreadIdentifier[]);
  38. void CriarThreadsClientes(HANDLE idThread[], parametroCliente parametro[], DWORD ThreadIdentifier[]);
  39.  
  40.  
  41. void IniciarThreads(HANDLE idThread[], int n);
  42. void FecharThread(HANDLE idThread[], int n);
  43.  
  44. void criarMutex();
  45. void criarEvent();
  46.  
  47. void fazPedido(parametroCliente *par);
  48. void esperaPedido(int id);
  49. void recebePedido(bool *clienteEspera, int id);
  50. void consomePedido(int id);
  51. void cliente(void *parametro);
  52.  
  53. void recebePedidoMaximo(int idgarcon);
  54. void registraPedido(int idgarcon);
  55. void entregaPedido(int idgarcon);
  56. void garcon(void *parametro);
  57. bool fechoubar();
  58.  
  59.  
  60. int main()
  61. {
  62.     srand(12);
  63.  
  64.     DWORD clienteIdentifier[c];
  65.     HANDLE idCliente[c];
  66.     parametroCliente parCli[c];
  67.  
  68.  
  69.     DWORD garconIdentifier[x];
  70.     HANDLE idGarcon[x];
  71.     parametroGarcon parGar[x];
  72.  
  73.     criarMutex();
  74.     criarEvent();
  75.  
  76.     printf("Criando threads clientes\n");
  77.     CriarThreadsClientes(idCliente, parCli, clienteIdentifier);
  78.  
  79.     printf("Criando threads Garcom\n");
  80.     CriarThreadsGarcon(idGarcon, parGar, garconIdentifier);
  81.  
  82.     printf("Inciar threads ? ");
  83.     system("pause");
  84.  
  85.     WaitForSingleObject(mutexCriar, INFINITE);
  86.         IniciarThreads(idGarcon, x);
  87.     ReleaseMutex(mutexCriar);
  88.  
  89.     WaitForSingleObject(mutexCriar, INFINITE);
  90.         IniciarThreads(idCliente, c);
  91.     ReleaseMutex(mutexCriar);
  92.  
  93.     WaitForMultipleObjects(c, idCliente, TRUE, INFINITE);
  94.     WaitForMultipleObjects(x, idGarcon, TRUE, INFINITE);
  95.  
  96.     FecharThread(idCliente, c);
  97.     FecharThread(idGarcon, x);
  98.  
  99.  
  100.     return 0;
  101. }
  102.  
  103.  
  104. void CriarThreadsClientes(HANDLE idThread[], parametroCliente parametro[], DWORD ThreadIdentifier[])
  105. {
  106.     for(int i = 0; i < c; i++)
  107.     {
  108.         parametro[i].id = i;
  109.         idThread[i] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)cliente, &parametro[i], CREATE_SUSPENDED, &ThreadIdentifier[i]);
  110.  
  111.     }
  112. }
  113.  
  114. void CriarThreadsGarcon(HANDLE idThread[], parametroGarcon parametro[], DWORD ThreadIdentifier[])
  115. {
  116.     for(int i = 0; i < x; i++)
  117.     {
  118.         parametro[i].id = i;
  119.         idThread[i] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)garcon, &parametro[i], CREATE_SUSPENDED, &ThreadIdentifier[i]);
  120.  
  121.     }
  122. }
  123.  
  124. void IniciarThreads(HANDLE idThread[], int n)
  125. {
  126.         for(int i = 0; i < n; i++)
  127.         {
  128.             ResumeThread(idThread[i]);
  129.         }
  130. }
  131.  
  132. void FecharThread(HANDLE idThread[], int n)
  133. {
  134.     for(int i = 0; i < n; i++)
  135.     {
  136.  
  137.         CloseHandle(idThread[i]);
  138.  
  139.     }
  140. }
  141.  
  142.  
  143. void criarMutex()
  144. {
  145.     mutexVideo = CreateMutex(NULL, FALSE, NULL);
  146.     mutexRodada = CreateMutex(NULL, FALSE, NULL);
  147.     mutexCriar = CreateMutex(NULL, FALSE, NULL);
  148.  
  149.     for(int i = 0; i < x; i++)
  150.     {
  151.  
  152.         mutexGarcon[i]= CreateMutex(NULL, FALSE, NULL);
  153.     }
  154. }
  155.  
  156. void criarEvent()
  157. {
  158.     for(int i = 0; i < c; i++)
  159.     {
  160.          eventCliente[i] = CreateEvent(NULL, TRUE, FALSE, NULL);
  161.     }
  162.  
  163.  
  164. }
  165.  
  166. void fazPedido(parametroCliente *par)
  167. {
  168.  
  169.     int gar;
  170.  
  171.     WaitForSingleObject(mutexVideo, INFINITE);
  172.         cout << "Cliente " << par->id <<  " faz pedido" << endl;
  173.     ReleaseMutex(mutexVideo);
  174.  
  175.  
  176.     WaitForSingleObject(mutexRodada, INFINITE);
  177.         do{
  178.             gar = rand() % x;
  179.         }while(topo[gar] >= k);
  180.         topo[gar] += 1;
  181.     ReleaseMutex(mutexRodada);
  182.  
  183.     WaitForSingleObject(mutexVideo, INFINITE);
  184.         cout << "Cliente " << par->id <<  " pede ao " << " garcom " << gar << endl;
  185.        
  186.     ReleaseMutex(mutexVideo);
  187.  
  188.     WaitForSingleObject(mutexGarcon[gar], INFINITE); //Apenas um cliente pede por vez a um único garçom
  189.         numeroPedidos[gar] += 1;
  190.         matrizCliente[gar][numeroPedidos[gar] - 1] = par->id;
  191.         par->clienteEsperaPedido = true;
  192.     ReleaseMutex(mutexGarcon[gar]);
  193.  
  194.  
  195.  
  196. }
  197.  
  198. void esperaPedido(int id)
  199. {
  200.     WaitForSingleObject(mutexVideo, INFINITE);
  201.         cout << "Cliente " << id <<  " espera pedido" << endl;
  202.     ReleaseMutex(mutexVideo);
  203.  
  204.     WaitForSingleObject(eventCliente[id], INFINITE);
  205. }
  206.  
  207. void recebePedido(bool *clienteEspera, int id)
  208. {
  209.     WaitForSingleObject(mutexVideo, INFINITE);
  210.         cout << "Cliente " << id <<  " recebe pedido" << endl;
  211.     ReleaseMutex(mutexVideo);
  212.     *clienteEspera = false;
  213. }
  214.  
  215. void consomePedido(int id)
  216. {
  217.     WaitForSingleObject(mutexVideo, INFINITE);
  218.         cout << "Cliente " << id <<  " consome pedido" << endl;
  219.     ReleaseMutex(mutexVideo);
  220.  
  221.     Sleep(1800 + rand() % 500);
  222. }
  223.  
  224. void cliente(void *parametro)
  225. {
  226.     parametroCliente *par = (parametroCliente *) parametro;
  227.  
  228.        
  229.         WaitForSingleObject(mutexVideo, INFINITE);
  230.             printf("ClientThread %d iniciada\n", par->id);
  231.         ReleaseMutex(mutexVideo);
  232.  
  233.         while(!fechoubar()){
  234.            
  235.             fazPedido(par);
  236.             esperaPedido(par->id);
  237.             recebePedido(&(par->clienteEsperaPedido), par->id);
  238.             consomePedido(par->id);
  239.            
  240.            
  241.         }
  242.  
  243. }
  244.  
  245. void recebePedidoMaximo(int idgarcon)
  246. {
  247.     WaitForSingleObject(mutexVideo, INFINITE);
  248.             printf("Gar %d  recebe Pedido Maximo\n",idgarcon );
  249.         ReleaseMutex(mutexVideo);
  250.  
  251.     while(numeroPedidos[idgarcon] < k && fechoubar() != true)
  252.     {
  253.        
  254.     }
  255.    
  256. }
  257.  
  258. void registraPedido(int idgarcon)
  259. {
  260.     WaitForSingleObject(mutexGarcon[idgarcon], INFINITE); // por enquanto ninguem mais pede a este garcon
  261.    
  262.     WaitForSingleObject(mutexVideo, INFINITE);
  263.             printf(" Gar %d  registra Pedido\n",idgarcon);
  264.         ReleaseMutex(mutexVideo);
  265.  
  266. }
  267.  
  268. void entregaPedido(int idgarcon)
  269. {
  270.     WaitForSingleObject(mutexVideo, INFINITE);
  271.             printf(" Gar %d  entrega Pedido\n",idgarcon);
  272.         ReleaseMutex(mutexVideo);
  273.    
  274.     for(int i = 0; i < k ; i++)
  275.     {
  276.         PulseEvent(eventCliente[matrizCliente[idgarcon][i]]);
  277.        
  278.     }
  279.     numeroPedidos[idgarcon] = 0;
  280.     topo[idgarcon] = 0;
  281.  
  282.     ReleaseMutex(mutexGarcon[idgarcon]);
  283.  
  284. }
  285.  
  286. void garcon(void *parametro)
  287. {
  288.     parametroGarcon *par = (parametroGarcon *) parametro;
  289.  
  290.     WaitForSingleObject(mutexVideo, INFINITE);
  291.         cout << "Garcom " << par->id <<  " iniciado" << endl << endl;
  292.     ReleaseMutex(mutexVideo);
  293.  
  294.     while(!fechoubar())
  295.     {
  296.         recebePedidoMaximo(par->id);
  297.         registraPedido(par->id);
  298.         entregaPedido(par->id);
  299.  
  300.         WaitForSingleObject(mutexRodada, INFINITE);
  301.             rodadas++;
  302.            
  303.         ReleaseMutex(mutexRodada);
  304.        
  305.         WaitForSingleObject(mutexVideo, INFINITE);
  306.             if(rodadas <= (r + 1))
  307.             {
  308.                 printf("rodadas: %d\n", rodadas);
  309.             }
  310.         ReleaseMutex(mutexVideo);
  311.        
  312.     }
  313. }
  314.  
  315. bool fechoubar()
  316. {
  317.     if(rodadas < r)
  318.     {
  319.  
  320.         return(false);
  321.     }
  322.  
  323.     else
  324.     {
  325.         for(int i = 0; i < c ; i++)
  326.     {
  327.         ResetEvent(eventCliente[i]);
  328.        
  329.        
  330.     }
  331.    
  332.     for(int i = 0; i < x; i++)
  333.     {
  334.         topo[i] = 0;
  335.         numeroPedidos[i] = 0;
  336.        
  337.     }
  338.         return(true);
  339.        
  340.     }
  341. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement