I am studying Advanced Unix Programming and have a problem with exercise 11 in chapter 10 .
In my program, I set RLIMIT_FSIZE to 1024 .
Therefore, the kernel should send SIGXFSZ to my program when writing, trying to exceed this limit.
But I found that SIGXFSZ not sent unless something is printed on stdout .
Here is my code:
#include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/time.h> #include <sys/resource.h> #include <signal.h> #define BUFFSIZE 100 void xfsz_handler(int signo) { fprintf(stderr, "%d, %s\n", signo, strsignal(signo)); } int main(int argc, char* argv[]) { int n; char buf[BUFFSIZE]; struct rlimit fsizeLimit; fsizeLimit.rlim_cur=1024; fsizeLimit.rlim_max=1024; if(setrlimit(RLIMIT_FSIZE, &fsizeLimit) < 0) { perror("setrlimit error"); exit(-1); } if(signal(SIGXFSZ, xfsz_handler)==SIG_ERR) { fprintf(stderr, "set signal handler error for %d\n", SIGXFSZ); exit(-1); } printf("what ever\n"); /* we need this to get SIGXFSZ sent */ while ( (n=read(STDIN_FILENO, buf, BUFFSIZE)) > 0) { int byteWrite = 0; if ( (byteWrite = write(STDOUT_FILENO, buf, n)) < 0) { perror("write error"); exit(-1); } if(byteWrite!=n) { fprintf(stderr, "byteWrite=%d, n=%d\n", byteWrite, n); exit(-1); } } if (n<0) { perror("read error"); exit(-1); } return 0; }
If I comment on the next line in the code, the kernel will not pass SIGXFSZ .
printf("What ever . . . \n");
Why is this happening? Thanks in advance.
[ root@luaDevelopment ex11]
source share