Usleep do not quit the discovery that has been sleeping

Source: Internet
Author: User
Tags usleep

The pre-write code encounters a situation where usleep will die under multiple threads.
Look at the log is the thread stuck, so gdb went in to see, but did not find the deadlock, or normal stay in the usleep, and finally found that it is usleep in the back.

At the end of the week, an experiment was made, and it seemed that the machine had something to do with the other machine.
Also found a situation, that is, although Usleep will die, but after about 50 minutes, will be withdrawn from the usleep. There is no permanent card death situation. Each occurrence is stuck for 50 minutes after recovery, but my program also does not have a 50-minute timer.

It is found that 2 threads may be stuck in a loop that is usleep (1000).
Try to try to print out the return value of the Usleep, and find out if the card is dead, there is no return failure. Nor did any one of the usleep return a failure.
Not all machines will appear, very inexplicably. This kind of problem has never been seen on the line before.

Very strange, so mass mail to colleagues, some people say makes clean a, some people do not believe that usleep will be backward.

"Delete all the target files of the machine and make a try again."
-------------------------
The problem of the machine is not compiled code, just put the compiled program up. 2 machines are running the same execution program. The operating system is the same.

"encountered, but the phenomenon is not the same, the card will not come out after death."
The problem is the __fd_setsize defined in/usr/include/bits/typesizes.h and the inconsistencies defined in the process. ”
-----------------------
See the next 2 systems __fd_setsize are the same, the process is not redefined __fd_setsize

This is said to be related to SIGALRM signal processing. Logically, each thread in Linux has a process number, and the usleep signal should be sent to the thread's process number. However, if you are sending to a process group, there may be a problem because the signal does not accumulate.
To do a test can be confirmed, write a signal capture function, put into the main process, and so caught sigalrm, if it should be a thread, it may be a problem. ”
--------------------
So I wrote a simple program.
Found no alarm in usleep.

void Sigalarmhandler (int sig)
{
printf ("\ n alarm \ n");
} int main ()
{
signal (SIGALRM, sigalarmhandler);
While (1)
 {
usleep (1);
 }
return 0;
} to run without any printing. It is estimated that the alarm signal emitted by the usleep is handled separately. Because Usleep is a library function, it is implemented by calling Nanosleep.

"There may be a problem with usleep and sleep mixing."
--------------------
I've changed all the threads to usleep, or I'll be there.

The final solution is to use Select to replace the Usleep, but strangely, GDB debugging when the advantages of the card.

By the way, a precision test was made, which found that the precision of select was better than usleep between 0.1 milliseconds and 1 milliseconds. More than 1 milliseconds, the difference is not small.

There was also a time when select appeared 1 milliseconds to get stuck for 1 seconds.


Percent of error 100 microseconds to 10000 microseconds
(Start time, end time, true error, theoretical error, difference value, percent of difference)

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%


This article is from "Flying Justice Blog" blog, please be sure to keep this source http://xzq2000.blog.51cto.com/2487359/1767396

Usleep do not quit the discovery that has been sleeping

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.