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
- #define tempmax 15
- typedef struct{
- int id, garfoesquerda, garfodireita, numgarfos;
- }parametro;
- int garfo[threads] = { 1, 1, 1, 1, 1};
- int jantou[threads];
- 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(parametro *par);
- void pega_garfo_direita(parametro *par);
- void come(int id);
- void devolver_garfo_esquerda(parametro *par);
- void devolver_garfo_direita(parametro *par);
- 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);
- /*while(1)
- {
- fim = clock();
- tempo = ((double) (fim - inicio)) / CLOCKS_PER_SEC;
- if(tempo > 30)
- {
- for(int i = 0; i < threads; i++)
- {
- TerminateThread(idThread[i], TRUE);
- }
- break;
- }
- WaitForSingleObject(mutexVideo, INFINITE);
- printf("%lf", tempo);
- ReleaseMutex(mutexVideo);
- }*/
- for(int i = 0; i < threads; i++)
- {
- CloseHandle(idThread[i]);
- }
- for(int i = 0; i < threads; i++)
- {
- printf("jantou filosofo %d = %d\n", i, jantou[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].numgarfos = 0;
- jantou[i] = 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;
- clock_t inicio = clock(), fim;
- double tempo;
- WaitForSingleObject(mutexVideo, INFINITE);
- printf("Filosofo %d iniciado \n", filosofopar->id );
- ReleaseMutex(mutexVideo);
- while(1) {
- do{
- pensa(filosofopar->id);
- pega_garfo_esquerda(filosofopar);
- if(filosofopar->numgarfos == 1)
- {
- pega_garfo_direita(filosofopar);
- }
- WaitForSingleObject(mutexVideo, INFINITE);
- printf("Filosofo %d numgarfos %d\n", filosofopar->id, filosofopar->numgarfos);
- ReleaseMutex(mutexVideo);
- }while(filosofopar->numgarfos != 2);
- come(filosofopar->id);
- devolver_garfo_esquerda(filosofopar);
- devolver_garfo_direita(filosofopar);
- fim = clock();
- tempo = ((double) (fim - inicio)) / CLOCKS_PER_SEC;
- if(tempo > tempmax)
- {
- break;
- }
- printf("tempo %lf\n", tempo);
- }
- }
- void pensa(int id)
- {
- srand(time(NULL));
- int t = rand() % 1 + 4;
- WaitForSingleObject(mutexVideo, INFINITE);
- printf("filosofo %d vai pensar por %d segundos\n", id, t );
- ReleaseMutex(mutexVideo);
- Sleep(t * 1000);
- }
- void pega_garfo_esquerda(parametro *par)
- {
- WaitForSingleObject(mutexGarfo, INFINITE);
- if(garfo[par->garfoesquerda] != 1)
- {
- }
- garfo[par->garfoesquerda] = 0;
- par->numgarfos += 1;
- ReleaseMutex(mutexGarfo);
- WaitForSingleObject(mutexVideo, INFINITE);
- printf("filosofo %d pegou garfo esquerdo %d\n", par->id, par->garfoesquerda);
- ReleaseMutex(mutexVideo);
- }
- void pega_garfo_direita(parametro *par)
- {
- WaitForSingleObject(mutexGarfo, INFINITE);
- if(garfo[par->garfodireita] != 1)
- {
- devolver_garfo_esquerda(par);
- }
- else
- {
- garfo[par->garfodireita] = 0;
- par->numgarfos += 1;
- }
- ReleaseMutex(mutexGarfo);
- WaitForSingleObject(mutexVideo, INFINITE);
- printf("filosofo %d pegou garfo direito %d\n", par->id, par->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);
- jantou[id] += 1;
- Sleep(t * 1000);
- }
- void devolver_garfo_esquerda(parametro *par)
- {
- WaitForSingleObject(mutexGarfo, INFINITE);
- garfo[par->garfoesquerda] = 1;
- par->numgarfos = par->numgarfos - 1;
- ReleaseMutex(mutexGarfo);
- WaitForSingleObject(mutexVideo, INFINITE);
- cout << "filosofo" << par->id << " devolve garfo esquerda " << par->garfoesquerda << endl;
- ReleaseMutex(mutexVideo);
- }
- void devolver_garfo_direita(parametro *par)
- {
- WaitForSingleObject(mutexGarfo, INFINITE);
- garfo[par->garfodireita] = 1;
- par->numgarfos = par->numgarfos - 1;
- ReleaseMutex(mutexGarfo);
- WaitForSingleObject(mutexVideo, INFINITE);
- cout << "filosofo" << par->id << " devolve garfo direita " << par->garfodireita << endl;
- ReleaseMutex(mutexVideo);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement