设置TCP connect超时时间的2种方法

本文介绍如何通过设置socket为非阻塞模式并结合select实现超时处理的方法。此外,还详细讨论了在多线程环境中使用alarm信号量时需要注意的问题,包括信号量的传递机制以及操作系统如何选择接收信号的线程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.常用方法

设置socket非阻塞,之后使用select等设置超时时间

2.使用alarm信号量

需要注意:线程信号量掩码是线程私有的,当指定进程递交信号量时,操作系统会将信号量递交至该进程中未屏蔽该信号量的所有线程中的随机之一。

见 man 7 signal:

  A  signal  may 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

       are  signals  targeted  at a specific thread using pthread_kill(3)).  A

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

       does  not  currently  have the signal blocked.  If more than one of the

       threads has the signal unblocked, then the kernel chooses an  arbitrary

       thread to which to deliver the signal.



皆抄袭自《TCP高效编程》

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值