link、symlink、readlink、unlink函數的使用

#include <unistd.h>

int link(const char *oldpath, const char *newpath);

作用:創建一個硬鏈接???? ?0成功?? -1 失敗

//代碼

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>int main(int argc, char* argv[])
{if(argc < 3){printf("a.out oldpath newpath\n");exit(0);}int ret = link(argv[1], argv[2]);if(ret == -1){perror("link");exit(1);}return 0;
}

#include <unistd.h>

int symlink(const char *oldpath, const char *newpath);

作用:創建一個軟鏈接??? 0成功? -1失敗

?

#include <unistd.h>

ssize_t readlink(const char *path, char *buf, size_t bufsiz);

作用:讀一個軟鏈接文件其本身的內容(即所鏈接的那個文件的文件路徑或文件名),不是去讀文件內容,將內容讀到buff緩沖區(由用戶維護)。 注意:該函數只能用于軟鏈接文件。

返回值:成功則返回讀取的字節數;失敗則為-1。

//代碼

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>int main(int argc, char* argv[])
{if(argc < 2){printf("a.out softlink\n");exit(1);}char buf[512];int ret = readlink(argv[1], buf, sizeof(buf));if(ret == -1){perror("readlink");exit(1);}buf[ret] = 0;  // 必須的,賦值為0或\0表示字符串結束符,該位及以后的字符不再輸出,如果需要輸出,可以采用循環一個一個的輸出。printf("buf = %s\n", buf);return 0;
}

#include <unistd.h>

int unlink(const char *pathname);

作用:1. 如果是符號鏈接,刪除符號鏈接(即直接刪除該文件);2. 如果是硬鏈接,硬鏈接數減1(簡化的FCB),當減為0時,釋放數據塊和inode;3. 如果文件硬鏈接數為1,但有進程已打開該文件,并持有文件描述符,則等該進程關閉該文件時,kernel才真正去刪除該文件。第3個作用可以讓臨時文件關閉之后自己把自己刪除掉利用該特性創建臨時文件,先opencreat創建一個文件,馬上unlink此文件。unlink后,所有使用該文件的進程結束以后,文件才會被刪除,這些進程在未結束時,unlink刪除的只是文件的目錄項(此時文件已經具有了被刪除的條件,硬鏈接數為0),文件本身的內容即inode未被刪除。以如下代碼為例。

返回值:0成功? -1失敗

//臨時文件自己把自己干掉

[root@localhost work]# vim tmp.c

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>int main( )
{int fd;fd = open( "unlink",O_RDWR | O_CREAT,0664);  //沒有就創建該文件if( fd == -1 ){perror("open file");exit(1);}//打開后立即unlink文件(刪除文件)int fd1;fd1==unlink("unlink");if( fd1 == -1 ){perror("unlink file");exit(1);}//為了證明該文件確實被存在過,則在關閉文件之間進行讀、寫和打印測試int fd2;fd2 = write(fd,"hello unlink!\n",14);if( fd2 == -1 ){perror("write file");exit(1);}int ret;ret = lseek(fd,0,SEEK_SET);  //將文件讀寫指針置于開頭才能讀if( ret == -1 ){perror("lseek file");exit(1);}int fd3;char buff[15]={0};fd3 = read(fd,buff,15);if( fd3 == -1 ){perror("read file");exit(1);}int fd4;fd4 = write(1,buff,fd3);if( fd4 == -1 ){perror("write file");exit(1);}int ret1=close(fd);if( ret1 == -1 ){perror("close file");exit(1);}return 0;
}

[root@localhost work]# ./tmp

hello unlink!

[root@localhost work]# ls?????? ??//可見,沒有unlink文件,自己把自己刪除了

english.txt? ls-l.c? stat.c? statuse? statuse.c? tmp? tmp.c

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/385392.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/385392.shtml
英文地址,請注明出處:http://en.pswp.cn/news/385392.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【Leetcode】113. 路徑總和 II

給定一個二叉樹和一個目標和&#xff0c;找到所有從根節點到葉子節點路徑總和等于給定目標和的路徑。 說明: 葉子節點是指沒有子節點的節點。 示例: 給定如下二叉樹&#xff0c;以及目標和 sum 22&#xff0c; 5 / \ 4 8 / / \ …

目錄操作相關的系統函數

主要介紹幾個常用函數的使用方法&#xff1a;chdir&#xff08;改變進程的當前工作目錄&#xff09;、getcwd&#xff08;獲取當前進程的工作目錄&#xff09;、mkdir&#xff08;創建目錄&#xff09;、rmdir&#xff08;刪除空目錄&#xff09;、opendir&#xff08;打開一個…

1079. Total Sales of Supply Chain (25)

A supply chain is a network of retailers&#xff08;零售商&#xff09;, distributors&#xff08;經銷商&#xff09;, and suppliers&#xff08;供應商&#xff09;-- everyone involved in moving a product from supplier to customer. Starting from one root suppli…

chdir、getcwd、mkdir、rmdir函數

#include <unistd.h> int chdir(const char *path); int fchdir(int fd); 作用&#xff1a;改變調用這一函數的進程&#xff08;即程序執行&#xff09;的當前工作目錄&#xff0c;注意不是shell的當前工作目錄。 返回值&#xff1a;0成功 -1失敗 #include <unis…

【Leetcode | 235】 235. 二叉搜索樹的最近公共祖先

給定一個二叉搜索樹, 找到該樹中兩個指定節點的最近公共祖先。 百度百科中最近公共祖先的定義為&#xff1a;“對于有根樹 T 的兩個結點 p、q&#xff0c;最近公共祖先表示為一個結點 x&#xff0c;滿足 x 是 p、q 的祖先且 x 的深度盡可能大&#xff08;一個節點也可以是它自己…

