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 threads 5
- typedef struct{
- int id, garfoesquerda, garfodireita, jantou;
- }parametro;
- int garfo[threads] = { 1, 1, 1, 1, 1};
- HANDLE mutexVideo;
- HANDLE mutexGarfo;
- void criarMutex();
- void setParametros(parametro par[]);
- void criarThreads(HANDLE idThread[], DWORD threadIdentifier[], parametro par[]);
- void iniciarThreads(HANDLE idThread[]);
- void filosofos(void *par);
- void pensa(int id);
- void pega_garfo_esquerda(int id, int garfoesquerda);
- void pega_garfo_direita(int id, int garfodireita);
- void come(int id);
- void devolver_garfo_esquerda(int id, int garfoesquerda);
- void devolver_garfo_direita(int id, int garfodireita);
- int main()
- {
- srand(time(NULL));
- parametro par[threads];
- HANDLE idThread[threads];
- DWORD threadIdentifier[threads];
- criarMutex();
- setParametros(par);
- criarThreads(idThread, threadIdentifier, par);
- WaitForSingleObject(mutexVideo, INFINITE);
- cout << "Iniciar threads?" << endl;
- system("pause");
- ReleaseMutex(mutexVideo);
- iniciarThreads(idThread);
- WaitForMultipleObjects(threads, idThread, TRUE, INFINITE);
- for(int i = 0; i < threads; i++)
- {
- CloseHandle(idThread[i]);
- }
- }
- void criarMutex()
- {
- mutexGarfo = CreateMutex(NULL, FALSE, NULL);
- mutexVideo = CreateMutex(NULL, FALSE, NULL);
- }
- void setParametros(parametro par[])
- {
- for(int i = 0; i < threads; i++)
- {
- par[i].id = i;
- par[i].garfoesquerda = i;
- par[i].garfodireita = (i + 1) % threads;
- par[i].jantou = 0;
- }
- }
- void criarThreads(HANDLE idThread[], DWORD threadIdentifier[], parametro par[])
- {
- for(int i = 0; i < threads; i++)
- {
- idThread[i] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)filosofos, &par[i], CREATE_SUSPENDED, &threadIdentifier[i]);
- }
- }
- void iniciarThreads(HANDLE idThread[])
- {
- for(int i = 0; i < threads; i++)
- {
- ResumeThread(idThread[i]);
- }
- }
- void filosofos(void *par)
- {
- parametro *filosofopar = (parametro *) par;
- WaitForSingleObject(mutexVideo, INFINITE);
- printf("Filosofo %d iniciado \n", filosofopar->id );
- ReleaseMutex(mutexVideo);
- while(1) {
- pensa(filosofopar->id);
- pega_garfo_esquerda(filosofopar->id, filosofopar->garfoesquerda);
- pega_garfo_direita(filosofopar->id, filosofopar->garfodireita);
- come(filosofopar->id);
- devolver_garfo_esquerda(filosofopar->id, filosofopar->garfoesquerda);
- devolver_garfo_direita(filosofopar->id, filosofopar->garfodireita);
- }
- }
- void pensa(int id)
- {
- srand(time(NULL));
- int t = rand() % 1 + 1;
- WaitForSingleObject(mutexVideo, INFINITE);
- printf("filosofo %d vai pensar por %d segundos\n", id, t );
- ReleaseMutex(mutexVideo);
- Sleep(t * 1000);
- }
- void pega_garfo_esquerda(int id, int garfoesquerda)
- {
- WaitForSingleObject(mutexGarfo, INFINITE);
- while(garfo[garfoesquerda] != 1)
- {
- }
- garfo[garfoesquerda] = 0;
- ReleaseMutex(mutexGarfo);
- WaitForSingleObject(mutexVideo, INFINITE);
- printf("filosofo %d pegou garfo esquerdo %d\n", id, garfoesquerda);
- ReleaseMutex(mutexVideo);
- }
- void pega_garfo_direita(int id, int garfodireita)
- {
- WaitForSingleObject(mutexGarfo, INFINITE);
- while(garfo[garfodireita] != 1)
- {
- }
- garfo[garfodireita] = 0;
- ReleaseMutex(mutexGarfo);
- WaitForSingleObject(mutexVideo, INFINITE);
- printf("filosofo %d pegou garfo direito %d\n", id, garfodireita);
- ReleaseMutex(mutexVideo);
- }
- void come(int id)
- {
- srand(time(NULL));
- int t = rand() % 1 + 1;
- WaitForSingleObject(mutexVideo, INFINITE);
- cout << "filosofo " << id << " vai comer por " << t << "segundos" << endl;
- ReleaseMutex(mutexVideo);
- Sleep(t * 1000);
- }
- void devolver_garfo_esquerda(int id, int garfoesquerda)
- {
- WaitForSingleObject(mutexGarfo, INFINITE);
- garfo[garfoesquerda] = 1;
- ReleaseMutex(mutexGarfo);
- WaitForSingleObject(mutexVideo, INFINITE);
- cout << "filosofo" << id << " devolve garfo esquerda " << garfoesquerda << endl;
- ReleaseMutex(mutexVideo);
- }
- void devolver_garfo_direita(int id, int garfodireita)
- {
- WaitForSingleObject(mutexGarfo, INFINITE);
- garfo[garfodireita] = 1;
- ReleaseMutex(mutexGarfo);
- WaitForSingleObject(mutexVideo, INFINITE);
- cout << "filosofo" << id << " devolve garfo direita " << garfodireita << endl;
- ReleaseMutex(mutexVideo);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement