一、開啟檔案核心資料結構
1、一個進程開啟兩個檔案
檔案狀態標誌:讀、寫、追加、同步、非阻塞等
2、一個進程兩次開啟同一檔案
3、兩個進程開啟同一檔案
樣本程式:
C++ Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
|
/*************************************************************************
> File Name: file_share.c
> Author: Simba
> Mail: dameng34@163.com
> Created Time: Sat 23 Feb 2013 02:34:02 PM CST
************************************************************************/
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
#include<stdio.h>
#include<stdlib.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 fd1, fd2;
char buf1[1024] = {0};
char buf2[1024] = {0};
/* 進程式控制制塊PCB
* struct task {
* ...
* struct files_struct *files;
* }
* 同一個進程兩次開啟同一個檔案,一個進程擁有的一個檔案描述符表其中一個fd索引對應的指標指向一個
* 檔案表(包括檔案狀態(讀寫追加同步非阻塞等),當前檔案位移量,
* 檔案引用次數(當有兩個fd指向同個檔案表時引用計數為2,見dup,也可用於重新導向),
* 檔案操作指標, V節點指標等)不共用,
* V節點表(包括V節點資訊(struct stat), i節點資訊等)共用
*/
/* 兩個進程開啟同一個檔案的情況與上類同*/
fd1 = open("test.txt", O_RDONLY);
if (fd1 == -1)
ERR_EXIT("open error");
read(fd1, buf1, 5);
printf("buf1=%s\n", buf1);
fd2 = open("test.txt", O_RDWR);
if (fd2 == -1)
ERR_EXIT("open error");
read(fd2, buf2, 5);
printf("buf2=%s\n", buf2);
write(fd2, "AAAAA", 5);
memset(buf1, 0, sizeof(buf1));
read(fd1, buf1, 5);
printf("buf1=%s\n", buf1);
close(fd1);
close(fd2);
return 0;
} |
假設test.txt檔案的內容是 ABCDEhello
測試如下:
simba@ubuntu:~/Documents/code/linux_programming/APUE/File_IO$ ./file_share
buf1=ABCDE
buf2=ABCDE
buf1=AAAAA
test.txt檔案內容變成 ABCDEAAAAA
分析:由分析可知,一個進程兩次開啟同一檔案,檔案表是不共用的,即各有自己的檔案位移量和開啟檔案標誌,所以兩次read不同的fd都是從頭開始讀取,但V節點表是共用的,在fd2寫入(同個檔案表的read和write是共用位移的)更改了inode指向的硬碟資料區塊,再次read fd1得到的也是更改後的值。
二、I/O重新導向
當我們執行了dup(3)之後,系統選擇一個閒置檔案描述符即4,這樣就有兩個檔案描述符指向同個檔案表,所以引用計數為2。利用dup等函數可以進行重新導向的步驟是先close輸入輸出檔案描述符,然後執行dup(fd), 這樣輸入輸出檔案描述符也指向fd指向的檔案,這樣就實現了重新導向。此外dup2, fcntl 函數也可以實現,其實不使用這些函數,而直接close(0/1/2)完再open也可以實現。如下使用cat命令實現複製檔案的功能:
C++ Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
|
/*************************************************************************
> File Name: process_.c
> Author: Simba
> Mail: dameng34@163.com
> Created Time: Sat 23 Feb 2013 02:34:02 PM CST
************************************************************************/
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<signal.h>
#define ERR_EXIT(m) \
do { \
perror(m); \
exit(EXIT_FAILURE); \
} while(0)
int main(int argc, char *argv[])
{
close(0);
open("Makefile", O_RDONLY);
close(1);
open("test.txt", O_WRONLY | O_CREAT | O_TRUNC, 0664);
execlp("cat", "cat", NULL);
return 0;
} |
現在標準輸入是檔案Makefile,標準輸出是檔案Makefile2,將當前進程替換成cat,則cat會從標準輸入讀而後輸出到標準輸出,即完成了copy的功能。
dup/fcntl 函數樣本程式如下:
C++ Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
|
/*************************************************************************
> File Name: file_dup.c
> Author: Simba
> Mail: dameng34@163.com
> Created Time: Sat 23 Feb 2013 02:34:02 PM CST
************************************************************************/
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#define ERR_EXIT(m) \
do { \
perror(m); \
exit(EXIT_FAILURE); \
} while(0)
/* dup dup2 fcntl */
int main(int argc, char *argv[])
{
int fd;
fd = open("test2.txt", O_WRONLY);
if (fd == -1)
ERR_EXIT("open error");
/*
close(1);
dup(fd);
*/
// dup2(fd, 1);
close(1);
if (fcntl(fd, F_DUPFD, 0) < 0) //從0開始搜尋可用的fd
ERR_EXIT("fcntl error");
printf("hello\n"); // 輸出重新導向到test2.txt
return 0;
} |
參考:《APUE》