Ralf Wildenhues wrote: > In the first example using 'read', don't you have to take care of EINTR > so that './mycat9 < file' doesn't drop forget half the file after being > suspended and continued again?
Excellent point. I thought that in programs that don't install signal handlers, EINTR was last seen in SunOS 4, 15 years ago. But no, it also occurs in MacOS X! Test program: ========================================================================== #include <stdio.h> #include <sys/types.h> #include <unistd.h> int main () { for (;;) { char buf[4096]; ssize_t count = read (0, buf, sizeof (buf)); if (count == 0) break; if (count < 0) { perror ("read"); break; } fwrite (buf, 1, count, stdout); } return 0; } ========================================================================== Run it, type Hello<Return>, then Ctrl-Z and the job control command 'fg'. Result on all systems except MacOS X: $ ./a.out Hello Hello ^Z [1]+ Stopped ./a.out $ fg ./a.out World World Result on MacOS X: $ ./a.out Hello Hello ^Z [1]+ Stopped ./a.out $ fg ./a.out read: Interrupted system call I have updated the blog / tutorial lesson, and I'm adding this to the gnulib documentation: 2011-07-03 Bruno Haible <br...@clisp.org> Comments about EINTR. * lib/safe-read.h: Explain the purpose of this module. * lib/safe-write.h: Likewise. * doc/posix-functions/read.texi: Mention EINTR and the 'safe-read' module. * doc/posix-functions/write.texi: Mention EINTR and the 'safe-write' module. Reported by Ralf Wildenhues <ralf.wildenh...@gmx.de>. --- doc/posix-functions/read.texi.orig Sun Jul 3 13:33:13 2011 +++ doc/posix-functions/read.texi Sun Jul 3 12:29:44 2011 @@ -17,4 +17,11 @@ Portability problems not fixed by Gnulib: @itemize +@item +This function may fail with error @code{EINTR}, even in programs that don't +install any signal handlers, on some platforms: +MacOS X 10.5. @end itemize + +For handling @code{EINTR}, Gnulib provides a module @samp{safe-read} with a +function @code{safe_read}. --- doc/posix-functions/write.texi.orig Sun Jul 3 13:33:13 2011 +++ doc/posix-functions/write.texi Sun Jul 3 12:29:40 2011 @@ -32,4 +32,11 @@ Portability problems not fixed by Gnulib: @itemize +@item +This function may fail with error @code{EINTR}, even in programs that don't +install any signal handlers, on some platforms: +MacOS X 10.5. @end itemize + +For handling @code{EINTR}, Gnulib provides a module @samp{safe-write} with a +function @code{safe_write}. --- lib/safe-read.h.orig Sun Jul 3 13:33:13 2011 +++ lib/safe-read.h Sun Jul 3 12:57:35 2011 @@ -14,6 +14,19 @@ You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ +/* Some system calls may be interrupted and fail with errno = EINTR in the + following situations: + - The process is stopped and restarted (signal SIGSTOP and SIGCONT, user + types Ctrl-Z) on some platforms: MacOS X. + - The process receives a signal for which a signal handler was installed + with sigaction() with an sa_flags field that does not contain + SA_RESTART. + - The process receives a signal for which a signal handler was installed + with signal() and for which no call to siginterrupt(sig,0) was done, + on some platforms: AIX, HP-UX, IRIX, OSF/1, Solaris. + + This module provides a wrapper around read() that handles EINTR. */ + #include <stddef.h> #ifdef __cplusplus --- lib/safe-write.h.orig Sun Jul 3 13:33:13 2011 +++ lib/safe-write.h Sun Jul 3 12:57:36 2011 @@ -14,6 +14,19 @@ You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ +/* Some system calls may be interrupted and fail with errno = EINTR in the + following situations: + - The process is stopped and restarted (signal SIGSTOP and SIGCONT, user + types Ctrl-Z) on some platforms: MacOS X. + - The process receives a signal for which a signal handler was installed + with sigaction() with an sa_flags field that does not contain + SA_RESTART. + - The process receives a signal for which a signal handler was installed + with signal() and for which no call to siginterrupt(sig,0) was done, + on some platforms: AIX, HP-UX, IRIX, OSF/1, Solaris. + + This module provides a wrapper around write() that handles EINTR. */ + #include <stddef.h> #define SAFE_WRITE_ERROR ((size_t) -1) -- In memoriam Yuri Shchekochikhin <http://en.wikipedia.org/wiki/Yuri_Shchekochikhin>