-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmany_smokers.cpp
More file actions
120 lines (95 loc) · 2.36 KB
/
many_smokers.cpp
File metadata and controls
120 lines (95 loc) · 2.36 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
SMOKERS - C++ semaphores
Each smoker is represented by f1, f2 and f3:
- f1 has tobacco
- f2 has paper
- f3 has matches
Representation of pairs of ingredients that
the shop assistant put on its shop:
0 -> paper and matches
1 -> matches and tobacco
2 -> tobacco and paper
If one smoker wants to smoke, he needs to have
the three mentioned ingredients. At time, each
smoker has only 1 ingredient, so they have to
go to the shop to ask for the rest of ingredients.
*/
#include <iostream>
#include <pthread.h>
#include <semaphore.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
using namespace std ;
const long NFUM = 3; // number of smokers
sem_t estanquer, fum[NFUM]; // semaphores: 'estangquer' is the shop assistant, and 'fum' are the smokers
/**
It's the action of a smoker
*/
void fumar()
{
sleep (rand() % 5);
}
/**
Method that performs the shop assistant.
*/
void* Estanquero (void * arg)
{
int ingredientes;
while(true) // the shop assistat is always doing something
{
sem_wait( &estanquer );
ingredientes = rand() % 3; // we choose one pair of ingredient
if(ingredientes == 0)
{
cout<<"SA: Putting paper and matches..."<<endl;
sem_post( &(fum[ ingredientes ]) );
}
else if(ingredientes == 1)
{
cout<<"SA: Putting matches and tobacco..."<<endl;
sem_post( &(fum[ ingredientes ]) );
}
else if(ingredientes == 2)
{
cout<<"SA: Putting paper and tobacco..."<<endl;
sem_post( &(fum[ ingredientes ]) );
}
}
}
/**
Smokers perform this method.
var t is the id number of each smoker.
*/
void* fumador (void* t)
{
while(true)
{
sem_wait( &(fum[ (unsigned long)(t)]) );
cout<< "Smoker "<< (unsigned long) t <<" starts to somke."<<endl;
sem_post( &estanquer );
fumar();
cout<< "Smoker "<<(unsigned long) t <<" waits for ingredients."<<endl;
}
}
int main()
{
srand(time(NULL));
// Init of semaphores
sem_init( &estanquer, 0, 1);
for(int i=0; i<NFUM; i++)
sem_init( &(fum[i]), 0, 0);
// Threads init
pthread_t est, hebras[NFUM];
pthread_create( &est, NULL, Estanquero, NULL );
for(long i=0; i<NFUM; i++)
pthread_create( &(hebras[i]), NULL, fumador, (void *) i );
// wait for the threads
pthread_join( est, NULL );
for(long i=0; i<NFUM; i++)
pthread_join( hebras[i], NULL );
// destroy semaphores
sem_destroy( &estanquer );
for(int i=0; i<NFUM; i++)
sem_destroy( &(fum[i]) );
}