• <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)  編輯 收藏 引用

            久久免费精品视频| 99久久国产热无码精品免费久久久久| 久久精品无码专区免费青青| 国产亚洲色婷婷久久99精品91| 久久99精品久久久久久动态图| 久久中文字幕人妻丝袜| 亚洲伊人久久大香线蕉苏妲己| 99久久精品国产免看国产一区| 亚洲色婷婷综合久久| 亚洲人成伊人成综合网久久久| 久久国产劲爆AV内射—百度| 久久成人小视频| 日韩精品久久久久久免费| 狠狠综合久久综合88亚洲| 日韩精品久久无码人妻中文字幕| 欧美丰满熟妇BBB久久久| 久久99国内精品自在现线| 国内精品久久久久久野外| 91精品国产91久久久久久青草| 丁香五月综合久久激情| 欧美与黑人午夜性猛交久久久| 日韩中文久久| 亚洲色欲久久久综合网东京热 | 久久精品亚洲精品国产欧美| 伊人久久大香线焦综合四虎| 韩国三级中文字幕hd久久精品 | 久久国产精品一区| 伊人久久成人成综合网222| 99精品久久久久久久婷婷| 久久久久久久99精品免费观看| 国产精品一区二区久久精品无码 | 欧洲国产伦久久久久久久| 亚洲综合伊人久久综合| 9久久9久久精品| 久久精品女人天堂AV麻| 男女久久久国产一区二区三区| 国产2021久久精品| 日韩精品久久久久久免费| 激情久久久久久久久久| 久久久亚洲欧洲日产国码二区| 久久精品国产亚洲av瑜伽|