// Grabs a string from a buffered queue and counts it void *reduce(void* rArgs){ struct reducerArgs *args = (struct reducerArgs*) rArgs; int mappersFinished = 0; int error; int count; map_t map = *(args->map); struct buffered_queue *conn = (args->conns)[args->reducerId]; // Corresponding buffered queue while (1){ // continually attempt to read until signal received all mappers done char *str = (char*)buffered_queue_pop(conn); // Word to reduce from buffered queue if (str == NULL) { // Received mappers finished signal break; } else { printf("Reducer %d is counting \"%s\"\n", args->reducerId, str); error = hashmap_get(map, str, &count); if (error == MAP_OK){ hashmap_put(map, str, count + 1); printf("Reducer %d counted \"%s\" %d times\n", args->reducerId, str, count + 1); } else { hashmap_put(map, str, 1); printf("Reducer %d counted \"%s\" 1 time\n",args->reducerId, str); } } } hashmap_iterate(map, print_result); return NULL; }
void *reducer( void *ptr ){ struct ma p = *(struct ma *)ptr; int num = 0; char *flag = "1"; int error; while(1){ char * token = (char *)buffered_queue_pop(&queue[p.id]); if(!strcmp(token,flag)){ num++; if(num == p.m) return NULL; continue; } int count; error = hashmap_get(mymap[p.id], token, &count); if(error==MAP_OK) hashmap_put(mymap[p.id], token, count+1); else hashmap_put(mymap[p.id], token, 1); } return NULL; /*each reducer puts one queue to one hashtable*/ }