mirror of https://github.com/mkerrisk/man-pages
7b6a329977
The following program illustrates the difference between TCP and Unix stream sockets doing sendfile. Since TCP implements zero-copy, the new modifications to the file transferred is seen upon reading despite the modifications happening after sendfile was last called. Unix stream sockets do not implement zero-copy (as of Linux 3.15), so readers continue to see the contents of the file at the time it was sent, not as they are at the time of reading. ----------------- sendfile-mod.c --------------- #define _GNU_SOURCE #include <sys/ioctl.h> #include <sys/types.h> #include <sys/socket.h> #include <sys/sendfile.h> #include <arpa/inet.h> #include <stdio.h> #include <errno.h> #include <string.h> #include <unistd.h> #include <assert.h> #include <fcntl.h> static void tcp_socketpair(int sv[2]) { struct sockaddr_in addr; socklen_t addrlen = sizeof(addr); int l = socket(PF_INET, SOCK_STREAM, 0); int c = socket(PF_INET, SOCK_STREAM, 0); int a; int val = 1; addr.sin_family = AF_INET; addr.sin_addr.s_addr = INADDR_ANY; addr.sin_port = 0; assert(0 == bind(l, (struct sockaddr*)&addr, addrlen)); assert(0 == listen(l, 1024)); assert(0 == getsockname(l, (struct sockaddr *)&addr, &addrlen)); assert(0 == connect(c, (struct sockaddr *)&addr, addrlen)); a = accept4(l, NULL, NULL, SOCK_NONBLOCK); assert(a >= 0); close(l); assert(0 == ioctl(c, FIONBIO, &val)); sv[0] = a; sv[1] = c; } int main(int argc, char *argv[]) { int pair[2]; FILE *tmp = tmpfile(); int tfd; char buf[16384]; ssize_t w, r; size_t i; const size_t n = 2048; off_t off = 0; char expect[4096]; int flags = SOCK_STREAM|SOCK_NONBLOCK; tfd = fileno(tmp); assert(tfd >= 0); /* prepare the tempfile */ memset(buf, 'a', sizeof(buf)); for (i = 0; i < n; i++) assert(sizeof(buf) == write(tfd, buf, sizeof(buf))); if (argc == 2 && strcmp(argv[1], "unix") == 0) assert(0 == socketpair(AF_UNIX, flags, 0, pair)); else if (argc == 2 && strcmp(argv[1], "pipe") == 0) assert(0 == pipe2(pair, O_NONBLOCK)); else tcp_socketpair(pair); /* fill up the socket buffer */ for (;;) { w = sendfile(pair[1], tfd, &off, n); if (w > 0) continue; if (w < 0 && errno == EAGAIN) break; assert(0 && "unhandled error" && w && errno); } printf("wrote off=%lld\n", (long long)off); /* rewrite the tempfile */ memset(buf, 'A', sizeof(buf)); assert(0 == lseek(tfd, 0, SEEK_SET)); for (i = 0; i < n; i++) assert(sizeof(buf) == write(tfd, buf, sizeof(buf))); /* we should be reading 'a's, not 'A's */ memset(expect, 'a', sizeof(expect)); do { r = read(pair[0], buf, sizeof(expect)); /* TCP fails here since it is zero copy (on Linux 3.15.5) */ if (r > 0) assert(memcmp(buf, expect, r) == 0); } while (r > 0); return 0; } Signed-off-by: Eric Wong <normalperson@yhbt.net> Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com> |
||
---|---|---|
man1 | ||
man2 | ||
man3 | ||
man4 | ||
man5 | ||
man6 | ||
man7 | ||
man8 | ||
scripts | ||
Changes | ||
Changes.old | ||
Makefile | ||
README | ||
man-pages-3.80.Announce | ||
man-pages-3.80.lsm |
README
This package contains Linux man pages for sections 2, 3, 4, 5, and 7. Some more information is given in the `Announce' file. Install by copying to your favourite location. "make install" will just copy them to /usr/share/man/man[1-8]. To install to a path different from /usr use "make install prefix=/install/path". "make" will move the pages from this package that are older than the already installed ones to a subdirectory `not_installed', then remove old versions (compressed or not), compress the pages, and copy them to /usr/share/man/man[1-8]. Note that you may have to remove preformatted pages. Note that sometimes these pages are duplicates of pages also distributed in other packages. This has been reported about dlclose.3, dlerror.3, dlopen.3, dlsym.3 (found in ld.so), about resolver.3, resolv.conf.5 (found in bind-utils), and about passwd.5, and mailaddr.7. Be careful not to overwrite more up-to-date versions. Reports on further duplicates are welcome. Formerly present and now removed duplicates: exports.5 (found in nfs-server-2.2*), fstab.5, nfs.5 (found in util-linux-2.12*), lilo.8, lilo.conf.5 (found in lilo-21.6*). Copyrights: These man pages come under various copyrights. All pages are freely distributable when the nroff source is included. If you have corrections and additions to suggest, see http://www.kernel.org/doc/man-pages/contributing.html