加入收藏 | 设为首页 | 会员中心 | 我要投稿 温州站长网 (https://www.0577zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 服务器 > 搭建环境 > Linux > 正文

c – SIGXFSZ是由内核发送的,除非有什么内容打印到stdout?

发布时间:2021-01-12 12:08:54 所属栏目:Linux 来源:网络整理
导读:我正在学习“Unix环境下的高级编程”,并且在第10章的练习11中遇到了问题. 在我的程序中,我将RLIMIT_FSIZE设置为1024. 所以当写入试图超过该限制时,内核应该将SIGXFSZ发送到我的程序. 但我发现SIGXFSZ不发送,除非打印到stdout的东西. 这是我的代码: #inclu

我正在学习“Unix环境下的高级编程”,并且在第10章的练习11中遇到了问题.

在我的程序中,我将RLIMIT_FSIZE设置为1024.

所以当写入试图超过该限制时,内核应该将SIGXFSZ发送到我的程序.

但我发现SIGXFSZ不发送,除非打印到stdout的东西.

这是我的代码:

#include 

如果我在代码中注释掉以下行,内核将不会传输SIGXFSZ.

printf("What ever . . . n");

为什么会这样?提前致谢.

[root@luaDevelopment ex11]# ./myCopy < /root/workspace/AdvanceProgrammingInTheUnixEnvironment.20140627.tar.bz2 >aa.tar.bz2
byteWrite=24,n=100
[root@luaDevelopment ex11]# make
gcc -o myCopy myCopy.c -std=gnu99 -I../../lib/ -L../../lib/ -lch10
[root@luaDevelopment ex11]# ./myCopy < /root/workspace/AdvanceProgrammingInTheUnixEnvironment.20140627.tar.bz2 >aa.tar.bz2
byteWrite=24,n=100
25,File size limit exceeded
[root@luaDevelopment ex11]#
最佳答案 user3693690在本书附录C中找到了答案:

10.11 Under Linux 3.2.0,Mac OS X 10.6.8,and Solaris 10,the signal handler for SIGXFSZ is never called [because the loop exits the program on a short write],but write returns a count of 24 as soon as the file’s size reaches 1,024 bytes. When the file’s size has reached 1,000 bytes under FreeBSD 8.0,the signal handler is called on the next attempt to write 100 bytes,and the write call returns ?1 with errno set to EFBIG(‘‘File too big’’). On all four platforms,if we attempt an additional write at the current file offset (the end of the file),we will receive SIGXFSZ and write will fail,returning?1 with errno set to EFBIG.

如果这是Linux内核用来处理SIGXFSZ的方式,那就这样吧,但我觉得这很奇怪.

(编辑:温州站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读