日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

unlink(file_name)

發布時間:2023/12/20 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 刪除目錄
#include<unistd.h> #include<stdio.h> #include<fcntl.h> #include<assert.h>int main() {int fd = open("/zgaoq/q.txt", O_RDWR | O_TRUNC | O_CREAT, 0664);if (df < 0) {return -1; }if(unlink("/zgaoq/q.txt") < 0){printf("unlink errpr!\n");}char buff[256] = {0};write(fd, "hello world!", 12);if(lseek(fd,0,SEEK_SET) == -1){printf("lseek error!\n");}read(fd, buff, 12);printf("%s\n",buff);return 0; }

因此:執行unlink(filename)之后,并不會立即刪除文件,還可以對文件進行讀寫操作,只有當打開這個文件對應的inode的文件描述符都關閉了之后才會將該文件刪除。

?

總結:

調用unlink()的時候,文件還是存在的,只是目錄里找不到該文件了,但是已經打開這個文件的進程可以正常讀寫,只有打開這個文件對應的inode的所有fd都關閉時,這個文件才會被刪除。

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的unlink(file_name)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。