Pipes, cat buffer size

Mel fbsd.hackers at rachie.is-a-geek.net
Mon Oct 20 14:13:28 UTC 2008


On Sunday 19 October 2008 02:50:22 Dan Nelson wrote:

> > But if it works in general, it may simply be that it isn't really
> > applicable to my purpose (and I should modify the reader to read
> > multiple blocks).
>
> That's my suggestion, yes.  That way your program would also work when
> passed data from an internet socket (where you will get varying read()
> sizes too).  It wouldn't add more than 10 lines to wrap your read in a
> loop that exits when your preferred size has been reached.

Since you mention a socket, would this patch be a good idea and use kqueue to 
read from the pipe?
I would think that having the kernel fill the buffer, rather then a busy loop 
kernel/userland would improve speed, but I'm not too familiar with the code 
to know if this causes any problems.

diff -u -r1.191.2.3 sys_pipe.c
--- sys_pipe.c  6 Jun 2008 12:17:28 -0000       1.191.2.3
+++ sys_pipe.c  20 Oct 2008 14:04:18 -0000
@@ -1594,7 +1609,10 @@
                PIPE_UNLOCK(rpipe);
                return (1);
        }
-       ret = kn->kn_data > 0;
+       if ( kn->kn_sfflags & NOTE_LOWAT)
+               ret = kn->kn_data >= kn->kn_sdata;
+       else
+               ret = kn->kn_data > 0;
        PIPE_UNLOCK(rpipe);
        return ret;
 }

-- 
Mel


More information about the freebsd-hackers mailing list