首页 > 系统相关 >[Linux]----文件操作(重定向+缓冲区)

[Linux]----文件操作(重定向+缓冲区)

时间:2022-11-05 14:01:34浏览次数:62  
标签:fp char stdout printf ---- Linux 缓冲区 hello


文章目录

  • ​​前言​​
  • ​​一、重定向​​
  • ​​具体操作​​
  • ​​dup2​​
  • ​​二、关于缓冲区的理解​​
  • ​​1. 什么是缓冲区​​
  • ​​2. 为什么要有缓冲区​​
  • ​​3. 缓冲区在哪里​​
  • ​​刷新策略机制​​
  • ​​如果在刷新之前,关闭了fd会有什么问题?​​
  • ​​FLIE内部的封装​​
  • ​​特殊情况​​
  • ​​模拟实现封装C标准库​​
  • ​​标准输出和标准错误​​
  • ​​总结​​

前言

本节继续基于上节文件描述符继续往下拓展,讲一讲关于文件操作的重定向和缓冲区。


​正文开始!​

首先来基于上节课的问题

#include<stdio.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
#include<unistd.h>
#include<string.h>
int main()
{
close(1);
int fd=open("log.txt",O_WRONLY|O_CREAT|O_TRUNC,0666);
if(fd<0)
{
perror("open");
return 1;
}
fprintf(stdout,"打开文件成功,fd=%d",fd);
//fflush(stdout);
close(fd);
}

[Linux]----文件操作(重定向+缓冲区)_linux


我们发现既没有往显示器打印,也没有往文件里面打印!

然后我们将fflush()这一行代码取消注释后

[Linux]----文件操作(重定向+缓冲区)_#include_02


我们可以发现打印内容到了文件中了。至于为什么要用fflush()函数,需要了解到缓冲区的内容,接下来带大家理解!

那我有一个问题了?为什么不往显示器去打印,而是打印在了文件中呢???

一、重定向

[Linux]----文件操作(重定向+缓冲区)_#include_03


如果我们要进行重定向,上层只认0,1,2,3这样的fd,我们可以在OS内部,通过一定的方式调整数组的特定下标内容够(指向),我们就可以完成重定向操作!

对于上图,我们进行重定向后,fprintf并不知道1号文件描述符指向了"log.txt"文件,而继续向1号文件描述符打印东西。

具体操作

上面的一堆的数据,都是内核数据结构。只有谁有权限呢???

必定是操作系统(OS)---->必定提供系统结构!

dup2

[Linux]----文件操作(重定向+缓冲区)_运维_04

相比于dup,dup2更复杂一些,我们今天主要使用多duo2进行重定向操作!

[Linux]----文件操作(重定向+缓冲区)_#include_05


在这里我们首先进行输出重定向

stdout–>1 log.txt–>fd

  1. 那么对于dup2()接口,谁是谁的一份拷贝呢?
    对于上面框起来的内容翻译就是newfd是oldfd的一份拷贝,就是把oldfd的内容放置newfd里面。最后只剩oldfd了!!!
  2. 参数怎么传呢??
    我们要输出重定向到文件中,即就是stdout的输出到文件中,即就是1号文件描述符的内容要指向新创建文件的描述符。

dup2(fd,1);

int main()
{

int fd=open("log.txt",O_WRONLY|O_CREAT|O_TRUNC,0666);
if(fd<0)
{
perror("open");
return 1;
}
dup2(fd,1);
fprintf(stdout,"打开文件成功,fd=%d",fd);

fflush(stdout);
close(fd);
}

[Linux]----文件操作(重定向+缓冲区)_#include_06

输入重定向

int main()
{

int fd=open("log.txt",O_RDONLY);
if(fd<0)
{
perror("open");
return 1;
}
dup2(fd,0);
char line[64];
while(fgets(line,sizeof(line),stdin)!=NULL)
{
printf(line);
}

fflush(stdout);
close(fd);
}

[Linux]----文件操作(重定向+缓冲区)_#include_07

二、关于缓冲区的理解

1. 什么是缓冲区

  • 缓冲区的本质:就是一段内存。

2. 为什么要有缓冲区

  • 解放使用缓冲区的进程时间
  • 缓冲区的存在可以集中处理数据刷新,减少IO的次数,从而达到提高整机的效率的目的!

3. 缓冲区在哪里

我来写一份代码带大家验证一下!

int main()
{
printf("hello printf\n"); //stdout-->1
const char* msg="hello write\n";
write(1,msg,strlen(msg));
}

[Linux]----文件操作(重定向+缓冲区)_重定向_08


去掉’'以后

int main()
{
printf("hello printf"); //stdout-->1
const char* msg="hello write";
write(1,msg,strlen(msg));
}

[Linux]----文件操作(重定向+缓冲区)_#include_09


printf没有立即刷新的原因,是因为有缓冲区的存在

write可是立即刷新的!

所以我们根据以上的实验现象我们可以发现stdout必定封装了write!

那么这个缓冲区不在哪里?? ---->一定不在wirte内部!

所以我们曾经讨论的缓冲区,不是内核级别的!

所以这个缓冲区在哪里???—>只能是C语言提供的!!!(语言级别的缓冲区)

因为printf是往stdout中打印,stdout—>FILE—>struct—>封装很多的属性---->fd—>该FILE对于的语言级别的缓冲区!

[Linux]----文件操作(重定向+缓冲区)_运维_10

刷新策略机制

int main()
{
printf("hello printf");//stdout->1->封装了write
fprintf(stdout,"hello fprintf");
fputs("hello fputs",stdout);

const char* msg="hello write";
write(1,msg,strlen(msg));

return 0;
}

[Linux]----文件操作(重定向+缓冲区)_重定向_11

什么时候刷新?

  • 无缓冲(立即刷新)
  • 行缓冲(逐行刷新)—>显示器文件
  • 全缓冲(缓冲区满,刷新)—>块设备对应的文件(磁盘文件)

无缓冲的特殊情况
a.进程退出
b.用户强制刷新

如果在刷新之前,关闭了fd会有什么问题?

int main()
{
printf("hello printf");//stdout->1->封装了write
fprintf(stdout,"hello fprintf");
fputs("hello fputs",stdout);

const char* msg="hello write";
write(1,msg,strlen(msg));
close(1);
//close(stdout->_fileno);//和上面close(1)效果相同
return 0;
}

[Linux]----文件操作(重定向+缓冲区)_服务器_12


一开始我们只是把数据写入到FILE结构体的缓冲区,然后你把文件描述符给关了,当然就写不进文件中了!!!

[Linux]----文件操作(重定向+缓冲区)_重定向_13


现在我们就能来理解一开始我们抛出的问题了!!!

既然缓冲区在FILE内部,在C语言中,而我们每一次打开一个文件,都要有一个FILE*会返回!!
那就意味着,每一个文件都有一个fd和属于他自己的语言级别的缓冲区!!!

FLIE内部的封装

在/usr/include/libio.h
struct _IO_FILE {
int _flags; /* High-order word is _IO_MAGIC; rest is flags. */
#define _IO_file_flags _flags
//缓冲区相关
/* The following pointers correspond to the C++ streambuf protocol. */
/* Note: Tk uses the _IO_read_ptr and _IO_read_end fields directly. */
char* _IO_read_ptr; /* Current read pointer */
char* _IO_read_end; /* End of get area. */
char* _IO_read_base; /* Start of putback+get area. */
char* _IO_write_base; /* Start of put area. */

[Linux]----文件操作(重定向+缓冲区)_linux_14

特殊情况

int main()
{
const char* str1="hello printf\n";
const char* str2="hello fprintf\n";
const char* str3="hello fputs\n";
const char* str4="hello write\n";

//C库函数
printf(str1);
fprintf(stdout,str2);
fputs(str3,stdout);

//系统接口
write(1,str4,strlen(str4));

//是调用完了上面的代码,才执行的fork
fork();
}

[Linux]----文件操作(重定向+缓冲区)_linux_15

对代码去掉’\n’

int main()
{
const char* str1="hello printf";
const char* str2="hello fprintf";
const char* str3="hello fputs";
const char* str4="hello write";

//C库函数
printf(str1);
fprintf(stdout,str2);
fputs(str3,stdout);

//系统接口
write(1,str4,strlen(str4));

//是调用完了上面的代码,才执行的fork
fork();
}

[Linux]----文件操作(重定向+缓冲区)_linux_16

