unlink(file_name)
官方描述:
unlink的文檔是這樣描述的:
????unlink()??deletes??a??name??from??the??filesystem.??If that name was the last link to a file and no processes have the file open the file is deleted and the space it was using is made available for reuse.????If the name was the last link to a file but any processes still have the file open the file will remain in existence??until??the??last??file descriptor referring to it is closed.????If the name referred to a symbolic link the link is removed.????If the name referred to a socket, fifo or device the name for it is removed but processes which have the object open may continue to use it.????首先你要明確一個概念,一個文件是否存在取決于它的inode是否存在,你在目錄里看到的是目錄項里一條指向該inode的鏈接,而不是文件的本身.
????當你調用unlink的時候他直接把目錄項里的該條鏈接刪除了,但是inode并沒有動,該文件還是存在的,這時候你會發現,目錄里找不到該文件,但是已經打開這個文件的進程可以正常讀寫.只有當打開這個inode的所有文件描述符被關閉,指向該inode的鏈接數為0的情況下,這個文件的inode才會被真正的刪除.
????從unlink的名字上就應該能判斷出來,unlink含義為取消鏈接,remove才是刪除的意思
?
unlink()函數功能即為刪除文件。執行unlink()函數會刪除所給參數指定的文件。
unlink()函數返回值: 成功返回0,失敗返回 -1
remove()與unlink()的區別:
- 當remove() 中的pathname指定問文件時,相當于調用unlink 刪除文件鏈接
- 當remove() 中的pahtname指定為目錄時,相當于調用rmdir 刪除目錄
因此:執行unlink(filename)之后,并不會立即刪除文件,還可以對文件進行讀寫操作,只有當打開這個文件對應的inode的文件描述符都關閉了之后才會將該文件刪除。
?
總結:
調用unlink()的時候,文件還是存在的,只是目錄里找不到該文件了,但是已經打開這個文件的進程可以正常讀寫,只有打開這個文件對應的inode的所有fd都關閉時,這個文件才會被刪除。
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的unlink(file_name)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ubuntu命令 图片 壁纸_用Linu
- 下一篇: 计算机网络第七版1-1