-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmthread.c
More file actions
executable file
·302 lines (263 loc) · 6.69 KB
/
mthread.c
File metadata and controls
executable file
·302 lines (263 loc) · 6.69 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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
copie os .lib e .a para: c:\Arquivos de Programas (x86)\Codeblocks\mingw\lib\
copie os .dll para: c:\Arquivos de Programas (x86)\Codeblocks\mingw\bin\
copie os .h para: c:\Arquivos de Programas (x86)\Codeblocks\mingw\include\
project -> build options -> linker settings -> Other linker options: inclua -lpthreadGC1
*/
//#define MWIN
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <pthread.h>
#include <signal.h>
#include <errno.h>
#include <unistd.h>
#ifdef MWIN
#include <windows.h>
#endif
#define ERRO 0
#define OK 1
#define BSIZE 6
#define NITEM 1000
#define TEMPO_SLEEP 10
// Variaveis globais
// declaracao dos mutexes
pthread_mutex_t G_p_fi; // independente
pthread_mutex_t G_p_be; // segundo - nao usado com G_p_bs
pthread_mutex_t G_p_bs; // segundo - nao usado com G_p_be
// controladores dos buffers
int G_qtd_be;
int G_qtd_bs;
// variavel de controle do encerramento
int G_terminou;
// buffers de processamento
char G_be[BSIZE];
char G_bs[BSIZE];
void m_usleep(unsigned long pause)
{
#ifdef MWIN
Sleep(pause);
#else
usleep(pause*1000);
#endif
return;
}
void init_lock(pthread_mutex_t *lock) // inicializa as variaveis de lock, fazer isto antes do inicio das threads
{
pthread_mutex_init(lock,NULL);
return;
}
void fini_lock(pthread_mutex_t *lock) // finalize as variaveis de lock, apos o pthread_kill
{
pthread_mutex_destroy(lock);
return;
}
int gerar_entrada()
{
FILE *arq;
int i;
if ((arq = fopen("e.txt","wt"))==NULL)
{
printf("\n>> ERRO: criando o arquivo de entrada (e.txt)\n");
return(ERRO);
}
for (i = 1 ; i <= NITEM; ++i)
{
fprintf(arq,"%05d\n",i);
}
fflush(arq);
fclose(arq);
return(OK);
}
void *escrita()
{
int acabou = 0;
FILE *arq;
printf("\n>> Iniciou Escrita");
arq = fopen("s.txt","wt");
while (1)
{
if (!acabou)
{
pthread_mutex_lock(&G_p_bs);
if (G_qtd_bs > 0)
{
fprintf(arq, "%s\n", G_bs);
memset(G_bs,0,sizeof(G_bs)*BSIZE);
G_qtd_bs = 0;
}
pthread_mutex_unlock(&G_p_bs);
pthread_mutex_lock(&G_p_fi);
if (G_terminou == 1)
{
G_terminou = 0;
fflush(arq);
fclose(arq);
acabou = 1;
printf("\n>> Terminou Escrita");
}
pthread_mutex_unlock(&G_p_fi);
}
}
return(NULL);
}
void *leitura()
{
int i = 0;
int acabou = 0;
char c, buf[BSIZE];
FILE *arq;
printf("\n>> Iniciou Leitura\n");
arq = fopen("e.txt","rt");
while (1)
{
if (!acabou)
{
pthread_mutex_lock(&G_p_be);
if (G_qtd_be <= 0)
{
if (fgets(G_be, BSIZE, arq) != NULL)
G_qtd_be = 6;
else
acabou = 1;
}
pthread_mutex_unlock(&G_p_be);
}
else
{
pthread_mutex_lock(&G_p_fi);
if (G_terminou == 3)
{
G_terminou = 2;
fclose(arq);
printf("\n>> Terminou Leitura\n");
}
pthread_mutex_unlock(&G_p_fi);
}
m_usleep(TEMPO_SLEEP);
}
return(NULL);
}
void *processamento()
{
int acabou = 0;
char buf[BSIZE];
memset(buf, 0, sizeof(buf)*BSIZE);
printf("\n>> Iniciou Processamento");
int i = 0;
while (1)
{
if (!acabou)
{
if (i <= 0)
{
pthread_mutex_lock(&G_p_be);
i = 0;
if (G_qtd_be != 0)
{
for (int j=G_qtd_be-2; j>=0; j--, i++)
buf[i] = G_be[j];
buf[5] = '\0';
memset(G_be, 0, sizeof(G_be)*BSIZE);
G_qtd_be = 0;
}
pthread_mutex_unlock(&G_p_be);
}
else
{
pthread_mutex_lock(&G_p_bs);
if (G_qtd_bs == 0)
{
sprintf(G_bs,"%s",buf);
G_qtd_bs = sizeof G_bs;
memset(buf, 0, sizeof(buf)*BSIZE);
}
i = 0;
pthread_mutex_unlock(&G_p_bs);
}
pthread_mutex_lock(&G_p_fi);
if (G_terminou == 2)
{
G_terminou = 1;
acabou = 1;
printf("\n>> Terminou Processamento");
}
pthread_mutex_unlock(&G_p_fi);
}
m_usleep(TEMPO_SLEEP);
}
return(NULL);
}
void finalizar()
{
int nao_acabou = 1;
while (nao_acabou)
{
m_usleep(50);
pthread_mutex_lock(&G_p_fi);
if (G_terminou == 0)
{
printf("\n>> Finalizou Tudo \n\n");
nao_acabou = 0;
}
pthread_mutex_unlock(&G_p_fi);
}
return;
}
int main(void)
{
// declaracao das pthreads
pthread_t T_entrada, T_processa, T_saida;
// inicializacao do G_terminou
G_terminou = 3;
// inicializacao dos mutexes de lock
init_lock(&G_p_fi);
init_lock(&G_p_be);
init_lock(&G_p_bs);
// limpeza dos buffers
memset(G_be,0,sizeof(G_be)*BSIZE);
memset(G_bs,0,sizeof(G_bs)*BSIZE);
// inicializacao dos controladores dos buffers
G_qtd_bs = 0;
G_qtd_be = 0;
// geracao do arquivo de entrada
if (!gerar_entrada())
{
printf("\nFalha na Geracao do Arquivo de Entrada");
return(1);
}
else
printf("\n>> Arquivo de Entrada Criado com Sucesso ");
// chamada das pthreads
// cria thread de leitura
if (pthread_create(&T_entrada,NULL,leitura,NULL))
{
printf("\nERRO: criando thread Leitura.\n");
return(0);
}
// cria thread de processamento
if (pthread_create(&T_processa,NULL,processamento,NULL))
{
printf("\nERRO: criando thread Processa.\n");
return(0);
}
// cria thread de saida
if (pthread_create(&T_saida,NULL,escrita,NULL))
{
printf("\nERRO: criando thread Saida.\n");
return(0);
}
// Aguarda finalizar
// G_terminou = 0;
finalizar();
// matar as pthreads
pthread_kill(T_entrada,0);
pthread_kill(T_processa,0);
pthread_kill(T_saida,0);
// finaliza mutexes
fini_lock(&G_p_fi);
fini_lock(&G_p_be);
fini_lock(&G_p_bs);
return(0);
}