1. 文件描述符在內核中數據結構
在具體說dup/dup2以前,我認爲有必要先了解一下文件描述符在內核中的形態。一個進程在此存在期間,會有一些文件被打開,從而會返回一些文件描述符,從shell中運行一個進程,默認會有3個文件描述符存在(0、1、2), 0與進程的標準輸入相關聯,1與進程的標準輸出相關聯,2與進程的標準錯誤輸出相關聯,一個進程當前有哪些打開的文件描述符能夠經過/proc/進程ID/fd目錄查看。 下圖能夠清楚的說明問題:
進程表項linux
————————————————shell
fd標誌 文件指針數據結構
_____________________函數
fd 0:|________|____________|------------> 文件表spa
fd 1:|________|____________|.net
fd 2:|________|____________|指針
fd 3:|________|____________|code
| ....... |blog
|_____________________|進程
圖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