-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathphilosophs2.cpp
101 lines (81 loc) · 2.6 KB
/
philosophs2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// ConsoleApplication3.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <process.h>
#include <windows.h>
#include <iostream>
#include <time.h>
#define MAX_RAND_INT 100
#define MIN_RAND_INT 0
#define PHILOSOPHS_NUMBER 4
using namespace std;
void Philosoph(void *);
int getRandom();
void printTab(int);
HANDLE forks[PHILOSOPHS_NUMBER];
HANDLE constraint;
int _tmain(int argc, _TCHAR* argv[])
{
constraint = CreateSemaphore (NULL, // default security attributes
PHILOSOPHS_NUMBER - 1, // initial count
PHILOSOPHS_NUMBER - 1, //maximum count
NULL); // object name
for(int i = 0; i < PHILOSOPHS_NUMBER; ++i)
{
char integer_string[32];
sprintf_s(integer_string, "Event%d", i);
forks[i] = CreateEvent(NULL, FALSE, TRUE, integer_string);
SetEvent(forks[i]);
_beginthread(Philosoph, 1024, (void*)i);
}
system("pause");
return 0;
}
void Philosoph (void * number)
{
int fork1Num = (int)number;
int fork2Num = fork1Num == PHILOSOPHS_NUMBER - 1 ? 0 : fork1Num + 1;
while(true)
{
WaitForSingleObject (constraint,INFINITE);
printTab(fork1Num);printf("Philosoph %d is sleeping\n", fork1Num);
Sleep((getRandom() % 10) * 1000);
printTab(fork1Num);printf("Philosoph %d is waking up\n", fork1Num);
int fork1 = 1;
int fork2 = 1;
while (fork1 != 0)
{
printTab(fork1Num);printf("Philosoph %d is trying to take %d fork\n", fork1Num, fork1Num);
fork1 = WaitForSingleObject(forks[fork1Num], INFINITE);
}
printTab(fork1Num);printf("Philosoph %d takes %d fork\n", fork1Num, fork1Num);
ResetEvent(forks[fork1Num]);
printTab(fork1Num);printf("Philosoph %d took %d fork\n", fork1Num, fork1Num);
while (fork2 != 0)
{
printTab(fork1Num);printf("Philosoph %d is trying to take %d fork\n", fork1Num, fork2Num);
fork2 = WaitForSingleObject(forks[fork2Num], INFINITE);
}
printTab(fork1Num);printf("Philosoph %d takes %d fork\n", fork1Num, fork2Num);
ResetEvent(forks[fork2Num]);
printTab(fork1Num);printf("Philosoph %d took %d fork\n", fork1Num, fork2Num);
printTab(fork1Num);printf("Philosoph %d is eating now\n", fork1Num);
Sleep((getRandom() % 10) * 1000);
printTab(fork1Num);printf("Philosoph %d ate\n", fork1Num);
SetEvent(forks[fork1Num]);
SetEvent(forks[fork2Num]);
printTab(fork1Num);printf("Philosoph %d returned all forks\n", fork1Num);
ReleaseSemaphore (constraint,1, NULL);
}
}
int getRandom()
{
return rand() % (MAX_RAND_INT - MIN_RAND_INT) + MIN_RAND_INT;
}
void printTab(int amount)
{
for(int i = 0; i < amount; ++i)
{
printf(" ");
}
}