TEMP_FAILURE_RETRY,__extension,MSG_NOSIGNAL

#define TEMP_FAILURE_RETRY(expression) \
  (__extension__\
   ({ long int __result;\
       do __result = (long int)(expression);\
       while(__result == -1L&& errno == EINTR);\
       __result;})\
#endif

关于使用TEMP_FAILURE_RETRY

This macro evaluates expression once, and examines its value as type long int. If the value equals -1, that indicates a failure and errno should be set to show what kind of failure. If it fails and reports error code EINTR,TEMP_FAILURE_RETRY evaluates it again, and over and over until the result is not a temporary failure.

从代码里看到,当while为真时,会不断执行表达式,例如,我们可以在我们的套接字里不断读取数据

int ret = TEMP_FAILURE_RETRY(::recv(sock,buffer,size,MSG_NOSIGNAL));

这样程序得一直读到数据返回,

使用MSG_NOSIGNAL

linux下当连接断开,还发数据的时候,会向系统发送一个异常消息,如果不作处理,系统会出 BrokePipe,程序会退出。为此,函数的最后一个参数可以设MSG_NOSIGNAL,禁止向系统发送异常消息。

使用errno

表达式中函数作为不可重入函数,执行过程中,被信号中断,包含下面两种情况

Interruption of System Calls and Library Functions by Signal Handlers

If a signal handler is invoked while a system call or library function call is blocked, then either:

the call is automatically restarted after the signal handler returns; or the call fails with the error EINTR.



关于使用__extension
GCC uses the __extension__ attribute when using the -ansi flag to avoid
warnings in headers with GCC extensions.  This is mostly used in glibc
with function declartions using long long. 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值