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

            色婷婷狠狠久久综合五月| 91精品国产9l久久久久| 亚洲一区中文字幕久久| 久久青青草原亚洲av无码app| 性做久久久久久久久浪潮| 久久天天日天天操综合伊人av| 精品国产热久久久福利| 久久综合色之久久综合| 日韩va亚洲va欧美va久久| 久久精品无码一区二区三区日韩| 国产精品久久久久乳精品爆| 久久精品国产清自在天天线| 免费精品久久久久久中文字幕 | 色狠狠久久AV五月综合| 久久久久高潮综合影院| 精品久久久无码人妻中文字幕豆芽 | 久久99精品久久久久久噜噜| 久久精品亚洲精品国产欧美| 亚洲Av无码国产情品久久| 亚洲精品tv久久久久| 99精品久久久久久久婷婷| 国产日产久久高清欧美一区| 国产2021久久精品| 人妻无码精品久久亚瑟影视 | 久久99热这里只有精品国产| 欧美一级久久久久久久大| 99久久国产宗和精品1上映 | 久久66热人妻偷产精品9| 国产精品成人久久久久三级午夜电影 | 无码国内精品久久综合88| 99久久免费国产特黄| 午夜福利91久久福利| 国产精品一久久香蕉国产线看观看| 国产精品美女久久久网AV| 亚洲女久久久噜噜噜熟女| 国产亚洲精久久久久久无码AV| 囯产极品美女高潮无套久久久 | 国产精品久久久久久久app | 久久久久久国产精品无码下载 | 久久久久se色偷偷亚洲精品av| 91精品国产高清久久久久久io|