Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<Windows.h>
- #include<process.h>
- #include<stdio.h>
- #include<stdlib.h>
- #include<time.h>
- #include<iostream>
- #include<vector>
- using namespace std;
- #define r 30//número de rodadas
- #define x 4//número de garçons
- #define c 13//número de clientes
- #define k 2//capacidade dos garçons
- typedef struct{
- bool clienteEsperaPedido;
- int id;
- }parametroCliente;
- typedef struct{
- int id;
- }parametroGarcon;
- HANDLE mutexGarcon[x];
- HANDLE mutexVideo;
- HANDLE mutexRodada;
- HANDLE eventCliente[c];
- HANDLE mutexCriar;
- int numeroPedidos[x] = {0};
- int matrizCliente[x][k]; // gardar os ids dos clientes que pediram para determinado garcon
- int topo[x] = {0};
- int rodadas = 0;
- void CriarThreadsGarcon(HANDLE idThread[], parametroGarcon parametro[], DWORD ThreadIdentifier[]);
- void CriarThreadsClientes(HANDLE idThread[], parametroCliente parametro[], DWORD ThreadIdentifier[]);
- void IniciarThreads(HANDLE idThread[], int n);
- void FecharThread(HANDLE idThread[], int n);
- void criarMutex();
- void criarEvent();
- void fazPedido(parametroCliente *par);
- void esperaPedido(int id);
- void recebePedido(bool *clienteEspera, int id);
- void consomePedido(int id);
- void cliente(void *parametro);
- void recebePedidoMaximo(int idgarcon);
- void registraPedido(int idgarcon);
- void entregaPedido(int idgarcon);
- void garcon(void *parametro);
- bool fechoubar();
- int main()
- {
- srand(12);
- DWORD clienteIdentifier[c];
- HANDLE idCliente[c];
- parametroCliente parCli[c];
- DWORD garconIdentifier[x];
- HANDLE idGarcon[x];
- parametroGarcon parGar[x];
- criarMutex();
- criarEvent();
- printf("Criando threads clientes\n");
- CriarThreadsClientes(idCliente, parCli, clienteIdentifier);
- printf("Criando threads Garcom\n");
- CriarThreadsGarcon(idGarcon, parGar, garconIdentifier);
- printf("Inciar threads ? ");
- system("pause");
- WaitForSingleObject(mutexCriar, INFINITE);
- IniciarThreads(idGarcon, x);
- ReleaseMutex(mutexCriar);
- WaitForSingleObject(mutexCriar, INFINITE);
- IniciarThreads(idCliente, c);
- ReleaseMutex(mutexCriar);
- WaitForMultipleObjects(c, idCliente, TRUE, INFINITE);
- WaitForMultipleObjects(x, idGarcon, TRUE, INFINITE);
- FecharThread(idCliente, c);
- FecharThread(idGarcon, x);
- return 0;
- }
- void CriarThreadsClientes(HANDLE idThread[], parametroCliente parametro[], DWORD ThreadIdentifier[])
- {
- for(int i = 0; i < c; i++)
- {
- parametro[i].id = i;
- idThread[i] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)cliente, ¶metro[i], CREATE_SUSPENDED, &ThreadIdentifier[i]);
- }
- }
- void CriarThreadsGarcon(HANDLE idThread[], parametroGarcon parametro[], DWORD ThreadIdentifier[])
- {
- for(int i = 0; i < x; i++)
- {
- parametro[i].id = i;
- idThread[i] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)garcon, ¶metro[i], CREATE_SUSPENDED, &ThreadIdentifier[i]);
- }
- }
- void IniciarThreads(HANDLE idThread[], int n)
- {
- for(int i = 0; i < n; i++)
- {
- ResumeThread(idThread[i]);
- }
- }
- void FecharThread(HANDLE idThread[], int n)
- {
- for(int i = 0; i < n; i++)
- {
- CloseHandle(idThread[i]);
- }
- }
- void criarMutex()
- {
- mutexVideo = CreateMutex(NULL, FALSE, NULL);
- mutexRodada = CreateMutex(NULL, FALSE, NULL);
- mutexCriar = CreateMutex(NULL, FALSE, NULL);
- for(int i = 0; i < x; i++)
- {
- mutexGarcon[i]= CreateMutex(NULL, FALSE, NULL);
- }
- }
- void criarEvent()
- {
- for(int i = 0; i < c; i++)
- {
- eventCliente[i] = CreateEvent(NULL, TRUE, FALSE, NULL);
- }
- }
- void fazPedido(parametroCliente *par)
- {
- int gar;
- WaitForSingleObject(mutexVideo, INFINITE);
- cout << "Cliente " << par->id << " faz pedido" << endl;
- ReleaseMutex(mutexVideo);
- WaitForSingleObject(mutexRodada, INFINITE);
- do{
- gar = rand() % x;
- }while(topo[gar] >= k);
- topo[gar] += 1;
- ReleaseMutex(mutexRodada);
- WaitForSingleObject(mutexVideo, INFINITE);
- cout << "Cliente " << par->id << " pede ao " << " garcom " << gar << endl;
- ReleaseMutex(mutexVideo);
- WaitForSingleObject(mutexGarcon[gar], INFINITE); //Apenas um cliente pede por vez a um único garçom
- numeroPedidos[gar] += 1;
- matrizCliente[gar][numeroPedidos[gar] - 1] = par->id;
- par->clienteEsperaPedido = true;
- ReleaseMutex(mutexGarcon[gar]);
- }
- void esperaPedido(int id)
- {
- WaitForSingleObject(mutexVideo, INFINITE);
- cout << "Cliente " << id << " espera pedido" << endl;
- ReleaseMutex(mutexVideo);
- WaitForSingleObject(eventCliente[id], INFINITE);
- }
- void recebePedido(bool *clienteEspera, int id)
- {
- WaitForSingleObject(mutexVideo, INFINITE);
- cout << "Cliente " << id << " recebe pedido" << endl;
- ReleaseMutex(mutexVideo);
- *clienteEspera = false;
- }
- void consomePedido(int id)
- {
- WaitForSingleObject(mutexVideo, INFINITE);
- cout << "Cliente " << id << " consome pedido" << endl;
- ReleaseMutex(mutexVideo);
- Sleep(1800 + rand() % 500);
- }
- void cliente(void *parametro)
- {
- parametroCliente *par = (parametroCliente *) parametro;
- WaitForSingleObject(mutexVideo, INFINITE);
- printf("ClientThread %d iniciada\n", par->id);
- ReleaseMutex(mutexVideo);
- while(!fechoubar()){
- fazPedido(par);
- esperaPedido(par->id);
- recebePedido(&(par->clienteEsperaPedido), par->id);
- consomePedido(par->id);
- }
- }
- void recebePedidoMaximo(int idgarcon)
- {
- WaitForSingleObject(mutexVideo, INFINITE);
- printf("Gar %d recebe Pedido Maximo\n",idgarcon );
- ReleaseMutex(mutexVideo);
- while(numeroPedidos[idgarcon] < k && fechoubar() != true)
- {
- }
- }
- void registraPedido(int idgarcon)
- {
- WaitForSingleObject(mutexGarcon[idgarcon], INFINITE); // por enquanto ninguem mais pede a este garcon
- WaitForSingleObject(mutexVideo, INFINITE);
- printf(" Gar %d registra Pedido\n",idgarcon);
- ReleaseMutex(mutexVideo);
- }
- void entregaPedido(int idgarcon)
- {
- WaitForSingleObject(mutexVideo, INFINITE);
- printf(" Gar %d entrega Pedido\n",idgarcon);
- ReleaseMutex(mutexVideo);
- for(int i = 0; i < k ; i++)
- {
- PulseEvent(eventCliente[matrizCliente[idgarcon][i]]);
- }
- numeroPedidos[idgarcon] = 0;
- topo[idgarcon] = 0;
- ReleaseMutex(mutexGarcon[idgarcon]);
- }
- void garcon(void *parametro)
- {
- parametroGarcon *par = (parametroGarcon *) parametro;
- WaitForSingleObject(mutexVideo, INFINITE);
- cout << "Garcom " << par->id << " iniciado" << endl << endl;
- ReleaseMutex(mutexVideo);
- while(!fechoubar())
- {
- recebePedidoMaximo(par->id);
- registraPedido(par->id);
- entregaPedido(par->id);
- WaitForSingleObject(mutexRodada, INFINITE);
- rodadas++;
- ReleaseMutex(mutexRodada);
- WaitForSingleObject(mutexVideo, INFINITE);
- if(rodadas <= (r + 1))
- {
- printf("rodadas: %d\n", rodadas);
- }
- ReleaseMutex(mutexVideo);
- }
- }
- bool fechoubar()
- {
- if(rodadas < r)
- {
- return(false);
- }
- else
- {
- for(int i = 0; i < c ; i++)
- {
- ResetEvent(eventCliente[i]);
- }
- for(int i = 0; i < x; i++)
- {
- topo[i] = 0;
- numeroPedidos[i] = 0;
- }
- return(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement