c - 当某个进程打开文件时,unlink() 会做什么?

标签 c linux file hardlink

来自 APUE

#include <unistd.h>
int unlink(const char *pathname);

Only when the link count reaches 0 can the contents of the file be deleted. One other condition prevents the contents of a file from being deleted: as long as some process has the file open, its contents will not be deleted. When a file is closed, the kernel first checks the count of the number of processes that have the file open. If this count has reached 0, the kernel then checks the link count; if it is 0, the file’s contents are deleted.

  1. 如果进程中的 execve() 正在使用一个文件,它是否将其视为“进程已打开文件”?

  2. 如果某个进程正在打开文件或execve()ed,unlink()会立即返回0或-1,或者等到进程关闭文件或 execve() 完成运行并执行其工作?

最佳答案

1) 进程通过 execve 继承的文件句柄将保持打开状态,直到明确关闭或进程退出。

2) unlink 不会阻塞。它将简单地删除路径并减少硬链接(hard link)文件的引用计数,此时文件系统可以删除引用的文件并在文件不再被任何进程打开后释放与其关联的空间。 unlink 将返回 0,除非出现 I/O 或权限错误等。

关于c - 当某个进程打开文件时,unlink() 会做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50778585/

相关文章:

linux - 从 Matlab 启动 Linux 终端

linux - 当从 ls | 传送结果时 head 不起作用grep

c - FreeRTOS 任务不是上下文切换

c++ - 我收到以下错误 : [Warning] multi-character character constant

c - Uart 检查接收缓冲区中断与轮询

c# - 如何在 Windows Phone 8 上永久删除库文件?

java - 检测文件是否存在

c - 如何保存栈和堆

linux - 如何更改 bash 脚本的 `echo` 输出?

python - 关于python的两个简单问题