fork() and dup()

I have met this code:

switch(fork()) {

case 0:
           close\(1\);
           dup\(p[1]\);
           close\(p[0]\);
           close\(p[1]\);
           execvp\(<whatever>\);
           perror\("Exec failed"\);

}

Can anyone tell me what this piece of code does?
Thx alot..

Since it tested for 0 after the fork(), this code will be executed by the the child process. First it closes fd 1 which is stdout. Next it dups the fd p[1]. The array p was created by pipe() or socketpair(). We write to p[1] and we read from p[0]. The dup call make a copy of a file descriptor. It will always use the lowest fd available. Since we just closed 1, 1 is available. So after the dup, the child process will have set its stdout to the "write" end of a pipe. Then it just closes the uneeded fd's and it runs a program.

Meanwhile, no doubt, the parent will be reading from p[0] to read the output of the child process.