文件IO完结
This commit is contained in:
@@ -181,8 +181,6 @@ ssize_t getline(char **lineptr, size_t *n, FILE *stream);
|
||||
char *tmpnam(char *s);
|
||||
FILE *tmpfile(void);
|
||||
|
||||
|
||||
|
||||
```
|
||||
|
||||
|
||||
@@ -251,15 +249,79 @@ off_t lseek(int fd, offt offset, int whence);
|
||||
|
||||
|
||||
### 文件IO与标准IO的区别
|
||||
区别:响应速度&吞吐量
|
||||
文件IO需要频繁进入内核,标准IO通过缓冲区合并系统调用。
|
||||
响应速度快就文件IO,吞吐量大就标准IO。
|
||||
> [!warning]
|
||||
> 二者不可混用
|
||||
|
||||
转换方法:`fileno`, `fdopen`
|
||||
|
||||
### IO的效率问题
|
||||
习题:将`mycpy.c`程序进行更改,将`BUFSIZE`的值放大,观察进程消耗的时间,注意性能出现拐点的值以及程序何时段错误。
|
||||
|
||||
### 文件共享
|
||||
多个任务共同操作一个文件或者协同完成任务
|
||||
|
||||
面试题:写程序删除一个文件的第10行
|
||||
补充函数:
|
||||
```c
|
||||
// 截断文件到某长度
|
||||
int truncate(const char *path, off_t length);
|
||||
int ftruncate(int fd, off_t length);
|
||||
```
|
||||
|
||||
```c
|
||||
// 最简单思路,将11行开始的内容到第10行开始处覆盖写
|
||||
while()
|
||||
{
|
||||
lseek 11 + read +lseek 10 + write
|
||||
}
|
||||
|
||||
// 优化思路,两个文件描述符,一个读一个写
|
||||
1 -> open r -> fd1 -> lseek 11
|
||||
2 -> open r+ -> fd2 -> lseek 10
|
||||
|
||||
while()
|
||||
{
|
||||
1->fd1-> read
|
||||
2->fd2-> write
|
||||
}
|
||||
|
||||
// 两个进程, 设计进程间通信
|
||||
process1 -> open -> r
|
||||
process2 -> open -> r+
|
||||
|
||||
p1->read -> p2->write
|
||||
|
||||
```
|
||||
|
||||
### 原子操作
|
||||
指不可分割的操作
|
||||
作用:解决竞争和冲突
|
||||
如`tmpnam`函数,产生文件名和创建文件是两步,会有并发问题。
|
||||
|
||||
|
||||
### 程序中的重定向:`dup`, `dup2`
|
||||
```c
|
||||
int dup(int oldfd);
|
||||
int dup2(int oldfd, int newfd);
|
||||
```
|
||||
|
||||
### 同步:`sync`, `fsync`, `fdatasync`, `fcntl`, `ioctl`
|
||||
### 同步
|
||||
同步内核层面的buffer和cache
|
||||
```c
|
||||
void sync(void);
|
||||
int fsync(int fd);
|
||||
int fdatasync(int fd); // 只刷新数据,不刷新亚数据
|
||||
|
||||
// 文件描述符所有的操作几乎都来源于该函数
|
||||
int fcntl(int fd, int cmd, ... /* arg */);
|
||||
|
||||
// 设备相关的内容
|
||||
int ioctl(int fd, unsigned long request, ... /* arg */);
|
||||
|
||||
```
|
||||
|
||||
### /dev/fd/目录
|
||||
**虚目录**:显示当前进程的文件描述符信息
|
||||
|
||||
27
Chapter13/io/sys/ab.c
Normal file
27
Chapter13/io/sys/ab.c
Normal file
@@ -0,0 +1,27 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
putchar('a');
|
||||
write(1, "b", 1);
|
||||
|
||||
putchar('a');
|
||||
write(1, "b", 1);
|
||||
|
||||
putchar('a');
|
||||
write(1, "b", 1);
|
||||
|
||||
/**
|
||||
* 这里标准IO与文件IO混用
|
||||
* 实际输出:
|
||||
* bbbaaa
|
||||
*
|
||||
* 命令 strace
|
||||
* 可以跟踪系统调用
|
||||
*
|
||||
*/
|
||||
|
||||
exit(0);
|
||||
}
|
||||
51
Chapter13/io/sys/dup.c
Normal file
51
Chapter13/io/sys/dup.c
Normal file
@@ -0,0 +1,51 @@
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define FNAME "/tmp/out"
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int fd;
|
||||
|
||||
/***
|
||||
* bug:
|
||||
* 1. 假如默认没有1,这里有bug。
|
||||
* 2. 假如这个非原子操作的操作的过程,有可能被别的进程影响。
|
||||
*/
|
||||
fd = open(FNAME, O_WRONLY | O_CREAT | O_TRUNC, 0600);
|
||||
if (fd < 0)
|
||||
{
|
||||
perror("open");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// close(1);
|
||||
// dup(fd);
|
||||
dup2(fd, 1);
|
||||
// 使用 dup2 原子操作
|
||||
// 且dup2如果两个参数相等,不会做任何事
|
||||
|
||||
// !!!
|
||||
if (1 != fd)
|
||||
close(fd);
|
||||
|
||||
#if 0
|
||||
close(1);
|
||||
|
||||
fd = open(FNAME, O_WRONLY | O_CREAT | O_TRUNC, 0600);
|
||||
if (fd < 0)
|
||||
{
|
||||
perror("open");
|
||||
exit(1);
|
||||
}
|
||||
#endif
|
||||
|
||||
/********************************************************************/
|
||||
puts("hello!");
|
||||
|
||||
// TODO: 将sdtout回到终端
|
||||
|
||||
exit(0);
|
||||
}
|
||||
@@ -4,6 +4,11 @@
|
||||
#include <unistd.h>
|
||||
|
||||
#define BUFSIZE 1024
|
||||
/**
|
||||
* 可以用 time 命令找到 BUFSIZE 最佳值
|
||||
*
|
||||
* time ./mycpy /etc/services /tmp/out
|
||||
*/
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
@@ -47,6 +52,7 @@ int main(int argc, char **argv)
|
||||
pos = 0;
|
||||
while (len > 0)
|
||||
{
|
||||
/* 可能没写完被别的中断打断 */
|
||||
ret = write(dfd, buf + pos, len);
|
||||
if (ret < 0)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user