  1. 刷新的本质,就是把缓冲区的数据写到OS内部,清空缓冲区!
  2. 缓冲区是自己的FILE内部维护的,属于父进程
  3. 子进程也继承了父进程的缓冲区,也就打印了父进程缓冲区的内容!

模拟实现封装C标准库

只封装了C语言库的一部分!

#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include <sys/stat.h>
#include<fcntl.h>
#include<string.h>
#include<assert.h>
#include<stdlib.h>

#define NUM 1024

#define NONE_FLUSH 0x0
#define LINE_FLUSH 0x1
#define FULL_FLUSH 0x2

typedef struct _MyFILE
{
int _fileno;
char buffer[NUM];
int _end;
int _flags;//fflush method
}MyFILE;

MyFILE* my_fopen(const char* filename,const char* method)
{
assert(filename&&method);
int flags=O_RDONLY;

if(strcmp(method,"r")==0)
{}
else if(strcmp(method,"r+")==0)
{}
else if(strcmp(method,"w")==0)
{
flags=O_WRONLY|O_CREAT|O_TRUNC;

}
else if(strcmp(method,"w+")==0)
{}
else if(strcmp(method,"a")==0)
{
flags=O_WRONLY|O_CREAT|O_APPEND;
}
else if(strcmp(method,"a+")==0)
{}

int fileno=open(filename,flags,0666);
if(fileno<0) return NULL;
MyFILE* fp=(MyFILE*)malloc(sizeof(MyFILE));
if(fp==NULL)
{
return NULL;
}
memset(fp,0,sizeof(MyFILE));
fp->_fileno=fileno;
fp->_flags|=LINE_FLUSH;
fp->_end=0;
return fp;
}

void my_fflush(MyFILE* fp)
{
assert(fp);
if(fp->_end>0)
{
write(fp->_fileno,fp->buffer,fp->_end);
fp->_end=0;
syncfs(fp->_fileno);
}

}


void my_fwrite(MyFILE* fp,const char* start,int len)
{
assert(fp&&start&&len>0);

strncpy(fp->buffer+fp->_end,start,len);//将数据写入到缓冲区了
fp->_end+=len;

if(fp->_flags&NONE_FLUSH)
{

}
else if(fp->_flags&LINE_FLUSH)
{
if(fp->_end>0&&fp->buffer[fp->_end-1]=='\n')
{
//仅仅是写入到内核中
write(fp->_fileno,fp->buffer,fp->_end);
fp->_end=0;
}
}
else if(fp->_flags&FULL_FLUSH)
{

}
}

void my_close(MyFILE* fp)
{
my_fflush(fp);
close(fp->_fileno);
free(fp);
}

int main()
{
MyFILE* fp=my_fopen("log.txt","w");
if(fp==NULL)
{
printf("my_fopen fail\n");
return 1;
}
const char* s="hello my 111\n";
my_fwrite(fp,s,strlen(s));

printf("消息立即刷新");
sleep(3);


const char* ss="hello my 222";
my_fwrite(fp,ss,strlen(ss));
sleep(3);
printf("写入了一个不满足条件的字符串\n");


const char* sss="hello my 333";
my_fwrite(fp,sss,strlen(sss));
sleep(3);
printf("写入了一个不满足条件的字符串\n");


const char* ssss="end\n";
my_fwrite(fp,ssss,strlen(ssss));
sleep(3);
printf("写入了一个满足条件的字符串\n");

const char* sssss="aaaaaaaaaa";
my_fwrite(fp,sssss,strlen(sssss));
printf("写入了一个不满足条件的字符串\n");

sleep(1);
my_fflush(fp);
sleep(3);

my_close(fp);
return 0;
}

[Linux]----文件操作(重定向+缓冲区)_运维_17

标准输出和标准错误

#include<iostream>
#include<cstdio>

int main()
{
//stdout
printf("hello printf 1\n");
fprintf(stdout,"hello fprintf 1\n");
fputs("hello puts 1\n",stdout);

//stderr
fprintf(stderr,"hello fprintf 2\n");
fputs("hello puts 2\n",stderr);
perror("hello perror 2");

//cout
std::cout<<"hello cout 1"<<std::endl;

//cerr
std::cerr<<"hello cerr 2"<<std::endl;

}

[Linux]----文件操作(重定向+缓冲区)_linux_18

我们发现打1的都不见了

[Linux]----文件操作(重定向+缓冲区)_服务器_19

[Linux]----文件操作(重定向+缓冲区)_服务器_20


[Linux]----文件操作(重定向+缓冲区)_#include_21


向stdout里面打的都重定向到log.txt文件中,stderr继续打印到显示器。

[Linux]----文件操作(重定向+缓冲区)_重定向_22

./a.out >stdout.txt 2>stderr.txt

一条语句进行两次重定向

那么意义在哪里呢?

可以区分那些是程序日常输出,那些是错误!

我们也可以将上面的两个文件打印在一个文件

./a.out >all.txt 2>&1

[Linux]----文件操作(重定向+缓冲区)_重定向_23


总结

(本章完!)
下节课我们基于重定义的学习来完善我们之前写的myshell!!!


标签:fp,char,stdout,printf,----,Linux,缓冲区,hello
From: https://blog.51cto.com/u_15612778/5825958

相关文章

