免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1596 | 回复: 3
打印 上一主题 下一主题

[Linux] unix网络编程的suspend函数的一些疑问 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-07-06 17:45 |只看该作者 |倒序浏览

sigset_t zeromask,newmask,oldmask;
12     sigemptyset(&zeromask);
13     sigemptyset(&newmask);
14     sigemptyset(&oldmask);
15
16     sigaddset(&newmask,SIGUSR1);
17     sigprocmask(SIG_BLOCK,&newmask,&oldmask);//block SIGUSR1
18
19     signal(SIGUSR1,sig_usr1);
20     printf("sleep before\n");
21     sleep(25);
22     printf("sleep after \n");

while(flag==0){
25 sigsuspend(&zeromask);//unblock SIGUSR1
26  flag=0;
27 }

static void sig_usr1(int signo)
54 {
55     flag=1;
56     printf("hello the wolrd\n");
57 }
如果在17行阻塞SIGUSR1和25行unblock SIGUSR1之间发个信号SIGUSR1,这个信号会被丢弃还是等执行到25行在执行信号处理函数?
              

论坛徽章:
4
水瓶座
日期:2013-09-06 12:27:30摩羯座
日期:2013-09-28 14:07:46处女座
日期:2013-10-24 14:25:01酉鸡
日期:2014-04-07 11:54:15
2 [报告]
发表于 2013-07-06 19:16 |只看该作者
非实时信号不会排队, 同一种信号只会保留一个.

你说的情况, 工作过程是: sigsuspend先挂起(原子替换信号掩码为zeromask), 然后信号被处理, 然后sigsuspend醒来(原子替换信号为newmask), 你检查变量成立, 退出while, 然后你恢复信号, 结束程序.

论坛徽章:
4
白羊座
日期:2013-09-17 21:59:30技术图书徽章
日期:2013-10-12 22:16:03白羊座
日期:2013-10-14 11:01:40双子座
日期:2013-12-17 18:26:39
3 [报告]
发表于 2013-07-06 20:48 |只看该作者
回复 1# surpassgood
应当分为两段看,17-19为一段,19-25为一段。
19-25之间发生信号的话,应当是等执行到25行时执行信号处理函数。
17-19之间发生的话:
apue中是这样说的
  1. If a signal that is blocked is generated for a process, and if the action for that signal is either the default action or to catch the signal, then the signal remains pending for the process until the process either (a) unblocks the signal or (b) changes the action to ignore the signal.
复制代码
posix是这样说的
  1. An implementation registers a signal as pending on the process when no thread has the signal unblocked and there are no threads blocked in a sigwait() function for that signal. Thereafter, the implementation delivers the signal to the first thread that unblocks the signal or calls a sigwait() function on a signal set containing this signal rather than choosing the recipient thread at the time the signal is sent.
复制代码
  1. In the 4.3 BSD system, signals that are blocked and set to SIG_IGN are discarded immediately upon generation. For a signal that is ignored as its default action, if the action is SIG_DFL and the signal is blocked, a generated signal remains pending. In the 4.1 BSD system and in System V Release 3 (two other implementations that support a somewhat similar signal mechanism), all ignored blocked signals remain pending if generated. Because it is not normally useful for an application to simultaneously ignore and block the same signal, it was unnecessary for POSIX.1 to specify behavior that would invalidate any of the historical implementations.
复制代码
从上面可以看出只要不是太早的系统的话,apue说的应当就是对的。
对于你的问题中,就是:如果之前对于信号的处理是SIG_IGN,17-19之间产生的信号就会丢弃,否则就是等到25行时调用信号处理程序。


   

论坛徽章:
0
4 [报告]
发表于 2013-07-07 08:48 |只看该作者
thank you 回复 3# 井蛙夏虫


   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP