-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgerador.c
212 lines (169 loc) · 5.05 KB
/
gerador.c
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <pthread.h>
#include "pedido.h"
#define NUM_CHARS_FILE_NAME 100
#define NUMBER_THREADS 2
//Order Processing
int nrOfRequests;
int maxUse;
pthread_mutex_t record_sem;
int record;
struct timespec ts;
struct timespec tInitial;
//Variables for statistics
int generated_m = 0;
int generated_f = 0;
int rejected_m = 0;
int rejected_f = 0;
int discarded_m = 0;
int discarded_f = 0;
struct thread_data{
int numberOfRequests;
int maxUse;
};
/**
* Thread that performs the random generation of orders
*/
void *thr_NewsRequest(void *thread_arg){
//input fifo
int input_fifo;
do{
input_fifo=open("/tmp/entrada", O_WRONLY);
}while(input_fifo==-1);
timespec_get(&tInitial, TIME_UTC);
struct request generatedRequest;
//assistant of rand
srand(time(NULL));
int cont = 1;
int nSerie = 1;
//Randomly generate gender
while (cont <= nrOfRequests) {
char g;
int t;
int auxG = rand() & 1;
if(auxG == 0){
g = 'M';
generated_m++;
}else{
g = 'F';
generated_f++;
}
//Randomly generate order time
t = rand() % maxUse + 1;
//Create the order and save to the structure "generatedRequest"
generatedRequest.serial_number = nSerie;
generatedRequest.gender = g;
generatedRequest.timeReq = t;
generatedRequest.nrOfRejects = 0;
write(input_fifo, &generatedRequest, sizeof(generatedRequest));
timespec_get(&ts, TIME_UTC);
dprintf(record,"%8.2f - %ul - %ld - %4d: %c - %4d - PEDIDO\n",(ts.tv_sec - tInitial.tv_sec)*1000+(ts.tv_nsec - tInitial.tv_nsec)*0.000001,getpid(),pthread_self(),generatedRequest.serial_number,generatedRequest.gender, generatedRequest.timeReq);
nSerie++;
cont++;
}
close(input_fifo);
return NULL;
}
/**
* Function used to process rejected orders
*/
void processRejectedRequest(struct request *generatedRequest){
int input_fifo;
do{
input_fifo=open("/tmp/entrada", O_WRONLY);
}while(input_fifo==-1);
//Increment before verifying
generatedRequest->nrOfRejects= generatedRequest->nrOfRejects+1;
timespec_get(&ts, TIME_UTC);
dprintf(record,"%8.2f - %u - %ld - %4d: %c - %4d - REJEITADO\n",(ts.tv_sec - tInitial.tv_sec)*1000+(ts.tv_nsec - tInitial.tv_nsec)*0.000001,getpid(),pthread_self(),generatedRequest->serial_number,generatedRequest->gender, generatedRequest->timeReq);
if(generatedRequest->gender== 'M')
rejected_m++;
else
rejected_f++;
//If the number of requests is 3 the request is discarded
if(generatedRequest->nrOfRejects==3){
timespec_get(&ts, TIME_UTC);
dprintf(record,"%8.2f - %u - %ld - %4d: %c - %4d - DESCARTADO\n",(ts.tv_sec - tInitial.tv_sec)*1000+(ts.tv_nsec - tInitial.tv_nsec)*0.000001,getpid(),pthread_self(),generatedRequest->serial_number,generatedRequest->gender, generatedRequest->timeReq);
if(generatedRequest->gender=='M')
discarded_m++;
else
discarded_f++;
close(input_fifo);
return;
}
else{
write(input_fifo, generatedRequest, sizeof(*generatedRequest));
close(input_fifo);
timespec_get(&ts, TIME_UTC);
dprintf(record,"%8.2f - %u - %ld - %4d: %c - %4d - PEDIDO\n",(ts.tv_sec - tInitial.tv_sec)*1000+(ts.tv_nsec - tInitial.tv_nsec)*0.000001,getpid(),pthread_self(),generatedRequest->serial_number,generatedRequest->gender, generatedRequest->timeReq);
return;
}
}
/**
* Thread that checks the rejected requests and places them in the queue of requests
*/
void *thr_RejectedRequest(void *arg){
int fdRej;
fdRej=open("/tmp/rejeitados",O_RDONLY);
if(fdRej == -1){
return (void*)1;
}
struct request generatedRequest;
//While can read a request, update generatedRequest and processes it
while(read(fdRej,&generatedRequest,sizeof(generatedRequest)) != 0){
processRejectedRequest(&generatedRequest);
}
close(fdRej);
unlink("/tmp/rejeitados");
//Print statistics
pthread_mutex_lock(&record_sem);
dprintf(STDOUT_FILENO,"N. gerados:\n%d, %d, %d\nN. rejeitados:\n%d, %d, %d\nN. descartados:\n%d, %d, %d\n"
,(generated_f+generated_m),generated_f,generated_m
,(rejected_f+rejected_m),rejected_f,rejected_m
,(discarded_f+discarded_m),discarded_f,discarded_m);
pthread_mutex_unlock(&record_sem);
return NULL;
}
int main(int argc, char const *argv[]) {
//Data processing
if(argc != 3){
return 1;
}
if(atoi(argv[1]) >= 1){
nrOfRequests = atoi(argv[1]);
}else{
return 1;
}
if(atoi(argv[2]) >= 1){
maxUse = atoi(argv[2]);
}else{
return 1;
}
//Rejected fifo
mkfifo("/tmp/rejeitados",0660);
//Creation and opening of log file
char nameFile[NUM_CHARS_FILE_NAME];
sprintf(nameFile, "%s%d","/tmp/ger.", getpid());
pthread_mutex_lock(&record_sem);
if((record = open(nameFile,O_WRONLY | O_CREAT | O_EXCL,0666)) == -1){
perror("gerador");
pthread_mutex_unlock(&record_sem);
printf("Erro ao abrir FILE - %s\n",nameFile);
return 1;
}
pthread_mutex_unlock(&record_sem);
/*threads*/
pthread_t newsRequest;
pthread_t rejectedRequest;
pthread_create(&newsRequest,NULL,thr_NewsRequest,NULL);
pthread_create(&rejectedRequest,NULL,thr_RejectedRequest,NULL);
/**/
pthread_exit(0);
}