• <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>

            大龍的博客

            常用鏈接

            統計

            最新評論

            gethostbyname timeout --- 轉

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

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



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

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


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


            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 大龍 閱讀(3293) 評論(0)  編輯 收藏 引用

            久久亚洲AV无码精品色午夜麻豆| 久久久久久极精品久久久 | 久久久久亚洲AV无码麻豆| 久久人人爽人人爽人人AV东京热| 亚洲一本综合久久| 亚洲日本久久久午夜精品| 久久久久亚洲AV片无码下载蜜桃| 久久久久久免费一区二区三区| 久久夜色精品国产亚洲av| 亚洲国产另类久久久精品黑人| 久久电影网2021| 日韩精品久久久肉伦网站| 人人狠狠综合88综合久久| www久久久天天com| 亚洲乱码中文字幕久久孕妇黑人| 中文字幕久久欲求不满| 久久国产精品77777| 久久久久久久女国产乱让韩| 国产精品美女久久久久av爽| 狠狠88综合久久久久综合网 | 色婷婷久久综合中文久久蜜桃av| 国产免费久久精品丫丫| 999久久久免费精品国产| 中文字幕无码免费久久| 日韩亚洲国产综合久久久| 久久国产视屏| 久久久久无码中| 久久中文字幕无码专区| 国内精品久久久久国产盗摄| 国产ww久久久久久久久久| 日本三级久久网| 久久99精品九九九久久婷婷| 久久男人AV资源网站| 欧美粉嫩小泬久久久久久久| 久久久久人妻一区精品| 久久久久久无码国产精品中文字幕 | 久久久久久免费视频| 性做久久久久久久久久久| 一本色综合久久| 久久无码人妻一区二区三区午夜| 久久偷看各类wc女厕嘘嘘|