1090. Highest Price in Supply Chain (25)

A supply chain is a network of retailers&#xff08;零售商&#xff09;, distributors&#xff08;經銷商&#xff09;, and suppliers&#xff08;供應商&#xff09;-- everyone involved in moving a product from supplier to customer. Starting from one root suppli…

opendir、readdir和closedir函數

注意&#xff1a;在Linux中&#xff0c;目錄的輸入格式&#xff1a;/mnt//fghs、/mnt/fghs、/mnt/fghs和/mnt/fghs//是等效的&#xff0c;都一樣。 #include <sys/types.h> #include <dirent.h> DIR *opendir(const char *name); DIR *fdopendir(int fd); 返回…

146. LRU緩存機制

運用你所掌握的數據結構&#xff0c;設計和實現一個 LRU (最近最少使用) 緩存機制。它應該支持以下操作&#xff1a; 獲取數據 get 和 寫入數據 put 。 獲取數據 get(key) - 如果密鑰 (key) 存在于緩存中&#xff0c;則獲取密鑰的值&#xff08;總是正數&#xff09;&#xff…

dup和dup2函數

#include <unistd.h> int dup(int oldfd); int dup2(int oldfd, int newfd); 作用&#xff1a;dup函數實現對一個文件的文件描述符進行復制&#xff0c;復制之后該進程就會新增加一一個文件描述符指向該文件&#xff08;即實現同一個文件對應多個文件描述符&#xff0…

fcntl函數(網絡編程會用)

#include <unistd.h> #include <fcntl.h> int fcntl&#xff08;int fd, int cmd&#xff09;&#xff1b; int fcntl&#xff08;int fd, int cmd, long arg&#xff09;&#xff1b;//long 長整型 int fcntl&#xff08;int fd, int cmd, struct flock *lock…

189. 旋轉數組

給定一個數組&#xff0c;將數組中的元素向右移動 k 個位置&#xff0c;其中 k 是非負數。 示例 1: 輸入: [1,2,3,4,5,6,7] 和 k 3 輸出: [5,6,7,1,2,3,4] 解釋: 向右旋轉 1 步: [7,1,2,3,4,5,6] 向右旋轉 2 步: [6,7,1,2,3,4,5] 向右旋轉 3 步: [5,6,7,1,2,3,4]示例 2: 輸…

58. 最后一個單詞的長度

給定一個僅包含大小寫字母和空格 的字符串&#xff0c;返回其最后一個單詞的長度。 如果不存在最后一個單詞&#xff0c;請返回 0 。 說明&#xff1a;一個單詞是指由字母組成&#xff0c;但不包含任何空格的字符串。 示例: 輸入: "Hello World" 輸出: 5 clas…

CPU和MMU(內存管理單元)

CPU的架構&#xff1a;要求能夠理解從源程序到微指令的整個經歷過程&#xff1a;存儲器的層次結構&#xff08;網絡資源下載到硬盤、磁盤緩存、內存、Cache、寄存器&#xff09;&#xff1b;CPU的四大部分&#xff1a;ALU、CU、中斷系統和寄存器&#xff1b;程序執行的整個過程…

【C++ Primer | 09】容器適配器

一、stack s.push(): 向棧內壓入一個成員&#xff1b; s.pop(): 從棧頂彈出一個成員&#xff1b; s.empty(): 如果棧為空返回true&#xff0c;否則返回false&#xff1b; s.top(): 返回棧頂&#xff0c;但不刪除成員&#xff1b; s.size(): 返回棧內元素…

進程控制塊PCB(進程描述符)

&#xff08;1&#xff09;PCB 每個進程在內核中都有一個進程控制塊&#xff08;PCB&#xff09;來維護進程相關的信息&#xff0c;Linux內核的進程控制塊是task_struct結構體。grep -r “task_struct” / 可以查找根目錄下&#xff0c;包含task_struct的文件文件。或者 find…

103. 二叉樹的鋸齒形層次遍歷

給定一個二叉樹&#xff0c;返回其節點值的鋸齒形層次遍歷。&#xff08;即先從左往右&#xff0c;再從右往左進行下一層遍歷&#xff0c;以此類推&#xff0c;層與層之間交替進行&#xff09;。 例如&#xff1a; 給定二叉樹 [3,9,20,null,null,15,7], 3 / \ 9 20 /…

fork、getpid、getppid函數

#include <unistd.h> pid_t fork(void); 作用&#xff1a;創建一個子進程。 到目前為止&#xff0c;我們可以直到兩種創建進程的方法&#xff1a;1. 通過執行二進制文件來創建一個進程&#xff0c;如&#xff1a;./a.out /bin/ls&#xff1b;2.通過fork函數來創建一個…

107. 二叉樹的層次遍歷 II

給定一個二叉樹&#xff0c;返回其節點值自底向上的層次遍歷。 &#xff08;即按從葉子節點所在層到根節點所在的層&#xff0c;逐層從左向右遍歷&#xff09; 例如&#xff1a; 給定二叉樹 [3,9,20,null,null,15,7], 3/ \9 20/ \15 7返回其自底向上的層次遍歷為&#xff…

循環創建N個子進程

以循環創建5個進程為例&#xff0c;給出如下代碼&#xff0c;分析其錯誤&#xff1a; #include <stdio.h> #include <stdlib.h> #include <unistd.h>int main(void) {int i;pid_t pid;printf("xxxxxxxxxxx\n");for (i 0; i < 5; i){pid fork…

【C++ Primer | 19】控制內存分配

1. 測試代碼&#xff1a; #include <iostream> #include <new> #include <cstring> #include <cstdlib> using namespace std;void* operator new(size_t size) {cout << "global Override operator new" << endl;if (void* p…