The Linux Programming Interface 05 File I/O: Further Details 文件I/O的更多细节

The Linux Programming Interface

File I/O: Further Details

(01) 原子操作

Atomicity is essential to the successful completion of some operations. In particular, it allows us to avoid race conditions.

(02) 重要,man 手册查询错误码, man errno

   ENOENT          No such file or directory (POSIX.1)

(03)原子操作的错误实例,两个进程都声称单独创建t.txt文件

  1 #include <stdio.h>
  2 #include <fcntl.h>
  3 #include <errno.h>
  4 
  5 int main(int argc, char *argv[]) {
  6     int fd;
  7     fd = open(argv[1], O_WRONLY);
  8     if (fd != -1) {
  9         printf("[PID %ld File \"%s\" already exists\n",
 10             (long) getpid(), argv[1]);
 11     } else {
 12         if (errno != ENOENT) {  /* Failed for unexpected reason */
 13             errExit("open");
 14         } else {
 15             printf("[PID %ld] File \"%s\" doesn't exist yet\n",
 16                 (long) getpid(), argv[1]);
 17             if (argc > 2) {
 18                 sleep(30);
 19                 printf("[PID %ld] Done sleeping\n", (long) getpid());
 20             }
 21             /* WINDOW FOR FAILURE */
 22             fd = open(argv[1], O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
 23             if (fd == -1)
 24                 errExit("open");
 25 
 26             printf("[PID %ld] Create file \"%s\" exclusively\n",
 27                 (long) getpid(), argv[1]);
 28         }
 29     }
 30 }
输出:

wang@wang:~/test/tlpi-dist/lib$ [PID 8567] File "t.txt" doesn't exist yet
wang@wang:~/test/tlpi-dist/lib$ ./bad_exclusive_open t.txt
[PID 8586] File "t.txt" doesn't exist yet
[PID 8586] Create file "t.txt" exclusively
wang@wang:~/test/tlpi-dist/lib$
wang@wang:~/test/tlpi-dist/lib$ [PID 8567] Done sleeping
[PID 8567] Create file "t.txt" exclusively
[1]+  Exit 43                 ./bad_exclusive_open t.txt sleep

Using a single open() call that specifies the O_CREAT and O_EXCL flags prevents this possibility by guaranteeing that the check and creation steps are carried out as a single atomic operation.

(04) fcntl函数

One use of fcntl() is to retrieve or modify the access mode and open file status flags of an open file.

int flags, accessMode;
flags = fcntl(fd, F_GEETFL);
if (flags == -1)
    errExit("fcntl");

if (flags & O_SYNC)
    printf("writes are synchronized\n");

(05) 检查文件读写权限

Checking the access mode of the file is slightly more complex, since the O_RDONLY(0), O_WRONLY(1), and O_RDWR(2) constants don't correspond to single bits in the open file status flags.

Therefore, to make this check, we mask the flags value with the constant O_ACCMODE,  and then test for equality with one of the constants:

accessMode = flags & O_ACCESS;
if (accessMode == O_WRONLY || accessMode == O_RDWR)
    printf(
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
"The Linux Programming Interface"是一本适合Linux、Unix以及其他符合POXIS规范的系统开发应用程序的程序开发人员、为Linux和Unix系统之间开发可移植应用程序的开发者、正在学习或教授Linux和Unix系统编程的老师或学生、系统管理员或"power users"以及想要深入了解Linux/UNIX编程接口及其实现方式的人士的书籍。\[2\]这本书主要关注Linux内核的API,因此通常使用术语"Linux"来指代整个系统。\[3\]对于想要深入学习Linux以及相关领域的人来说,通过学习"The Linux Programming Interface"这本书可以进一步加深对Linux系统的理解和应用。\[1\] #### 引用[.reference_title] - *1* *2* [Linux学习-the linux programming interface](https://blog.csdn.net/u010933311/article/details/125609158)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [(译文)The Linux Programming Interface:第1章(历史和标准)](https://blog.csdn.net/axonzw7571/article/details/102395373)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值