  • containerd和shim通信
    containerd-shim-runc-v2启动了一个ttrpcserver(类似grpc)containerd通过ttrpc和containerd-shim-runc-v2 通信来管理容器serviceTask{ rpcState(StateReques......
  • 关于rocketmq 中日志文件路径的配置
    rocketmq中的数据和日志文件默认都是存储在​​user.home​​路径下面的,往往我们都需要修改这些路径到指定文件夹以便管理。服务端日志网上搜索rocketmq日志存储路径的修改......
  • Atomsk孪晶多晶建模方法
    大家好,我是小马老师。本文介绍atomsk孪晶多晶建模方法。atomsk多晶建模的原理是先建立一个晶胞,然后编写一个polycrystal.txt文件,设定最终模型的尺寸和晶粒的个数。atomsk按......
  • 29. 两数相除
    29.两数相除题解:a/b=y等价于b>a-(b*y)>0,只要求出减了多少次b就好了。一个一个地减b,效率太低了,最坏情况下,a=2^31-1,b=1,超时;应该先预......
  • JS中的变量声明
    一、引入1.定义:在JavaScript中创建变量被称为“声明”变量。JavaScript中变量声明又分为显示声明和隐式声明。其中显示声明中,被“声明”变量之前的关键词有var、let、cons......
  • Typora打不开了
    按Windows+R打开运行窗口,输入regedit,点确定,打开注册表,依次展开计算机\HKEY_CURRENT_USER\SOFTWARE\Typora,然后在Typora上右键,点权限,选中Administrtors,把权限全部设置为拒......
  • Jmeter断言之Xpath Assertion
    Xpath:XML路径语言(XMLPathLanguage),它是一种用来确定XML文档中某部分位置的语言。首先添加XpathAssertionXpathAssertion界面......
  • 分数求和
    【题目名称】分数求和【题目内容】计算1/1-1/2+1/3-1/4+1/5……+1/99-1/100的值,打印出结果第一种:运用两个for循环 ,整体思路为:1/1+1/3+1/5+.....+1/99和1/2+1/4+1/6......
  • 使用nvm配置nodejs,已经nodejs使用的初始化步骤
    <-----------------------------------------nvm是什么------------------------------------------------->nvm:进行node版本切换管理手动配置nvm,需要进入地址https://gi......
  • computed和watch的区别
    计算属性computed:1.支持缓存,只有依赖数据发生改变,才会重新进行计算2.不支持异步,当computed内有异步操作时无效,无法监听数据的变化3.computed属性值会默认走缓存,计......