操作这个结构体有一点要注意, sa_handler和sigaction是在一个union里的,只有一个能赋值,原文如下: On some architectures a union is involved: do not assign to both sa_handler and sa_sigaction.
使用这个结构体的时候先bzero清空整个结构体,然后赋值sa_handler即可,不需要赋值sa_mask,那是信号处理中的信号掩码, 通常是不需要额外屏蔽什么信号的, 默认就会屏蔽当前被处理的信号,原文如下: sa_mask specifies a mask of signals which should be blocked (i.e., added to the signal mask of the thread in which the signal handler is invoked) during execution of the signal handler. In addition, the signal which triggered the handler will be blocked, unless the SA_NODEFER flag is used.
另外,
int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact);
sigprocmask //屏蔽所有信号 if (buffer空) { ret = write(fd, msg, len); if (ret == -1) memcpy(buffer, msg, len); else if (ret < len) memcpy(buffer, msg+ret, len - ret);
if (ret == -1 || ret < len) 添加fd的写事件; } else { memcpy(buffer + buffer_cur_len, msg, len); } sigprocmask //解除屏蔽
这是信号处理函数里的判定:
if (buffer is not empty) { memcpy(buffer + buffer_cur_len , local msg, len); } else { ret = write(fd, local msg, len); if (ret == -1) memcpy(buffer, local_msg, len); else if (ret < len) memcpy(buffer, local_msg+ret, len-ret);
if (ret == -1 || ret < len) 注册fd的写事件; }
------解决方案--------------------