usleep不退出一直沉睡的发现-成都快上网建站

usleep不退出一直沉睡的发现

前阵子写代码遇到多线程下usleep会卡死的情况。
看日志是线程卡死,于是gdb进去看,但是没有发现死锁,还是正常的停留在usleep中了,最后才发现是usleep进去后出不来了。

在安新等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供网站建设、成都网站制作 网站设计制作定制网站设计,公司网站建设,企业网站建设,品牌网站设计,成都全网营销,外贸网站建设,安新网站建设费用合理。

周末做了个实验,发现似乎跟机器有关,另外一台机器就一直没有出现这种情况。
还发现了一个情况,就是虽然usleep会卡死,但是过大概50分钟,就会从usleep中退出来。并没有出现永远卡死的情况。每次出现都是卡住50分钟后恢复,但是我的程序里面也没有50分钟的定时器。

发现有2个线程可能会卡死,都是循环中usleep(1000)的。
尝试把试着把usleep的返回值打印出来,发现是否卡死后退出,都没有返回失败。也没有任何一个usleep返回过失败。
并不所有的机器都会出现,很莫名其妙。以前线上也从来没有出现过这类问题。

很奇怪,于是群发邮件给同事,有人说make clean一把,有人不相信usleep会退步出来。

“把出问题那台机器的目标文件全部删掉,重新make一下试试;”
-------------------------
出问题的机器没有编译代码,只是把编译后的程序放上去了。2台机器运行的都是同样的执行程序。操作系统也同样。

“遇到过,但现象不一样,是卡死之后不会出来。
问题在于/usr/include/bits/typesizes.h中定义的__FD_SETSIZE和进程中定义的不一致。”
-----------------------
看了下2个系统的__FD_SETSIZE都一样,进程里面也没有重新定义__FD_SETSIZE

“这上面说是与SIGALRM信号处理有关。按理说linux里每个线程 都有进程号,usleep发的信号应该是发到线程的进程号上。但如果是发到进程组,可能会有问题,因为信号不会积累。
做个测试就可以确认了,写个信号捕捉函数,放到主进程,等抓到SIGALRM 后,如果应该是线程的,那就可能出问题。”
--------------------
于是我写了个简单的程序。
发现捕获不了usleep里面的alarm的信号

 void sigAlarmhandler(int sig)
{
 printf("\n alarm \n ");
} int main()
{
 signal(SIGALRM, sigAlarmhandler);
 while(1)
 {
  usleep(1);
 }
 return 0;
}运行,没有任何打印。估计是usleep发出的alarm信号有单独处理。因为usleep是个库函数,通过调用nanosleep实现。

 

“usleep和sleep混用可能有问题”
--------------------
我把所有线程的空闲都改成了usleep,还是会出现。

 

最后的解决办法是用select来替代usleep,不过很奇怪,gdb调试的时候变的优点卡。

顺便还做了个精度测试,发现在0.1毫秒到1毫秒之间select的精度比usleep好。超过1毫秒,都相差不大。

期间还有一次select出现了1毫秒卡住1秒的情况。


 

为100微秒到10000微秒的误差百分比
(开始时间,结束时间,真实误差,理论误差,差值,差值百分比)

select:975367,974213:1154:100:-1054,percent=-1054%
usleep:977413,975517:1896:100:-1796,percent=-1796%
select:978439,977463:976:200:-776,percent=-388%
usleep:980478,978486:1992:200:-1792,percent=-896%
select:981500,980526:974:300:-674,percent=-224%
usleep:983532,981547:1985:300:-1685,percent=-561%
select:984558,983581:977:400:-577,percent=-144%
usleep:986592,984609:1983:400:-1583,percent=-395%
select:987611,986640:971:500:-471,percent=-94%
usleep:988632,987658:974:500:-474,percent=-94%
select:989657,988679:978:600:-378,percent=-63%
usleep:990680,989705:975:600:-375,percent=-62%
select:991705,990727:978:700:-278,percent=-39%
usleep:993743,991752:1991:700:-1291,percent=-184%
select:994768,993789:979:800:-179,percent=-22%
usleep:996806,994815:1991:800:-1191,percent=-148%
select:997832,996853:979:900:-79,percent=-8%
usleep:999934,997879:2055:900:-1155,percent=-128%
select:895,999982:-999087:1000:1000087,percent=100008%
usleep:2928,942:1986:1000:-986,percent=-98%
select:4969,2974:1995:1100:-895,percent=-81%
usleep:7006,5016:1990:1100:-890,percent=-80%
select:9045,7052:1993:1200:-793,percent=-66%
usleep:11085,9092:1993:1200:-793,percent=-66%
select:13125,11132:1993:1300:-693,percent=-53%
usleep:16253,13172:3081:1300:-1781,percent=-137%
select:18227,16301:1926:1400:-526,percent=-37%
usleep:21275,18275:3000:1400:-1600,percent=-114%
select:23315,21322:1993:1500:-493,percent=-32%
usleep:26374,23362:3012:1500:-1512,percent=-100%
select:28414,26420:1994:1600:-394,percent=-24%
usleep:31467,28461:3006:1600:-1406,percent=-87%
select:33506,31514:1992:1700:-292,percent=-17%
usleep:36564,33553:3011:1700:-1311,percent=-77%
select:38603,36616:1987:1800:-187,percent=-10%
usleep:41654,38650:3004:1800:-1204,percent=-66%
select:43694,41701:1993:1900:-93,percent=-4%
usleep:46750,43741:3009:1900:-1109,percent=-58%
select:48789,46798:1991:2000:9,percent=0%
usleep:51844,48836:3008:2000:-1008,percent=-50%
select:54902,51891:3011:2100:-911,percent=-43%
usleep:57956,54949:3007:2100:-907,percent=-43%
select:61006,58002:3004:2200:-804,percent=-36%
usleep:64066,61054:3012:2200:-812,percent=-36%
select:67139,64113:3026:2300:-726,percent=-31%
usleep:71186,67187:3999:2300:-1699,percent=-73%
select:74249,71233:3016:2400:-616,percent=-25%
usleep:78311,74298:4013:2400:-1613,percent=-67%
select:81366,78319:3047:2500:-547,percent=-21%
usleep:85437,81374:4063:2500:-1563,percent=-62%
select:88512,85445:3067:2600:-467,percent=-17%
usleep:92585,88519:4066:2600:-1466,percent=-56%
select:95629,92592:3037:2700:-337,percent=-12%
usleep:98685,95637:3048:2700:-348,percent=-12%
select:101741,98692:3049:2800:-249,percent=-8%
usleep:105803,101753:4050:2800:-1250,percent=-44%
select:108867,105811:3056:2900:-156,percent=-5%
usleep:112937,108874:4063:2900:-1163,percent=-40%
select:115989,112945:3044:3000:-44,percent=-1%
usleep:120056,115997:4059:3000:-1059,percent=-35%
select:124130,120064:4066:3100:-966,percent=-31%
usleep:128199,124137:4062:3100:-962,percent=-31%
select:132272,128206:4066:3200:-866,percent=-27%
usleep:137361,132280:5081:3200:-1881,percent=-58%
select:141429,137368:4061:3300:-761,percent=-23%
usleep:146507,141436:5071:3300:-1771,percent=-53%
select:150591,146514:4077:3400:-677,percent=-19%
usleep:154656,150598:4058:3400:-658,percent=-19%
select:158730,154663:4067:3500:-567,percent=-16%
usleep:162796,158738:4058:3500:-558,percent=-15%
select:166896,162804:4092:3600:-492,percent=-13%
usleep:171956,166904:5052:3600:-1452,percent=-40%
select:176039,171964:4075:3700:-375,percent=-10%
usleep:181117,176046:5071:3700:-1371,percent=-37%
select:185191,181124:4067:3800:-267,percent=-7%
usleep:190281,185199:5082:3800:-1282,percent=-33%
select:194356,190288:4068:3900:-168,percent=-4%
usleep:199434,194363:5071:3900:-1171,percent=-30%
select:203505,199441:4064:4000:-64,percent=-1%
usleep:208602,203512:5090:4000:-1090,percent=-27%
select:213697,208609:5088:4100:-988,percent=-24%
usleep:218775,213704:5071:4100:-971,percent=-23%
select:223871,218783:5088:4200:-888,percent=-21%
usleep:228958,223879:5079:4200:-879,percent=-20%
select:234046,228965:5081:4300:-781,percent=-18%
usleep:239140,234054:5086:4300:-786,percent=-18%
select:244221,239147:5074:4400:-674,percent=-15%
usleep:250321,244228:6093:4400:-1693,percent=-38%
select:255427,250328:5099:4500:-599,percent=-13%
usleep:261523,255435:6088:4500:-1588,percent=-35%
select:265585,261530:4055:4600:545,percent=11%
usleep:270671,265593:5078:4600:-478,percent=-10%
select:275766,270678:5088:4700:-388,percent=-8%
usleep:281862,275774:6088:4700:-1388,percent=-29%
select:286951,281870:5081:4800:-281,percent=-5%
usleep:293058,286958:6100:4800:-1300,percent=-27%
select:298154,293065:5089:4900:-189,percent=-3%
usleep:304253,298161:6092:4900:-1192,percent=-24%
select:309335,304261:5074:5000:-74,percent=-1%
usleep:315429,309342:6087:5000:-1087,percent=-21%
select:321532,315437:6095:5100:-995,percent=-19%
usleep:327621,321540:6081:5100:-981,percent=-19%
select:333732,327628:6104:5200:-904,percent=-17%
usleep:339830,333739:6091:5200:-891,percent=-17%
select:345931,339837:6094:5300:-794,percent=-14%
usleep:352034,345939:6095:5300:-795,percent=-15%
select:358137,352042:6095:5400:-695,percent=-12%
usleep:365254,358146:7108:5400:-1708,percent=-31%
select:371365,365261:6104:5500:-604,percent=-10%
usleep:378475,371373:7102:5500:-1602,percent=-29%
select:383576,378482:5094:5600:506,percent=9%
usleep:389674,383584:6090:5600:-490,percent=-8%
select:395777,389682:6095:5700:-395,percent=-6%
usleep:402893,395785:7108:5700:-1408,percent=-24%
select:408999,402900:6099:5800:-299,percent=-5%
usleep:416116,409006:7110:5800:-1310,percent=-22%
select:422214,416124:6090:5900:-190,percent=-3%
usleep:429329,422222:7107:5900:-1207,percent=-20%
select:435457,429338:6119:6000:-119,percent=-1%
usleep:443563,435465:8098:6000:-2098,percent=-34%
select:450696,443570:7126:6100:-1026,percent=-16%
usleep:457798,450704:7094:6100:-994,percent=-16%
select:464944,457806:7138:6200:-938,percent=-15%
usleep:472052,464952:7100:6200:-900,percent=-14%
select:479164,472060:7104:6300:-804,percent=-12%
usleep:487296,479172:8124:6300:-1824,percent=-28%
select:494424,487304:7120:6400:-720,percent=-11%
usleep:502557,494432:8125:6400:-1725,percent=-26%
select:509678,502565:7113:6500:-613,percent=-9%
usleep:516792,509685:7107:6500:-607,percent=-9%
select:523917,516799:7118:6600:-518,percent=-7%
usleep:532050,523925:8125:6600:-1525,percent=-23%
select:539177,532057:7120:6700:-420,percent=-6%
usleep:547311,539186:8125:6700:-1425,percent=-21%
select:554421,547319:7102:6800:-302,percent=-4%
usleep:562539,554428:8111:6800:-1311,percent=-19%
select:569668,562550:7118:6900:-218,percent=-3%
usleep:577890,569675:8215:6900:-1315,percent=-19%
select:585017,577897:7120:7000:-120,percent=-1%
usleep:593148,585024:8124:7000:-1124,percent=-16%
select:601286,593155:8131:7100:-1031,percent=-14%
usleep:610427,601294:9133:7100:-2033,percent=-28%
select:617592,610434:7158:7200:42,percent=0%
usleep:625685,617599:8086:7200:-886,percent=-12%
select:633827,625692:8135:7300:-835,percent=-11%
usleep:641960,633835:8125:7300:-825,percent=-11%
select:650103,641968:8135:7400:-735,percent=-9%
usleep:659254,650111:9143:7400:-1743,percent=-23%
select:667420,659261:8159:7500:-659,percent=-8%
usleep:676540,667428:9112:7500:-1612,percent=-21%
select:684680,676548:8132:7600:-532,percent=-7%
usleep:692809,684688:8121:7600:-521,percent=-6%
select:700957,692817:8140:7700:-440,percent=-5%
usleep:710098,700965:9133:7700:-1433,percent=-18%
select:718237,710106:8131:7800:-331,percent=-4%
usleep:727389,718244:9145:7800:-1345,percent=-17%
select:735547,727397:8150:7900:-250,percent=-3%
usleep:744653,735555:9098:7900:-1198,percent=-15%
select:752804,744661:8143:8000:-143,percent=-1%
usleep:761970,752812:9158:8000:-1158,percent=-14%
select:771129,761979:9150:8100:-1050,percent=-12%
usleep:780270,771137:9133:8100:-1033,percent=-12%
select:789430,780278:9152:8200:-952,percent=-11%
usleep:798564,789438:9126:8200:-926,percent=-11%
select:807723,798572:9151:8300:-851,percent=-10%
usleep:816867,807731:9136:8300:-836,percent=-10%
select:826032,816874:9158:8400:-758,percent=-9%
usleep:836191,826039:10152:8400:-1752,percent=-20%
select:845370,836199:9171:8500:-671,percent=-7%
usleep:855507,845378:10129:8500:-1629,percent=-19%
select:863634,855515:8119:8600:481,percent=5%
usleep:872779,863641:9138:8600:-538,percent=-6%
select:881929,872787:9142:8700:-442,percent=-5%
usleep:892091,881937:10154:8700:-1454,percent=-16%
select:901251,892099:9152:8800:-352,percent=-4%
usleep:911417,901259:10158:8800:-1358,percent=-15%
select:919554,911424:8130:8900:770,percent=8%
usleep:929710,919567:10143:8900:-1243,percent=-13%
select:938868,929717:9151:9000:-151,percent=-1%
usleep:949031,938876:10155:9000:-1155,percent=-12%
select:959200,949039:10161:9100:-1061,percent=-11%
usleep:970374,959207:11167:9100:-2067,percent=-22%
select:979568,970382:9186:9200:14,percent=0%
usleep:989699,979575:10124:9200:-924,percent=-10%
select:999877,989707:10170:9300:-870,percent=-9%
usleep:10042,999885:-989843:9300:999143,percent=10743%
select:20206,10049:10157:9400:-757,percent=-8%
usleep:31391,20214:11177:9400:-1777,percent=-18%
select:40555,31399:9156:9500:344,percent=3%
usleep:50716,40562:10154:9500:-654,percent=-6%
select:60890,50724:10166:9600:-566,percent=-5%
usleep:72061,60897:11164:9600:-1564,percent=-16%
select:82227,72068:10159:9700:-459,percent=-4%
usleep:93418,82235:11183:9700:-1483,percent=-15%
select:102569,93425:9144:9800:656,percent=6%
usleep:113743,102582:11161:9800:-1361,percent=-13%
select:123925,113751:10174:9900:-274,percent=-2%
usleep:135098,123933:11165:9900:-1265,percent=-12%


当前题目:usleep不退出一直沉睡的发现
本文链接:http://kswjz.com/article/pcsppj.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流