加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

UNIX/Linux中的文件描述符 && 为何删除DB2的容器之后表

发布时间:2020-12-15 16:27:29 所属栏目:安全 来源:网络整理
导读:大多数UNIX文件I/O只需用到5个函数open,read,write,lseek,close。当使用open或者create函数打开或创建一个新文件时,内核向进程返回一个文件描述符,文件描述符是一个非负整数(也就是open/create函数的返回值,如果小于0,则出错)。当读、写文件时,read/wr
大多数UNIX文件I/O只需用到5个函数open,read,write,lseek,close。当使用open或者create函数打开或创建一个新文件时,内核向进程返回一个文件描述符,文件描述符是一个非负整数(也就是open/create函数的返回值,如果小于0,则出错)。当读、写文件时,read/write函数使用参数的是文件描述符,而非文件名。UNIX/LINUX shell中,文件描述符0代表标准输入,1代表标准输出,2代表标准错误。

看下面这个例子(例子略,等我补上)


<坑,待填>


知道文件描述符有啥用呢?举几个例子

例1,将命令输出中的错误信息丢弃

可以参考之前的文章: http://www.52php.cn/article/p-coywgvvd-bpn.html


例2,用exec命令创建自己的文件描述符

创建一个文件描述符进行文件读取(对应于文件打开模式中的只读模式),使用文件描述符3打开文件input.txt:
qingsong@db2a:/tmp$ echo this is a test line > input.txt
qingsong@db2a:/tmp$ exec 3<input.txt
之后就可以使用它:
qingsong@db2a:/tmp$ cat <&3
this is a test line

创建文件描述符用于写入(对应于文件打开模式中的截断模式)
qingsong@db2a:/tmp$ exec 4>output.txt
之后就可以使用它:
qingsong@db2a:/tmp$ cat<<EOF>&4
> Hello,world
> line 2
> EOF
qingsong@db2a:/tmp$ cat output.txt
Hello,world
line 2
这里的cat<<EOF>&4意思是从标准输入中读取,然后重定向到文件描述符4,EOF可以换成其他字符串,习惯上使用EOF

创建文件描述符用于追加(对应于文件打开模式中的追加模式:
qingsong@db2a:/tmp$ exec 5>>output.txt
使用文件描述符:
qingsong@db2a:/tmp$ echo line three >&5
qingsong@db2a:/tmp$ cat output.txt
Hello,world
line 2

line three


例3,

可以解释为什么删除了DB2的容器之后,表空间还是正常状态,直到重启。或者删除活动日志之后,为啥没有crash?
下面的例子中,虽然删除了表空间的容器,但表空间状态一切正常,直到重启数据库:

inst105@db2a:~$ db2 "connect to sample"
inst105@db2a:~$ db2 "create tablespace tbs1 managed by database using (file '/home/inst105/con1' 5000)"
inst105@db2a:~$ db2 "create table t1(id int) in tbs1"
inst105@db2a:~$ db2 "insert into t1 values(100),(200)"
inst105@db2a:~$ rm /home/inst105/con1
inst105@db2a:~$ ls /home/inst105/con1
ls: cannot access /home/inst105/con1: No such file or directory
inst105@db2a:~$ db2 "insert into t1 values(300)"
DB20000I  The SQL command completed successfully.
inst105@db2a:~$ db2 "select * from t1"

ID         
-----------
        100
        200
        300

  3 record(s) selected.
inst105@db2a:~$ db2 "force applications all"
inst105@db2a:~$ db2 "deactivate db sample"
inst105@db2a:~$ db2 "connect to sample"
inst105@db2a:~$ db2 "select * from t1"

ID         
-----------
SQL0290N  Table space access is not allowed.  SQLSTATE=55039

因为 程序是通过文件描述符操纵的文件,而不是文件名。你删了文件,实际上只是删了个文件名,文件描述符还是打开状态,真正的文件数据还没被删除,不信,你看:
inst105@db2a:~$ echo this is a test line > input.txt
inst105@db2a:~$ exec 3<input.txt
inst105@db2a:~$ rm input.txt
inst105@db2a:~$ ls input.txt
ls: cannot access input.txt: No such file or directory
inst105@db2a:~$ cat <&3 this is a test line 文件已经被“删掉”了,还能通过文件描述符访问

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读