-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsumers.c
173 lines (154 loc) · 3.53 KB
/
consumers.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <pthread.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdint.h>
#include <arpa/inet.h>
#include <limits.h>
#include <math.h>
#include "prodcon.h"
#define MIN(a,b) (a < b ? a : b)
#define MAX(a,b) (a < b ? b : a)
/*
** Client
*/
typedef struct pair{
pthread_t thread_id;
int id;
}pair;
char *host;
char *port;
int bad;
pair *pairs;
void* initiate_consumer(void * ptr);
double poissonRandomInterarrivalDelay( double r );
int main( int argc, char *argv[] )
{
int N;
int status;
double rate;
if(argc != 6 && argc != 5){
fprintf( stderr, "usage: chat [host] port clients rate bad\n" );
exit(-1);
}
if(argc == 6){
host = argv[1];
port = argv[2];
N = atoi(argv[3]);
rate = atof(argv[4]);
bad = atoi(argv[5]);
}else{
host = "localhost";
port = argv[1];
N = atoi(argv[2]);
rate = atof(argv[3]);
bad = atoi(argv[4]);
}
if(rate < 0){
fprintf( stderr, "rate should be positive\n" );
exit(-1);
}
if(bad < 0 || bad > 100){
fprintf( stderr, "bad should between 0 and 100\n" );
exit(-1);
}
if(bad == 0){
bad = INT_MAX;
}else{
bad = 100/bad;
}
pairs = (pair*)malloc(sizeof(pair) * N);
/* Create the N sockets in threads to the controller */
for (int i = 0; i< N; i++){
usleep(poissonRandomInterarrivalDelay(rate)*1E6);
pairs[i].id = i;
status = pthread_create(&pairs[i].thread_id,NULL,initiate_consumer,(void*)&pairs[i]);//(void*)&threads[i]
if(status < 0)
exit(-1);
}
for(int i = 0; i < N; i++){
pthread_join(pairs[i].thread_id,NULL);
}
free(pairs);
}
void* initiate_consumer(void * pairptr){
pthread_t tid = ((pair*)pairptr)->thread_id;
char filename[BUFSIZE];
sprintf(filename,"%ld",tid);
memcpy(filename + strlen(filename),".txt",strlen(".txt")+1);
char buf[BUFSIZE];
char *temp_ch;
char *temp_buf = NULL;
int32_t letter_count;
int32_t conv;
int cc;
int csock;
int counter;
int fd = -1;
if ( ( csock = connectsock( host, port, "tcp" )) == 0 )
{
fprintf( stderr, "Cannot connect to server.\n" );
goto END;
}
if( ( ((pair*)pairptr)->id + 1 ) % bad == 0){
printf("SLOW_CLIENT %d\n",((pair*)pairptr)->id + 1);
sleep(SLOW_CLIENT);
}
if ( write( csock, "CONSUME\r\n", strlen("CONSUME\r\n") ) < 0 )
{
fprintf( stderr, "client write: %s\n", strerror(errno) );
goto END;
}
temp_ch = (char*)&conv;
if ( (cc = read( csock, temp_ch, sizeof(conv) )) <= 0)
{
printf( "Server closed socket.\n" );
goto END;
}
letter_count = ntohl(conv);
if((fd = open(filename,O_WRONLY | O_CREAT,0777)) == -1){
printf( "Could not open file to write.\n" );
goto END;
}
temp_buf = (char*)malloc(letter_count*sizeof(char));
counter = 0;
while (counter < letter_count)
{
if( (cc = read( csock, temp_buf + counter, MIN(BUFSIZE,letter_count - counter) ) ) <= 0 )
{
printf( "Read failed in temp_buf %d.\n", counter );
goto END;
}
counter += cc;
}
counter = 0;
while (counter < letter_count)
{
if( (cc = write( fd, temp_buf + counter, MIN(BUFSIZE,letter_count - counter) ) ) < 0 )
{
printf( "Write file failed %d.\n", counter );
goto END;
}
counter += cc;
}
printf("%d bytes read\n",letter_count);
END:close( csock );
if(fd != -1)
close( fd );
if(temp_buf != NULL)
free(temp_buf);
pthread_exit( NULL );
}
double poissonRandomInterarrivalDelay( double r )
{
return (log((double) 1.0 -
((double) rand())/((double) RAND_MAX)))/-r;
}