#include <linux/limits.h> int mydup2(int oldfd, int newfd) { ` int i, fd, fdtable[OPEN_MAX]; if (newfd<0 || newfd>OPEN_MAX) return -1; if (oldfd<0 || oldfd>OPEN_MAX) return -1; if (newfd==oldfd) return newfd; for (i = 0; i<OPEN_MAX; ++i> if (fdtable[i]) close (newfd); while ((fd = dup(oldfd))!=newfd && fd!=-1) fdtable[fd] = 1; for (i = 0; i<OPEN_MAX; ++i>) if (fdtable[i]) close(i); return fd; }
create pipe if (fork()) In parent, open() file while not end of file read a character and write it to the pipe else In child while not end of pipe data read a character from pipe and discardYou should run this program a couple of times to make sure that the file you are copying is all stored in the buffer cache - it is much faster to read from memory than from disk.