• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            大龍的博客

            常用鏈接

            統(tǒng)計

            最新評論

            gethostbyname timeout --- 轉(zhuǎn)

            總結(jié): 下面第一種方法經(jīng)過測試是可行的,第二種方法(多線程中使用)不能夠用信號中斷。

            so , 對信號的使用要非常小心。



            gethostbyname 是阻塞的, 非線程安全的,所以最好使用getaddrinfo , 它是線程安全的。

            要給gethostbyname 設(shè)置一個超時值, 用信號(alarm)實現(xiàn)是不行的(目前項目嵌入式平臺上面測試不行) 。


            下面是網(wǎng)上摘錄的解決該問題的方法, 對怎么繞過其他系統(tǒng)函數(shù)(無論是線程安全還是非安全的)也有很好的借鑒意義


            Hello,

            I need to add timeout to gethostbyname function in my program. I
            googled and searched archives, found some examples with alert(), but
            can't make it work :/ I would be grateful of someone could tell me how
            to do it.

            You can't. Or rather, you shouldn't.

            The trick with alarm(2) (not alert()), is to longjmp(3) from the signal
            handler to a context created with setjmp(3) before calling gethostbyname(3).
            This is, unfortunately, very common in Perl code (in Perl die() actually
            uses longjmp() internally to a context set from an eval statement).

            However, gethostbyname() keeps internal state, and afterward its unsafe
            to call the function again. gethostbyname_r() might be relatively safer,
            but you've probably also leaked a file descriptor and memory,
            meaning you could only do it so many times from a single process before it
            won't work anymore. (And from a strict C perspective, jumping from the
            signal handler itself is questionable.)

            You do have options.

            1) Multiple processes. Use child processes and the
            gethostbyname()+alarm() trick, and return the answer down a pipe to
            the parent. The child will just kill itself if it timeouts (or
            alternatively the parent will set the alarm and kill the child), since it
            cannot reliably do it's job afterward.

            2) Use threads in conjunction with gethostbyname_r(), or preferably
            getaddrinfo(3). Still, you cannot interrupt these functions within the
            thread, and so you have a few more decisions to make in terms of how you
            handle timeouts.

            3) Use a third-party asynchronous DNS library: ADNS, C-Ares and UDNS are
            the first ones which comes to mind, the former two probably being the most
            popular.

            posted on 2012-11-12 14:45 大龍 閱讀(3285) 評論(0)  編輯 收藏 引用

            久久久久国产精品熟女影院| 欧美国产精品久久高清| 久久国产精品国语对白| 久久久久久亚洲Av无码精品专口| 亚洲国产精品无码久久青草| 国产午夜福利精品久久| 久久青草国产手机看片福利盒子| 久久精品人人做人人爽97| 久久久久久久97| 亚洲日韩欧美一区久久久久我| 久久99国产一区二区三区| 久久99精品九九九久久婷婷| 久久国产成人午夜aⅴ影院 | 亚洲国产成人久久精品动漫| 久久精品国产亚洲αv忘忧草 | 久久精品亚洲日本波多野结衣 | 色综合久久无码中文字幕| 亚洲欧洲日产国码无码久久99| 99蜜桃臀久久久欧美精品网站| 久久午夜夜伦鲁鲁片免费无码影视| 日韩十八禁一区二区久久| 久久这里有精品| 久久亚洲精品国产精品| 久久精品欧美日韩精品| 久久夜色精品国产亚洲| 久久噜噜久久久精品66| 一97日本道伊人久久综合影院| 精品无码久久久久国产动漫3d| 亚洲欧美日韩中文久久| 久久精品夜夜夜夜夜久久| 久久噜噜电影你懂的| 久久综合给合综合久久| 亚洲国产精品无码成人片久久| 国产V亚洲V天堂无码久久久| 办公室久久精品| 欧美与黑人午夜性猛交久久久| 久久无码AV中文出轨人妻| 久久99精品国产99久久6男男| 久久99亚洲综合精品首页| 国产美女亚洲精品久久久综合| 久久91精品国产91久久户|