分类: LINUX
2010-12-17 15:24:13
|
程序输出如下
./a.out
fd_file is 3
oldfd is 4
This should be written to the terminal
cat test.txt
This should be written to the test.txt
每个进程都有一个文件描述符表,每个表项中都有指向对应文件的指针,进程在获取描述符时总是获得"当前所有可用描述符中值最小的"那个.注意,在调用dup2时,如果newfd指向的文件没有和其他描述符关联,那么该文件会被关闭.可以用下面的示意图加深对dup()/dup2()的理解
after invoking open()
fd fp opened file
---------------------------------
| 0 | p0 |----------->STDIN
-----------
| 1 | p1 |----------->STDOUT
-----------
| 2 | p2 |----------->STDERR
-----------
| 3 | p3 |----------->test.txt
-----------
after invoking dup()
fd fp opened file
---------------------------------
| 0 | p0 |----------->STDIN
-----------
| 1 | p1 |----------->STDOUT
----------- |
| 2 | p2 |----------->STDERR
----------- |
| 3 | p3 |----------->test.txt
----------- |
| 4 | p4 |------
-----------
after invoking first dup2()
fd fp opened file
---------------------------------
| 0 | p0 |----------->STDIN
-----------
| 1 | p1 |--- ----->STDOUT
----------- | |
| 2 | p2 |----------->STDERR
----------- | |
| 3 | p3 |----------->test.txt
----------- |
| 4 | p4 |------
-----------
after invoking second dup2()
fd fp opened file
---------------------------------
| 0 | p0 |----------->STDIN
-----------
| 1 | p1 |----------->STDOUT
----------- |
| 2 | p2 |----------->STDERR
----------- |
| 3 | p3 |----------->test.txt
----------- |
| 4 | p4 |------
-----------
after invoking close() twice
fd fp opened file
---------------------------------
| 0 | p0 |----------->STDIN
-----------
| 1 | p1 |----------->STDOUT
-----------
| 2 | p2 |----------->STDERR
-----------
下面是一个从网上找来的例子,通过pipe()/dup()实现了父子进程间的通信
|
程序输出如下
./a.out
-rwxr-xr-x 1 jsun jsun 3899 Sep 7 16:17 c_rehash
-rwxrwxrwx 1 jsun jsun 4718 Sep 8 09:41 hash.sh
-rwxr-xr-x 1 jsun jsun 4165 Sep 14 18:03 hash_final.sh
-rw-r--r-- 1 jsun jsun 80 Sep 8 09:41 shash.sh
可以用下面的示意图表示上述程序中父子进程文件描述符表的变化,pfd表示父进程的文件描述符,pfp表示父进程的文件指针;cfp和cfd表示子进程的文件指针和文件描述符.
after pipe()
pfd pfp opened file cfp cfd
--------------------------------------------------
| 0 | p0 |----------->STDIN
-----------
| 1 | p1 |----------->STDOUT
-----------
| 2 | p2 |----------->STDERR
-----------
| 3 | p3 |----------->a pipe node this fd(fds[0]) is for reading
----------- |
| 4 | p4 |-------- this fd(fds[1]) is for writing
-----------
after fork()
pfd pfp opened file cfp cfd
------------------------------------------------------
| 0 | p0 |----------->STDIN<--------------| p0 | 0 |
----------- -----------
| 1 | p1 |----------->STDOUT<-------------| p1 | 1 |
----------- -----------
| 2 | p2 |----------->STDERR<-------------| p2 | 2 |
----------- -----------
| 3 | p3 |----------->a pipe node<--------| p3 | 3 |
----------- | | -----------
| 4 | p4 |-------- -----| p4 | 4 |
----------- -----------
after child close() and dup2()
pfd pfp opened file cfp cfd
------------------------------------------------------
| 0 | p0 |----------->STDIN<--------------| p0 | 0 |
----------- -----------
| 1 | p1 |----------->STDOUT -----| p1 | 1 |
----------- | -----------
| 2 | p2 |----------->STDERR<-------------| p2 | 2 |
----------- | -----------
| 3 | p3 |----------->a pipe node<---- | p3 | 3 |
----------- | | -----------
| 4 | p4 |-------- -----| p4 | 4 |
----------- -----------
after parent close() and dup2()
pfd pfp opened file cfp cfd
------------------------------------------------------
| 0 | p0 |------ STDIN<--------------| p0 | 0 |
----------- | -----------
| 1 | p1 |----------->STDOUT -----| p1 | 1 |
----------- | | -----------
| 2 | p2 |----------->STDERR<-------------| p2 | 2 |
----------- | | -----------
| 3 | p3 |----------->a pipe node<---- | p3 | 3 |
----------- | -----------
| 4 | p4 | -----| p4 | 4 |
----------- -----------
至此,父进程的标准输入被重定向到pipe node的读端,子进程的标准输出被重定向到pipe node的写端.该程序中并没有在处理结束后恢复父子进程的标准输入和标准输出,仅仅用于演示.