void handle_sigchld(int sig) { pid_t pid; unsigned int *value; uint32_t *ip; while ((pid = waitpid(-1, NULL, WNOHANG)) > 0) { --num_of_clients; ip = hash_lookup_value_by_key(pid_to_ip, &pid, sizeof(pid)); hash_free_entry(pid_to_ip, &pid, sizeof(pid)); value = hash_lookup_value_by_key(ip_to_clients, &ip, sizeof(ip)); *value = *value - 1; if (*value == 0) hash_free_entry(ip_to_clients, &ip, sizeof(ip)); } }
static void handle_sigchld(int sig) { pid_t pid; while((pid = waitpid(-1, NULL, WNOHANG)) > 0) { --num_of_clients; //pid -> ip uint32_t *p_ip = hash_lookup_value_by_key(pid_to_ip, &pid, sizeof(pid)); assert(p_ip != NULL); //ip必须能找到 uint32_t ip = *p_ip; //ip -> clients unsigned int *p_value = (unsigned int *)hash_lookup_value_by_key(ip_to_clients, &ip, sizeof(ip)); assert(p_value != NULL && *p_value > 0); --*p_value; //释放hash表的结点 hash_free_entry(pid_to_ip, &pid, sizeof(pid)); } }
unsigned int add_ip_to_hash(hash_t *hash, uint32_t ip) { unsigned int *p_value = (unsigned int*)hash_lookup_value_by_key(hash,&ip,sizeof(ip)); if (p_value == NULL) { unsigned int value = 1; hash_add_entry(hash, &ip, sizeof(ip), &value, sizeof(value)); return 1; } else { unsigned int value = *p_value; value++; *p_value = value; return value; } }