#include #include #include void copyfunc(void *v) { int *fd, n; char buf[256]; int tot = 0; fd = v; while((n = read(fd[0], buf, sizeof buf)) > 0){ if(write(fd[1], buf, n) != n) sysfatal("write: %r"); tot += n; fprint(2, "copied %d byte chunk\n", n); } fprint(2, "total copied: %d\n", tot); close(fd[0]); close(fd[1]); threadexits(nil); } /* copy stdin to stdout through a pipe. run like ./pipetest /dev/null */ void threadmain(int argc, char *argv[]) { int fd[4]; int pfd[2]; if(pipe(pfd) < 0) sysfatal("pipe error"); fd[0] = 0; fd[1] = pfd[0]; fd[2] = pfd[1]; fd[3] = 1; proccreate(copyfunc, fd, 8192); copyfunc(fd+2); }