本文主要向大家介绍了C/C++知识点之gethostbyname超时,与遇到的一些坑,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。
现网有些DNS服务器质量不好,经常不返回,导致服务器在做DNS请求时经常被卡住,简单测试后发现如果服务器不返回结果,一般20+秒超时。但是如果使用alarm和signal,是不能中断系统函数的,参考了一些网络上的资料,可以设置sigsetjmp和siglongjmp来打断系统函数,并可以恢复之前的堆栈,避免发生问题。
static sigjmp_buf jmpbuf;static void alarm_func(int sig_no){ siglongjmp(jmpbuf, 1);}struct hostent *gngethostbyname(const char *HostName, int timeout){ struct hostent *lpHostEnt; signal(SIGALRM, alarm_func); if(sigsetjmp(jmpbuf, 1) != 0) { alarm(0);//timout signal(SIGALRM, SIG_IGN); return NULL; } alarm(timeout);//setting alarm lpHostEnt = gethostbyname(HostName); signal(SIGALRM, SIG_IGN); return lpHostEnt;}
在实验例子里面非常好用,但是在实际代码中发现出现了异常
Thread 22 (Thread 0x7fffccff9700 (LWP 25722)):#0 0x00000038b7addfc3 in poll () from /lib64/libc.so.6#1 0x00000038b960bed4 in __libc_res_nsend () from /lib64/libresolv.so.2#2 0x00000038b960876a in __libc_res_nquery () from /lib64/libresolv.so.2#3 0x00000038b9608d29 in __libc_res_nquerydomain () from /lib64/libresolv.so.2#4 0x00000038b96099cf in __libc_res_nsearch () from /lib64/libresolv.so.2#5 0x00007fffec5f62d8 in _nss_dns_gethostbyname3_r () from /lib64/libnss_dns.so.2#6 0x00007fffec5f650e in _nss_dns_gethostbyname_r () from /lib64/libnss_dns.so.2#7 0x00000000ffffffff in ?? ()#8 0x0000000000000002 in ?? ()#9 0x0000000000000005 in ?? ()#10 0x0000000000205098 in ?? ()#11 0x0000000000000000 in ?? ()
发现子线程的堆栈变成了??? 而主线程的堆栈变成了子线程的堆栈。研究发现,应该是gethostbyname是在子线程调用的,而信号是主线程处理,导致子线程备份的堆栈内容被覆盖到了主线程里,引起了问题。 " "在主线程里屏蔽掉SIGALRM信号sigset_t mask,oldmask;sigemptyset(&mask);sigaddset(&mask,SIGALRM);pthread_sigmask(SIG_BLOCK,&mask,&oldmask);在子线程里解锁对信号的屏蔽
sigset_t mask,oldmask; sigemptyset(&mask); sigaddset(&mask,SIGALRM); pthread_sigmask(SIG_UNBLOCK,&mask,&oldmask);
继续运行,一些正常,可以按照配置的超时时间结束系统函数的阻塞。 " "补充:gethostbyname在多线程是不安全,但仅仅的结果会被冲掉,运行本身是安全的,因为这个函数是有锁的。所以如果多个线程运行gethostbyname,后面的线程会进入_L_lock_20,但是超时记录是在锁以后才计算的。比如AB线程同时运行,都被阻塞,A线程20秒后退出,B线程也会等待20秒后退出。
但是如果加上跳出,连运行都不安全了。继续做了多线程测试,发现如果多个线程阻塞,那么A线程跳出后,B线程因为没有得到解锁,会永远卡住。双阻塞:
Thread 2 (Thread 0x7ffff7fd6700 (LWP 13627)):#0 0x00000038b7af6dde in __lll_lock_wait_private () from /lib64/libc.so.6#1 0x00000038b7b02029 in _L_lock_20 () from /lib64/libc.so.6#2 0x00000038b7b01ea0 in gethostbyname () from /lib64/libc.so.6#3 0x000000000040099e in delete_file_svc (arg=0x0) at signtest.c:26#4 0x00000038b8207851 in start_thread () from /lib64/libpthread.so.0#5 0x00000038b7ae767d in clone () from /lib64/libc.so.6Thread 1 (Thread 0x7ffff7fd8720 (LWP 13626)):#0 0x00000038b7addfc3 in poll () from /lib64/libc.so.6#1 0x00000038b960bed4 in __libc_res_nsend () from /lib64/libresolv.so.2#2 0x00000038b960876a in __libc_res_nquery () from /lib64/libresolv.so.2#3 0x00000038b9608d29 in __libc_res_nquerydomain () from /lib64/libresolv.so.2#4 0x00000038b96099cf in __libc_res_nsearch () from /lib64/libresolv.so.2#5 0x00007ffff73a72d8 in _nss_dns_gethostbyname3_r () from /lib64/libnss_dns.so.2#6 0x00007ffff73a750e in _nss_dns_gethostbyname_r () from /lib64/libnss_dns.so.2#7 0x00000038b7b02733 in gethostbyname_r@@GLIBC_2.2.5 () from /lib64/libc.so.6#8 0x00000038b7b01f33 in gethostbyname () from /lib64/libc.so.6#9 0x0000000000400a8d in host_to_ip (host=0x400c9b "www.baidu.com") at signtest.c:83#10 0x0000000000400b4a in main () at signtest.c:118
跳出后
Thread 2 (Thread 0x7ffff7fd6700 (LWP 13627)):#0 0x00000038b7addfc3 in poll () from /lib64/libc.so.6#1 0x00000038b960bed4 in __libc_res_nsend () from /lib64/libresolv.so.2#2 0x00000038b960876a in __libc_res_nquery () from /lib64/libresolv.so.2#3 0x00000038b9608d29 in __libc_res_nquerydomain () from /lib64/libresolv.so.2#4 0x00000038b96099cf in __libc_res_nsearch () from /lib64/libresolv.so.2#5 0x00007ffff73a72d8 in _nss_dns_gethostbyname3_r () from /lib64/libnss_dns.so.2#6 0x00007ffff73a750e in _nss_dns_gethostbyname_r () from /lib64/libnss_dns.so.2#7 0x00000038b7b02733 in gethostbyname_r@@GLIBC_2.2.5 () from /lib64/libc.so.6#8 0x00000038b7b01f33 in gethostbyname () from /lib64/libc.so.6#9 0x000000000040099e in delete_file_svc (arg=0x0) at signtest.c:26#10 0x00000038b8207851 in start_thread () from /lib64/libpthread.so.0#11 0x00000038b7ae767d in clone () from /lib64/libc.so.6Thread 1 (Thread 0x7ffff7fd8720 (LWP 13626)):#0 0x00000038b7aab91d in nanosleep () from /lib64/libc.so.6#1 0x00000038b7aab790 in sleep () from /lib64/libc.so.6#2 0x0000000000400b5e in main () at signtest.c:121
可以看见B线程被永久锁死了。
所以如果dns性能有问题,不如自己写dns解析函数(简单处理设置禁止TCP,避免被要求TCP请求而导致解析失败),而不要使用跳出。
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言C/C+频道!