forked from FelipeResende/socket_redes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
53 lines (45 loc) · 1.18 KB
/
client.c
File metadata and controls
53 lines (45 loc) · 1.18 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
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include "buffer.h"
void *producer(void *);
void *consumer(void *);
char texto[] = "Texto a ser escrito no buffer para testes...";
#define check(A,msg) \
if ((A)) { \
printf("##msg"); \
exit(1); \
}
int main()
{
buffer *b = malloc(sizeof(buffer));
pthread_t pthread_consumidor, pthread_produtor;
initBuffer(b);
check(pthread_create(&pthread_produtor, NULL, producer, (void *)b), "ERROR creating thread producer!!\n");
check(pthread_create(&pthread_consumidor, NULL, consumer, (void *)b), "ERROR creating thread consumer!!\n")
check(pthread_join(pthread_produtor, NULL), "ERROR joining thread producer")
check(pthread_join(pthread_consumidor, NULL), "ERROR joining thread consumer")
destructBuffer(b);
pthread_exit(NULL);
return 1;
}
void *producer(void * d)
{
buffer *pb = (buffer *)d;
for (int i = 0; i < sizeof(texto) - 1; ++i)
{
printf("Producer: %c\n", texto[i]);
pb->insert(pb, texto[i]);
}
return 0;
}
void *consumer(void *d)
{
buffer *pb = (buffer *)d;
for (int i = 0; i < sizeof(texto) - 1; ++i)
{
char c = pb->get(pb);
printf("\t\tConsumer: %c\n", c);
}
return 0;
}