The 4.3 BSD awrite() solution

brnstnd at stealth.acf.nyu.edu brnstnd at stealth.acf.nyu.edu
Fri Feb 9 11:23:15 AEST 1990


Larry, why don't you at least test the code before asserting that it
doesn't work? I've now run my async library through a rather thorough
series of tests. It works perfectly. aread() and awrite() don't block.

Your theoretical error is the assertion that a system call can't be
interrupted by a signal (except in an obscure situation, where you
consider the behavior to be in error). But a blocked system call can
be interrupted. That's why siginterrupt() exists.

Try compiling this code and feeding it into various situations. Try it
with the interrupt flag changed from 1 to 0. Try it with write() instead
of awrite(). Try similar tests with reading. You'll become a believer too.

#include <signal.h>
#include <stdio.h>
#include "async.h"

main()
{
 char buf[100000];

 siginterrupt(SIGALRM,1); /* this should be in the async library, I guess */
 fprintf(stderr,"%d\n",awrite(1,buf,sizeof(buf)));
}

Here are a few tests I tried on a Sun with the above program:

kramden% ./astest > /dev/null
100000
kramden% ./astest | cat | wc
4096
       0       0    4096
kramden% ./astest | sleep 2
4096
kramden% (sleep 1;./astest) | cat|wc
4096
       0       0    4096
kramden% !!
( sleep 1 ; ./astest ) | cat | wc
8192
       0       0    8192
kramden% !!
( sleep 1 ; ./astest ) | cat | wc
8192
       0       0    8192
kramden% !!
( sleep 1 ; ./astest ) | cat | wc
8192
       0       0    8192

Notice the effects of various caching mechanisms: after the first time,
cat starts much more quickly. Obviously kramden's pipes hold 4096 bytes.

kramden% ./astest | cat > ascat
4096
kramden% ls -l ascat
-rw-------  1 brnstnd      4096 Feb  8 18:06 ascat
kramden% (cat ascat; ./astest; sleep 10; ./astest) | (sleep 5; cat) | wc
-1
8192
       0       0   12288
kramden% (cat /etc/termcap;./astest) | (sleep 2; cat) | wc
1730
    2724   10772  135168
kramden% wc /etc/termcap
    2724   10772  133438 /etc/termcap

If those last two lines don't convince you I don't know what will.
(135168 is 33 times 4096.)

One comment on your comments:

> (this is what almost every Unix system does anyway unless
> you have opened the file with O_SYNC, so this is a moot point, but...).

What about pipes, ttys, and sockets? None of this is moot.

My multitee program works beautifully. More evidence for the superiority
of BSD over System V...

---Dan



More information about the Comp.unix.wizards mailing list