郑州大型网站建设价格,网页设计与网站建设在线作业,彩虹网站建设,企业网站 优帮云系统调用可分为两类#xff1a;慢速系统调用和其他系统调用
慢速系统调用#xff1a;可能会使进程永远阻塞的一类#xff0c;如果在阻塞期间收到一个信号#xff0c;该系统调用就被中断#xff0c;不再继续执行(早期)#xff1b;也可以设定系统调用是否重启。如#xf…系统调用可分为两类慢速系统调用和其他系统调用
慢速系统调用可能会使进程永远阻塞的一类如果在阻塞期间收到一个信号该系统调用就被中断不再继续执行(早期)也可以设定系统调用是否重启。如read、write、pause、wait等等其他系统调用getpid、getppid、fork...
结合pause回顾慢速系统调用
想中断pause信号不能被屏蔽信号的处理方式必须是捕捉(默认、忽略都不可以)中断后返回-1设置errno为EINTR(表“被信号中断”
可修改sa_flags参数来设置被信号中断系统调用是否重启。SA_INTERRURT 不重启。SA_RESTART重启。
扩展了解
sa_flags还有许多可选参数适用不同情况如捕捉到信号后在执行捕捉函数期间不希望屏蔽该信号可将sa_flags设置为SA_NODEFER除非sa_mask中包含该信号。 1. 测试代码
#include stdio.h
#include stdlib.h
#include unistd.h
#include stdbool.h
#include signal.h
#include sys/types.h
#include errno.h
#include string.hvoid int_handler(int signum)
{printf(int handler %d\n, signum);
}int main(int argc, char **argv)
{char buf[100];ssize_t ret;struct sigaction oldact;struct sigaction act;act.sa_handler int_handler;act.sa_flags 0;act.sa_flags | SA_RESTART;sigemptyset(act.sa_mask);if (-1 sigaction(SIGINT, act, oldact)) {printf(sigaction failed!\n);return -1;}bzero(buf, 100);ret read(STDIN_FILENO, buf, 10);if (ret -1) {printf(read error %s\n, strerror(errno));}printf(read %d bytes, content is %s\n, (int)ret, buf);sleep(10);return 0;
}
输出结果 2. 测试代码
#include stdio.h
#include stdlib.h
#include unistd.h
#include stdbool.h
#include signal.h
#include sys/types.h
#include errno.h
#include string.hvoid int_handler(int signum)
{printf(int handler %d\n, signum);
}int main(int argc, char **argv)
{char buf[100];ssize_t ret;struct sigaction oldact;struct sigaction act;act.sa_handler int_handler;act.sa_flags 0;// act.sa_flags | SA_RESTART;sigemptyset(act.sa_mask);if (-1 sigaction(SIGINT, act, oldact)) {printf(sigaction failed!\n);return -1;}bzero(buf, 100);ret read(STDIN_FILENO, buf, 10);if (ret -1) {printf(read error %s\n, strerror(errno));}printf(read %d bytes, content is %s\n, (int)ret, buf);sleep(10);return 0;
}
输出结果 参考资料
1. linux SA_RESTART的问题
2. 44-中断系统调用与自动重启动