@@ -52,6 +52,7 @@ int main(int argc, char **argv) {
52
52
pthread_t * workers ;
53
53
pthread_attr_t pthread_custom_attr ;
54
54
params * p ;
55
+ Action action ;
55
56
Chunk chunk ;
56
57
mqd_t r_queue , w_queue ;
57
58
struct mq_attr qattrs ;
@@ -105,11 +106,15 @@ int main(int argc, char **argv) {
105
106
106
107
// Initialize the variables
107
108
buffer = (unsigned char * )malloc (chunk_size );
109
+ assert (buffer != NULL );
108
110
109
111
// Initialize the threads
110
112
workers = (pthread_t * )malloc (NUM_THREADS * sizeof (workers ));
113
+ assert (workers != NULL );
114
+
111
115
pthread_attr_init (& pthread_custom_attr );
112
116
p = (params * )malloc (sizeof (params ) * NUM_THREADS );
117
+ assert (p != NULL );
113
118
114
119
for (i = 0 ; i < NUM_THREADS ; i ++ ) {
115
120
p [i ].id = i ;
@@ -139,27 +144,38 @@ int main(int argc, char **argv) {
139
144
140
145
// Now we have a chunk in our buffer, stick it on the queue
141
146
chunk = bs_new_chunk (chunk_count , buffer , hash , chunk_size , hash_length );
147
+ action = bs_new_action (HASH_CHUNK , chunk );
142
148
printf ("sender chunk-%d: %p\n" , chunk_count , chunk );
143
- if (mq_send (r_queue , (char * )bs_new_action ( HASH_CHUNK , chunk ) , MSG_SIZE , 5 ) == -1 )
149
+ if (mq_send (r_queue , (char * )action , MSG_SIZE , 5 ) == -1 )
144
150
perror ("Unable to send to read queue" );
145
151
152
+ free (action );
153
+
146
154
chunk_count ++ ;
147
155
148
- if (chunk_count >= 4 )
156
+ if (chunk_count >= 1 )
149
157
break ;
150
158
}
151
159
152
160
// End the threads
153
161
for (i = 0 ; i < NUM_THREADS ; i ++ ) {
154
- if (mq_send (r_queue , (char * )bs_new_action (END_THREAD , hash ), MSG_SIZE , 5 ) == -1 )
162
+ action = bs_new_action (END_THREAD , hash );
163
+ if (mq_send (r_queue , (char * )action , MSG_SIZE , 5 ) == -1 )
155
164
perror ("Unable to send to read queue" );
165
+ free (action );
156
166
}
157
167
158
168
// Wait for the threads to exit
159
169
for (i = 0 ; i < NUM_THREADS ; i ++ ) {
160
170
pthread_join (workers [i ], NULL );
161
171
}
162
172
173
+ // Tidy up after ourselves
174
+ free (p );
175
+ free (buffer );
176
+ free (workers );
177
+
178
+ // Close the queues
163
179
if (mq_close (r_queue ) == -1 )
164
180
perror ("Cannot close read-queue" );
165
181
@@ -172,5 +188,10 @@ int main(int argc, char **argv) {
172
188
if (mq_unlink ("/bsync-wqueue" ) == -1 )
173
189
perror ("Cannot unlink write-queue" );
174
190
191
+ // Close the files
192
+ fclose (bd_fp );
193
+ fclose (bf_fp );
194
+ fclose (h_fp );
195
+
175
196
return 0 ;
176
197
};
0 commit comments