1. 文件描述符在內核中數據結構
在具體說dup/dup2之前,我認為有必要先了解一下文件描述符在內核中的形態。一個進程在此存在期間,會有一些文件被打開,從而會返回一些文件描述符,從shell中運行一個進程,默認會有3個文件描述符存在(0、1、2), 0與進程的標准輸入相關聯,1與進程的標准輸出相關聯,2與進程的標准錯誤輸出相關聯,一個進程當前有哪些打開的文件描述符可以通過/proc/進程ID/fd目錄查看。 下圖可以清楚的說明問題:
進程表項
————————————————
fd標志 文件指針
_____________________
fd 0:|________|____________|------------> 文件表
fd 1:|________|____________|
fd 2:|________|____________|
fd 3:|________|____________|
| ....... |
|_____________________|
圖1
文件表中包含:文件狀態標志、當前文件偏移量、v節點指針,這些不是本文討論的重點,我們只需要知道每個打開的文件描述符(fd標志)在進程表中都有自己的文件表項,由文件指針指向。
2. dup/dup2函數
APUE和man文檔都用一句話簡明的說出了這兩個函數的作用:復制一個現存的文件描述符。
#include <unistd.h>
int dup(int oldfd);
int dup2(int oldfd, int newfd);
從圖1來分析這個過程,當調用dup函數時,內核在進程中創建一個新的文件描述符,此描述符是當前可用文件描述符的最小數值,這個文件描述符指向oldfd所擁有的文件表項。
進程表項
————————————————
fd標志 文件指針
_____________________
fd 0:|________|____________| ______
fd 1:|________|____________|----------------> | |
fd 2:|________|____________| |文件表|
fd 3:|________|____________|----------------> |______|
| ....... |
|_____________________|
圖2:調用dup后的示意圖
如圖2所示,假如oldfd的值為1,當前文件描述符的最小值為3,那么新描述符3指向描述符1所擁有的文件表項。
dup2和dup的區別就是可以用newfd參數指定新描述符的數值,如果newfd已經打開,則先將其關閉。如果newfd等於oldfd,則dup2返回newfd, 而不關閉它。dup2函數返回的新文件描述符同樣與參數oldfd共享同一文件表項。
APUE用另外一個種方法說明了這個問題:
實際上,調用dup(oldfd)等效於
fcntl(oldfd, F_DUPFD, 0)
而調用dup2(oldfd, newfd)等效於
close(oldfd);
fcntl(oldfd, F_DUPFD, newfd);
實例: dup 和 dup2 都可以用來復制一個現存的文件描述符。經常用來重新定向進程的 STDIN, STDOUT, STDERR。 dup 函數 dup 函數定義在 <unistd.h> 中,函數原形為: int dup ( int filedes ) ; 函數返回一個新的描述符,這個新的描述符是傳給它的描述符的拷貝,若出錯則返回 -1。由dup返回的新文件描述符一定是當前可用文件描述符中的最小數值。這函數返回的新文件描述符與參數 filedes 共享同一個文件數據結構。 dup函數實例: [lingyun@localhost dup]$ ls dup.c [lingyun@localhost dup]$ cat dup.c /********************************************************************************* * Copyright: (C) 2013 fulinux<fulinux@sina.com> * All rights reserved. * * Filename: dup.c * Description: This file * * Version: 1.0.0(07/31/2013~) * Author: fulinux <fulinux@sina.com> * ChangeLog: 1, Release initial version on "07/31/2013 04:00:06 PM" * ********************************************************************************/ #include <stdio.h> #include <unistd.h> #include <sys/stat.h> #include <fcntl.h> int main(int argc, char* argv[]) { int fd = open("hello", O_CREAT|O_RDWR|O_TRUNC, S_IRUSR|S_IWUSR); if(fd < 0) { printf("Open Error!!\n"); return 0; } int nfd = dup(fd); if(nfd < 0) { printf("Error!!\n"); return 0; } char buf[1000]; int n; while((n = read(STDIN_FILENO, buf,1000)) > 0) { if(write(nfd, buf, n) != n) { printf("Write Error!!\n"); return 0; } } return 0; } 上面代碼中,nfd 拷貝了 fd,所以 write ( nfd, buf, n ) 這語句寫到 nfd 所代表的文件時也就是寫到 fd 所代表的文件。程序執行完后可以在相應的目錄的hello文件中看到輸出。 [lingyun@localhost dup]$ gcc dup.c [lingyun@localhost dup]$ ls a.out dup.c [lingyun@localhost dup]$ ./a.out hello world ^C [lingyun@localhost dup]$ ls a.out dup.c hello [lingyun@localhost dup]$ cat hello hello world [lingyun@localhost dup]$ dup2 函數 dup2 函數定義在 <unistd.h> 中,函數原形為: int dup2( int filedes, int filedes2 ) 同樣,函數返回一個新的文件描述符,若出錯則返回 -1。與 dup 不同的是,dup2 可以用 filedes2 參數指定新描述符的數值。如果 filedes2 已經打開,則先將其關閉。如若 filedes 等於 filedes2 , 則 dup2 返回 filedes2 , 而不關閉它。同樣,返回的新文件描述符與參數 filedes 共享同一個文件數據結構。 dup2函數實例: [lingyun@localhost dup2]$ ls dup2.c [lingyun@localhost dup2]$ cat dup2.c /********************************************************************************* * Copyright: (C) 2013 fulinux<fulinux@sina.com> * All rights reserved. * * Filename: dup2.c * Description: This file * * Version: 1.0.0(07/31/2013~) * Author: fulinux <fulinux@sina.com> * ChangeLog: 1, Release initial version on "07/31/2013 08:22:19 PM" * ********************************************************************************/ #include <stdio.h> #include <unistd.h> #include <sys/stat.h> #include <fcntl.h> int main(int argc, char* argv[]) { int fd = open("hello.file", O_CREAT|O_RDWR|O_TRUNC,S_IRUSR|S_IWUSR); if(fd < 0) { printf("Open Error!!\n"); return 0; } int nfd = dup2(fd, STDOUT_FILENO); if(nfd < 0) { printf("Error!!\n"); return 0; } char buf[5]; int n; while((n = read(STDIN_FILENO, buf, 5)) > 0) if(write(nfd, buf, n) != n) { printf("Write Error!!\n"); return 0; } return 0; } 上面的例子使用dup2將標准輸出重定向為hello.file文件,如下所示: [lingyun@localhost dup2]$ ls dup2.c [lingyun@localhost dup2]$ gcc dup2.c [lingyun@localhost dup2]$ ./a.out hello world ^C [lingyun@localhost dup2]$ cat hello.file hello world [lingyun@localhost dup2]$
轉自:http://blog.csdn.net/fulinus/article/details/9669177