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的鏈接,而不是文件的本身.
????當你調(diào)用unlink的時候他直接把目錄項里的該條鏈接刪除了,但是inode并沒有動,該文件還是存在的,這時候你會發(fā)現(xiàn),目錄里找不到該文件,但是已經(jīng)打開這個文件的進程可以正常讀寫.只有當打開這個inode的所有文件描述符被關閉,指向該inode的鏈接數(shù)為0的情況下,這個文件的inode才會被真正的刪除.
????從unlink的名字上就應該能判斷出來,unlink含義為取消鏈接,remove才是刪除的意思
?
unlink()函數(shù)功能即為刪除文件。執(zhí)行unlink()函數(shù)會刪除所給參數(shù)指定的文件。
unlink()函數(shù)返回值: 成功返回0,失敗返回 -1
remove()與unlink()的區(qū)別:
- 當remove() 中的pathname指定問文件時,相當于調(diào)用unlink 刪除文件鏈接
- 當remove() 中的pahtname指定為目錄時,相當于調(diào)用rmdir 刪除目錄
因此:執(zhí)行unlink(filename)之后,并不會立即刪除文件,還可以對文件進行讀寫操作,只有當打開這個文件對應的inode的文件描述符都關閉了之后才會將該文件刪除。
?
總結(jié):
調(diào)用unlink()的時候,文件還是存在的,只是目錄里找不到該文件了,但是已經(jīng)打開這個文件的進程可以正常讀寫,只有打開這個文件對應的inode的所有fd都關閉時,這個文件才會被刪除。
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的unlink(file_name)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ubuntu命令 图片 壁纸_用Linu
- 下一篇: 计算机网络第七版1-1