網(wǎng)站首頁 編程語言 正文
需要引入的頭文件:
#inlcude<unistd.h>
1.打開文件
打開一個已存在的文件
int open(const char *pathname, int flags);
新建一個文件并創(chuàng)建權(quán)限
int open(const char *pathname, int flags, mode_t mode);
參數(shù)介紹
pathname:將要打開的文件路徑和名稱
flags:打開標(biāo)志
標(biāo)志介紹:
The argument flags must include one of the following access modes: O_RDONLY, O_WRONLY, or O_RDWR. These request opening the file read- only, write-only, or read/write, respectively.
O_RDONLY 只讀打開
O_RDWR 讀寫打開
O_CREAT 文件不存在則創(chuàng)建
O_APPEND 文件末尾追加
O_TRUNC 清空文件,重新寫入 mode
The following symbolic constants are provided for mode: S_IRWXU 00700 user (file owner) has read, write, and execute permission S_IRUSR 00400 user has read permission S_IWUSR 00200 user has write permission S_IXUSR 00100 user has execute permission S_IRWXG 00070 group has read, write, and execute permission S_IRGRP 00040 group has read permission S_IWGRP 00020 group has write permission S_IXGRP 00010 group has execute permission S_IRWXO 00007 others have read, write, and execute permission S_IROTH 00004 others have read permission S_IWOTH 00002 others have write permission S_IXOTH 00001 others have execute permission
返回值:文件描述符
2. 讀文件
ssize_t read(int fd, void *buf, size_t count);
參數(shù)介紹
fd:對應(yīng)打開的文件描述符buf : 存放數(shù)據(jù)的空間count: 計(jì)劃一次從文件中讀多少字節(jié)數(shù)據(jù)返回值: 實(shí)際讀到的字節(jié)數(shù)
3. 寫文件
ssize_t write(int fd, const void *buf, size_t count);
參數(shù)介紹:
fd :對應(yīng)打開的文件描述符buf:存放待寫入的數(shù)據(jù)count:計(jì)劃一次向文件中寫入多少數(shù)據(jù)
4.關(guān)閉
int close(int fd);
fd :對應(yīng)的文件描述符
分析題
如果父進(jìn)程先打開一個文件,fork 后子進(jìn)程是否可以共享使用?
文件內(nèi)容
代碼
#include<stdio.h> #include<unistd.h> #include<assert.h> #include<fcntl.h> #include<stdlib.h> int main() { char buff[128] = {0}; int fd = open("myfile.txt", O_RDONLY); pid_t pid = fork(); assert(pid != -1); if (pid == 0) { read(fd, buff, 1); printf("child buff = %s\n", buff); sleep(1); read(fd, buff, 1); printf("child buff = %s\n", buff); } else { read(fd, buff, 1); printf("parent buff = %s\n", buff); sleep(1); read(fd, buff, 1); printf("parent buff = %s\n", buff); } close(fd); exit(0); }
運(yùn)行結(jié)果:
結(jié)論:
由于 fork 創(chuàng)建的子進(jìn)程的 PCB 是拷貝父進(jìn)程的,子進(jìn)程的 PCB 中的文件表指向打開文件的指針只是拷貝了父進(jìn)程 PCB 中的值,所以父子進(jìn)程共享父進(jìn)程 fork 之前打開的所有文件描述符。
練習(xí)題
完成對一個文件的復(fù)制(類似命令:cp)
原文件內(nèi)容為:
代碼:
#include<stdio.h> #include<unistd.h> #include<fcntl.h> #include<stdlib.h> #include<assert.h> int main(void) { char buff[128] = {0}; int fdr = open("myfile.txt", O_RDONLY); assert(fdr != -1); int fdw = open("newfile.txt", O_WRONLY | O_CREAT, 0600); assert(fdw != -1); int n = 0; while (n = read(fdr, buff, 128) > 0) { write(fdw, buff, n); } close(fdr); close(fdw); exit(0); }
運(yùn)行示例:
可以看到newfile.txt創(chuàng)建成功
系統(tǒng)調(diào)用和庫函數(shù)的區(qū)別
區(qū)別: 系統(tǒng)調(diào)用的實(shí)現(xiàn)在內(nèi)核中,屬于內(nèi)核空間,庫函數(shù)的實(shí)現(xiàn)在函數(shù)庫中,屬于用戶空間。
系統(tǒng)調(diào)用執(zhí)行過程:
原文鏈接:https://blog.csdn.net/m0_56257585/article/details/121742540
相關(guān)推薦
- 2022-07-29 詳解Go語言中配置文件使用與日志配置_Golang
- 2022-07-18 Linux 文件內(nèi)容瀏覽;cut命令;uniq命令;sort命令;tr命令;
- 2022-08-11 python?groupby函數(shù)實(shí)現(xiàn)分組后選取最值_python
- 2022-12-24 fetch()函數(shù)說明與使用方法詳解_基礎(chǔ)知識
- 2022-04-08 WPF綁定Binding用法_基礎(chǔ)應(yīng)用
- 2022-07-24 Nginx靜態(tài)壓縮和代碼壓縮提高訪問速度詳解_nginx
- 2022-08-26 Python?Decorator裝飾器的創(chuàng)建方法及常用場景分析_python
- 2022-09-18 C#面向?qū)ο缶幊讨幸蕾嚪崔D(zhuǎn)原則的示例詳解_C#教程
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支