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)); } }
void handle_sigchld(int sig) { pid_t pid; // 如果多个子进程同时断开连接 while ((pid = waitpid(-1 ,NULL, WNOHANG)) > 0) { unsigned int* ip = hash_lookup_entry(s_pid_ip_hash, &pid, sizeof(pid)); if (ip == NULL) { continue; } drop_ip_count(ip); --cur_childrens; hash_free_entry(s_pid_ip_hash, &pid, sizeof(pid)); } }
int main(void) { stu2_t stu_arr[] = { { 1234, "AAAA", 20 }, { 4568, "BBBB", 23 }, { 6729, "AAAA", 19 } }; hash_t *hash = hash_alloc(256, hash_int); int size = sizeof(stu_arr) / sizeof(stu_arr[0]); int i; for (i=0; i<size; i++) { hash_add_entry(hash, &(stu_arr[i].sno), sizeof(stu_arr[i].sno), &stu_arr[i], sizeof(stu_arr[i])); } int sno = 4568; stu2_t *s = (stu2_t *)hash_lookup_entry(hash, &sno, sizeof(sno)); if (s) { printf("%d %s %d\n", s->sno, s->name, s->age); } else { printf("not found\n"); } sno = 1234; hash_free_entry(hash, &sno, sizeof(sno)); s = (stu2_t *)hash_lookup_entry(hash, &sno, sizeof(sno)); if (s) { printf("%d %s %d\n", s->sno, s->name, s->age); } else { printf("not found\n"); } return 0; }
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)); } }
static void handle_sigchld(void* p_private) { unsigned int reap_one = 1; (void) p_private; while (reap_one) { reap_one = (unsigned int)vsf_sysutil_wait_reap_one(); if (reap_one) { struct vsf_sysutil_ipv4addr* p_ip; /* Account total number of instances */ --s_children; /* Account per-IP limit */ p_ip = (struct vsf_sysutil_ipv4addr*) hash_lookup_entry(s_p_pid_ip_hash, (void*)&reap_one); drop_ip_count(p_ip); hash_free_entry(s_p_pid_ip_hash, (void*)&reap_one); } } }
void drop_ip_count(void* ip) { unsigned int count; unsigned int* ip_count; ip_count = hash_lookup_entry(s_ip_count_hash, ip, sizeof(unsigned int)); if (ip_count == NULL) { return; } count = *ip_count; count--; if (count == 0) { hash_free_entry(s_ip_count_hash, ip, sizeof(unsigned int)); } *ip_count = count; }
static void drop_ip_count(struct vsf_sysutil_ipv4addr* p_ip) { unsigned int count; unsigned int* p_count = (unsigned int*)hash_lookup_entry(s_p_ip_count_hash, (void*)p_ip); if (!p_count) { bug("IP address missing from hash"); } count = *p_count; if (!count) { bug("zero count for IP address"); } count--; *p_count = count; if (!count) { hash_free_entry(s_p_ip_count_hash, (void*)p_ip); } }
static void drop_ip_count(void* p_raw_addr) { unsigned int count; unsigned int* p_count = (unsigned int*)hash_lookup_entry(s_p_ip_count_hash, p_raw_addr); if (!p_count) { bug("IP address missing from hash"); } count = *p_count; if (!count) { bug("zero count for IP address"); } count--; *p_count = count; if (!count) { hash_free_entry(s_p_ip_count_hash, p_raw_addr); } }
static void handle_sigchld(int duff) { unsigned int reap_one = 1; (void) duff; while (reap_one) { reap_one = (unsigned int)vsf_sysutil_wait_reap_one(); if (reap_one) { struct vsf_sysutil_ipaddr* p_ip; /* Account total number of instances */ --s_children; /* Account per-IP limit */ p_ip = (struct vsf_sysutil_ipaddr*) hash_lookup_entry(s_p_pid_ip_hash, (void*)&reap_one); /* Kitsune - old connections are not in the hash tables */ if (p_ip) { drop_ip_count(p_ip); hash_free_entry(s_p_pid_ip_hash, (void*)&reap_one); } } } }
int main() { stu_t stu_arr[]={ {"1","A",20}, {"2","B",21}, {"3","C",22} }; hash_t *hash=hash_alloc(256,hash_function); int size = sizeof(stu_arr)/sizeof(stu_arr[0]); /*用字符串作为关键码 int i=0; for(;i<size;++i) { hash_add_entry(hash,stu_arr[i].num,strlen(stu_arr[i].num),&stu_arr[i],sizeof(stu_arr[i])); } stu_t *s = (stu_t*)hash_lookuo_enty(hash,"1",1); if(s != NULL) { printf("%s %s %d\n",s->num,s->name,s->age); } else { printf("not found\n"); } hash_free_entry(hash,"1",1); s = (stu_t*)hash_lookuo_enty(hash,"1",1); if(s != NULL) { printf("%s %s %d\n",s->num,s->name,s->age); } else { printf("not found\n"); } */ //用年龄作为关键码 (用整数作为关键码) int i=0; for(;i<size;++i) { hash_add_entry(hash,&stu_arr[i].age,sizeof(stu_arr[i].age),&stu_arr[i],sizeof(stu_arr[i])); } int key=20; stu_t *s = (stu_t*)hash_lookuo_enty(hash,&key,4); if(s != NULL) { printf("%s %s %d\n",s->num,s->name,s->age); } else { printf("not found\n"); } hash_free_entry(hash,&key,4); s = (stu_t*)hash_lookuo_enty(hash,&key,4); if(s != NULL) { printf("%s %s %d\n",s->num,s->name,s->age); } else { printf("not found\n"); } hash_dealloc(hash,256); return 0; }