Linux kill 进程后 ,fd 是否被释放,是否会调用 release ?

  1. kill 进程后怎么验证 fd 确实被释放了?
    打开 APP,使用 lsof 命令 确定我们被使用的APP 确实被打开了
xxxxxx:/ # lsof | grep video10
lsof | grep video10
camerahalserver  1695 cameraserv  mem       CHR               81,0                 7834 /dev/video10
camerahalserver  1695 cameraserv   18u      CHR               81,0       0t0       7834 /dev/video10

同样,关闭APP后,使用测试命令,结果如下

xxxxxx:/ # lsof | grep video10
lsof | grep video10

可以发现被打开的设备节点被关闭了,也就是释放了对应的 fd 。
下面 用 dev/binder 增加对应的 dump_stack() 来说明,由于使用的进程也打开了binder,在kill pid命令后,串口log如下

[  159.753908]<1>[W](1)[] binder_release+0xc8/0xd8
[  159.754666]<1>[W](1)[] __fput+0x94/0x1d8
[  159.755348]<1>[W](1)[] ____fput+0xc/0x14
[  159.756031]<1>[W](1)[] task_work_run+0xc0/0xe0
[  159.756779]<1>[W](1)[] do_exit+0x2ac/0x984
[  159.757482]<1>[W](1)[] do_group_exit+0x3c/0x98
[  159.758230]<1>[W](1)[] get_signal+0x3a0/0x564
[  159.758967]<1>[W](1)[] do_signal+0x148/0x4cc
[  159.759693]<1>[W](1)[] do_notify_resume+0x84/0xa0
[  159.760473]<1>[W](1)[] work_pending+0x8/0x10
[  159.763496]<1>[I](1)init: Service 'camerahalserver' (pid 1674) received signal 15
[  159.764451]<1>[I](1)init: Sending signal 9 to service 'camerahalserver' (pid 1674) process group...
[  159.765813]<1>[I](1)libprocessgroup: Successfully killed process cgroup uid 1047 pid 1674 in 0ms

这里说明了两个方面内容:

  1. kill 进程后,进程中的fd会被释放,kernel 相应实现了 .release 的操作也会被调用
  2. fd 释放 是在 do_signal 即收到了 SIGTERM 实现了调用

引申:binder 服务的死亡通知就是通过 fd 释放会在 binder_release 实现了死亡通知

[   47.841435]<1>[W](1)Call trace:
[   47.841834]<1>[W](1)[] dump_backtrace+0x0/0x2c0
[   47.842590]<1>[W](1)[] show_stack+0x14/0x1c
[   47.843303]<1>[W](1)[] dump_stack+0x94/0xb4
[   47.844019]<1>[W](1)[] binder_release+0xac/0xbc
[   47.844777]<1>[W](1)[] __fput+0x94/0x1d8
[   47.845456]<1>[W](1)[] ____fput+0xc/0x14
[   47.846141]<1>[W](1)[] task_work_run+0xc0/0xe0
[   47.846889]<1>[W](1)[] do_exit+0x2ac/0x984
[   47.847591]<1>[W](1)[] do_group_exit+0x3c/0x98
[   47.848340]<1>[W](1)[] get_signal+0x3a0/0x564
[   47.849077]<1>[W](1)[] do_signal+0x148/0x4cc
[   47.849802]<1>[W](1)[] do_notify_resume+0x84/0xa0
[   47.850581]<1>[W](1)[] work_pending+0x8/0x10
[   47.852451]<1>[I](1)binder: send failed reply for transaction 106838 to 1653:3145
[   47.853698]<1>[E](1)binder_alloc: 2695: binder_alloc_buf, no vma
[   47.854450]<1>[I](1)binder: 1653:3145 transaction failed 29189/-3, size 144-0 line 3136
[   47.855501]<1>[I](1)binder: undelivered transaction 106902, process died.
[   47.856353]<1>[I](1)binder: undelivered transaction 106830, process died.
[   47.857225]<1>[I](1)binder: send failed reply for transaction 106839 to 1653:3149

你可能感兴趣的:(C,(Programming),Language)