1. 程式人生 > >設定TCP connect超時時間的2種方法

設定TCP connect超時時間的2種方法

1.常用方法

設定socket非阻塞,之後使用select等設定超時時間

2.使用alarm訊號量

需要注意:執行緒訊號量掩碼是執行緒私有的,當指定程序遞交訊號量時,作業系統會將訊號量遞交至該程序中未遮蔽該訊號量的所有執行緒中的隨機之一。

見 man 7 signal:

Asignalmay be generated (and thus pending) for a process as a whole

(e.g., when sent using kill(2)) or for a specific thread (e.g., certain

signals, such as SIGSEGV and SIGFPE

, generated as a consequence of exe-

cuting a specific machine-language instruction are thread directed,as

aresignalstargetedat a specific thread using pthread_kill(3)).A

process-directed signal may be delivered to any one of the threads that

doesnotcurrentlyhave the signal blocked.If more than one of the

threads has the signal unblocked, then the kernel chooses anarbitrary

thread to which to deliver the signal.



皆抄襲自《TCP高效程式設計》