操作系统-3.5 fd

109 阅读1分钟
# 查看当前进程文件描述符,$$表示当前进程号
[root@hadoop3 fd]# ll /proc/$$/fd
total 0
lrwx------ 1 root root 64 10月 13 21:18 0 -> /dev/pts/0
lrwx------ 1 root root 64 10月 13 21:18 1 -> /dev/pts/0
lrwx------ 1 root root 64 10月 13 21:18 2 -> /dev/pts/0
lrwx------ 1 root root 64 10月 14 10:42 255 -> /dev/pts/0
# 创建fd=8的文件描述符指向tcp连接
[root@hadoop3 fd]# exec 8<> /dev/tcp/www.baidu.com/80
[root@hadoop3 fd]# ll
total 0
lrwx------ 1 root root 64 10月 13 21:18 0 -> /dev/pts/0
lrwx------ 1 root root 64 10月 13 21:18 1 -> /dev/pts/0
lrwx------ 1 root root 64 10月 13 21:18 2 -> /dev/pts/0
lrwx------ 1 root root 64 10月 14 10:42 255 -> /dev/pts/0
lrwx------ 1 root root 64 10月 13 21:18 8 -> socket:[2288103]
# 重定向>加&符号后面加fd 表示重定向到fd=8
[root@hadoop3 fd]# echo -e "GET / HTTP/1.0\n" >& 8
[root@hadoop3 fd]# cat 0<& 8
HTTP/1.0 200 OK
Accept-Ranges: bytes
Cache-Control: no-cache
Content-Length: 14615
Content-Type: text/html
Date: Sun, 18 Oct 2020 15:57:28 GMT
P3p: CP=" OTI DSP COR IVA OUR IND COM "
P3p: CP=" OTI DSP COR IVA OUR IND COM "
Pragma: no-cache
Server: BWS/1.1
Set-Cookie: BAIDUID=4D7D904B40DA0CC6A9C9E8571E0B8B3B:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: BIDUPSID=4D7D904B40DA0CC6A9C9E8571E0B8B3B; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: PSTM=1603036648; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: BAIDUID=4D7D904B40DA0CC60EA0FBD6D0DB8734:FG=1; max-age=31536000; expires=Mon, 18-Oct-21 15:57:28 GMT; domain=.baidu.com; path=/; version=1; comment=bd
Traceid: 160303664813746234987139711693384181510
Vary: Accept-Encoding
X-Ua-Compatible: IE=Edge,chrome=1

<!DOCTYPE html><!--STATUS OK-->
<html>
<head>
	<meta http-equiv="content-type" content="text/html;charset=utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=Edge">
	<link rel="dns-prefetch" href="//s1.bdstatic.com"/>
	<link rel="dns-prefetch" href="//t1.baidu.com"/>
	<link rel="dns-prefetch" href="//t2.baidu.com"/>
	<link rel="dns-prefetch" href="//t3.baidu.com"/>
	<link rel="dns-prefetch" href="//t10.baidu.com"/>
	<link rel="dns-prefetch" href="//t11.baidu.com"/>
	<link rel="dns-prefetch" href="//t12.baidu.com"/>
	<link rel="dns-prefetch" href="//b1.bdstatic.com"/>
	<title>百度一下,你就知道</title>
...
## 删除fd=8
[root@hadoop3 fd]# exec 8<& -
[root@hadoop3 fd]# ll
total 0
lrwx------ 1 root root 64 10月 13 21:18 0 -> /dev/pts/0
lrwx------ 1 root root 64 10月 13 21:18 1 -> /dev/pts/0
lrwx------ 1 root root 64 10月 13 21:18 2 -> /dev/pts/0
lrwx------ 1 root root 64 10月 14 10:42 255 -> /dev/pts/0