二、复制文件描述符
复制前:

复制后:

复制后,两个文件描述符都指向了同一个文件表,refcnt=2。
复制文件描述符有三种方法:
1,dup
2,dup2
#include <unistd.h>
int dup(int oldfd);
int dup2(int oldfd, int newfd);
DESCRIPTION
These system calls create a copy of the file descriptor oldfd.
dup() uses the lowest-numbered unused descriptor for the new descriptor.
dup2() makes newfd be the copy of oldfd, closing newfd first if necessary, but note
the following:
* If oldfd is not a valid file descriptor, then the call fails, and newfd is not closed.
* If oldfd is a valid file descriptor, and newfd has the same value as
oldfd, then dup2() does nothing, and returns newfd.
After a successful return from one of these system calls, the old and new file descriptors may be used interchangeably. They refer to the same open file description (see open(2)) and thus share file offset and file status flags; for example, if the file offset is modified by using lseek(2) on one of the descriptors, the offset is also changed for the other.
RETURN VALUE
On success, these system calls return the new descriptor. On error, -1 is returned, and errno is set appropriately.
示例程序:
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <fcntl.h> int main(void) { int fd; fd = open("test.txt",O_WRONLY); if( fd == -1){ perror("open error"); exit(EXIT_FAILURE); } int fd2; fd2 = dup(fd); if(fd2 == -1){ perror("dup error"); exit(EXIT_FAILURE); } printf("oldfd = %d\n",fd); printf("newfd = %d\n",fd2); int fd3; close(0); //或者close(STDIN_FILENO) fd3 = dup(fd); if(fd3 == -1){ perror("dup error"); exit(EXIT_FAILURE); } printf("after close stdin,the newfd = %d\n",fd3); exit(EXIT_SUCCESS); }
运行结果:

用dup进行文件描述符的复制时,顺序查找即从0开始查找可以文件描述符
示例2:
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <fcntl.h> int main(void) { int fd; fd = open("test.txt",O_WRONLY); if( fd == -1){ perror("open error"); exit(EXIT_FAILURE); } int fd2; fd2 = dup2(fd,0); if(fd2 == -1){ perror("dup error"); exit(EXIT_FAILURE); } printf("oldfd = %d\n",fd); printf("newfd = %d\n",fd2); exit(EXIT_SUCCESS); }
运行结果:

用dup2进行文件描述符复制时,指定需要复制的新的描述符,如果该描述符已经被占用,则先关闭它在重新复制,类似于先调用close再dup
3,fcntl
功能:操纵文件描述符,改变已打开的文件的属性
#include <fcntl.h>
int fcntl(int fd, int cmd, ... /* arg */ );
DESCRIPTION
fcntl() performs one of the operations described below on the open file
descriptor fd. The operation is determined by cmd.
fcntl() can take an optional third argument. Whether or not this argu-
ment is required is determined by cmd. The required argument type is
indicated in parentheses after each cmd name (in most cases, the
required type is long, and we identify the argument using the name
arg), or void is specified if the argument is not required.
由第二个参数指定操作类型,后面点的可变参数指定该命令所需的参数
这里我们进行文件描述符复制,可将cmd 设为: F_DUPFD (long),该命令表示:
Find the lowest numbered available file descriptor greater than
or equal to arg and make it be a copy of fd. This is different
from dup2(2), which uses exactly the descriptor specified.
On success, the new descriptor is returned.
示例程序:
#include <unistd.h> #include <sys/stat.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdlib.h> #include <stdio.h> #include <errno.h> #include <string.h> #define ERR_EXIT(m) do { perror(m); exit(EXIT_FAILURE); } while(0) int main(int argc, char *argv[]) { int fd; fd = open("test.txt", O_WRONLY); if (fd == -1) ERR_EXIT("open error"); close(1); if (fcntl(fd, F_DUPFD, 0) < 0) ERR_EXIT("dup fd error"); printf("-----------\n");//进行重定向,将不会显示在标准输出, return 0; }
运行结果:

