UNIX环境高级编程第十章
程序清单 10-22
理了半天好不容易看得有点头绪了,抓紧记录一下。
#include "apue.h"
#define BUFFSIZE 1024
static void sig_tstp(int);
int main(void)
{
int n;
char buf[BUFFSIZE];
if(signal(SIGTSTP, SIG_IGN) == SIG_DFL) //后来收到交互停止作业信号是SIG_DFL的
signal(SIGTSTP, tstp); //跑到tstp函数去执行
while ((n = read(STDIN_FILENO, buf, BUFFSIZE))>0) //一开始没收到信号一直在这读在这写
if(write(STDOUT_FILENO, buf, n) != n)
err_sys("write error");
if(n<0)
err_sys("read error");
exit(0);
}
static void sig_tstp(int signo)
{
sigset_t mask; //定义信号集
sigemptyset(&mask); //清空信号集
sigaddset(&mask , SIGTSTP); //把SIGTSTP加到信号集mask中去,
sigprocmask(SIG_UNBLOCK, &mask, NULL); //解除mask的阻塞
signal( SIGTSTP, SIG_DFL); //把捕捉到的信号STGTSTP的操作设置问为SIG_DFL
kill(getpid(), SIGTSTP); //把SIGTSTP发送给自己,因为在捕捉到该信号期间系统会阻塞SIGTSTP,
signal(SIGTSTP, sig_tstp);)
}

浙公网安备 33010602011771号