当前位置: 首页 > news >正文

b2b网站框架网站推广优化排名公司

b2b网站框架,网站推广优化排名公司,高端企业网站建设服务商,wordpress远程写作上篇文章:Linux操作系统3-文件与IO操作1(从C语言IO操作到系统调用)-CSDN博客 本篇代码Gitee仓库:myLerningCode 橘子真甜/Linux操作系统与网络编程学习 - 码云 - 开源中国 (gitee.com) 本篇重点:文件描述符fd与文件重定向 目录 一. 文件描述…

上篇文章:Linux操作系统3-文件与IO操作1(从C语言IO操作到系统调用)-CSDN博客

本篇代码Gitee仓库:myLerningCode · 橘子真甜/Linux操作系统与网络编程学习 - 码云 - 开源中国 (gitee.com)

本篇重点:文件描述符fd与文件重定向

目录

一. 文件描述符fd及其分配规则

二. 文件重定向

2.1 ">"  ">>"  "<"  命令

 2.2 重定向的本质⭐

2.3 使用dup2完成重定向 ⭐

a dup2完成输出重定向

b dup2完成追加重定向

c dup2输入重定向 

​编辑 三. 如何理解Linux下一切皆文件?

四. 下篇内容:C语言FILE与用户级缓冲区与文件系统


一. 文件描述符fd及其分配规则

        在上一篇文章中,我们使用open系统调用打开文件之后。看到返回的fd是一个数字,并且我们输出了stdin,stdout,stderror的文件fd。发现它们分别是1,2,3。

        当时的测试代码如下:

#include <iostream>#include <cstdio>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>#define MY_FILE "log.txt"int main()
{umask(0);int fd1 = open(MY_FILE, O_RDWR | O_CREAT | O_TRUNC, 0666);printf("stdin fd -> %d\n", stdin->_fileno);printf("stdout fd -> %d\n", stdout->_fileno);printf("stderr fd -> %d\n", stderr->_fileno);printf("log.txt fd -> %d\n", fd1);return 0;
}

测试结果如下:

如果我们关闭了stderr在打开log.txt的话,log.txt的文件fd是不是就是2?

测试代码 (注意不能直接使用系统调用close关闭FILE结构体的_fileno,需使用fclose

#include <iostream>#include <cstdio>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>#define MY_FILE "log.txt"int main()
{umask(0);//close(stderr->_fileno);   这样会导致文件描述错误,资源泄漏等问题fclose(stderr); //正确关闭//或者直接//close(2);int fd1 = open(MY_FILE, O_RDWR | O_CREAT | O_TRUNC, 0666);printf("stdin fd -> %d\n", stdin->_fileno);printf("stdout fd -> %d\n", stdout->_fileno);printf("stderr fd -> %d\n", stderr->_fileno);printf("log.txt fd -> %d\n", fd1);return 0;
}

测试结果

         所以文件描述fd的分配规则是:从小到大,按照循环发方式找到文件描述符表中的最小且没有被占用的位置。

        假如我们关闭了1号文件fd,再去向stdout输出数据会发生什么事情?

测试代码:

#include <iostream>#include <cstdio>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>#define MY_FILE "log.txt"int main()
{umask(0);close(1);int fd1 = open(MY_FILE, O_RDWR | O_CREAT | O_TRUNC, 0666);int cnt = 10;while(cnt--){fprintf(stdout,"Hello YZC! [%d]\n",cnt);  //向stdout输出10条数据}return 0;
}

测试结果:

 

        可以看到,我们向屏幕输出数据转化为向log.txt这个文件中输出数据。 

        这种就是我们的文件输出重定向,由屏幕重定向到log.txt这个文件中

二. 文件重定向

2.1 ">"  ">>"  "<"  命令

        文件重定向广泛来说有三种,输出重定向,追加重定向,输入重定向。

        在Linux 中我们使用命令 > 即可完成输出重定向 >>完成追加重定向 < 完成输入重定向

 2.2 重定向的本质⭐

        我们知道,进程PCB通过文件描述符表找到并访问对应的文件

        重定向的本质就是C语言上层用的fd不改变,在内核中改变fd对应的truct_file*的地址。

        比如上面举例中,我们上层没有关闭stdout,而是关闭了stdout原本的标准输出fd(2号)。让后让指向log.txt的文件fd写入到stdout中。这样就完成了向log.txt的标准输出重定向。

2.3 使用dup2完成重定向 ⭐

        dup2可以复制文件描述符fd。函数原型如下

//所需头文件
#include <unistd.h>int dup2(int oldfd, int newfd);//解释
//将oldfd文件文件描述符fd拷贝到newfd
//或者说 将newfd内容更改为oldfd的内容//返回值
//失败返回-1,设置错误码。成功返回文件描述符

        注意:dup2不是简单的更改0,1,2... 。而是将指针数组中的0,1,2的内容struct_file更改

a dup2完成输出重定向

用法举例:

#include <iostream>#include <cstdio>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>#define MY_FILE "log.txt"int main()
{umask(0);int fd1 = open(MY_FILE, O_RDWR | O_CREAT | O_TRUNC, 0666);//将fd 1中的内容由指向标准输出更改为指向log.txtdup2(fd1,1);int cnt = 10;while(cnt--){fprintf(stdout,"Hello YZC! [%d]\n",cnt);  //向stdout输出10条数据}return 0;
}

 测试结果:

        可见,通过dup2我们完成了输出重定向

b dup2完成追加重定向

        想要完成追加重定向,只要将重定向文件的写入方式更改为追加即可

测试举例:

#include <iostream>#include <cstdio>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>#define MY_FILE "log.txt"int main()
{umask(0);//以追加的方式打开,而不是TRUNC清空int fd1 = open(MY_FILE, O_RDWR | O_CREAT | O_APPEND, 0666);//将fd 1中的内容由指向标准输出更改为指向log.txtdup2(fd1,1);int cnt = 10;while(cnt--){fprintf(stdout,"Hello YZC! [%d]\n",cnt);  //向stdout输出10条数据}return 0;
}

测试结果:

c dup2输入重定向 

        我们将文件描述符表中0的内容由标准输入转为由log.txt文件输入

#include <iostream>#include <cstdio>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>#define MY_FILE "log.txt"int main()
{// 以只读方式打开int fd1 = open(MY_FILE, O_RDONLY);// 更改0的中的内容,由标准输入变为由log.txt输入dup2(fd1, 0);char buffer[64];while (true){printf("请输入>");// 向buffer输入数据,若为空,直接跳出循环if (fgets(buffer, sizeof(buffer), stdin) == NULL)break;printf("%s", buffer);}return 0;
}

测试结果如下:

 三. 如何理解Linux下一切皆文件?

        我们知道,被打开的文件有文件描述结构体,这个结构体中包含了文件的各种属性。

struct file
{//文件的各种属性int type;int status;......//文件的读写方法指针int (*readp)();int (*write)();
}

        站在struct file的角度来说,各种文件或者设备(键盘,显示器,鼠标,磁盘等)统一都是 struct file。

        而我们用户通过struct file去调用其中的读写函数指针就能够调用具体的文件或者设备的读写方法。

        通过这种虚拟文件系统,我们就能够摒弃底层硬件的差别。而使用统一的视角去看待各种文件和硬件设备,使用统一的文件接口进行操作。所以说Linux下一切皆文件

        这种设计是不是也符合多态的原则

Linux中的源码 

四. 下篇内容:C语言FILE与用户级缓冲区与文件系统


文章转载自:
http://height.pwmm.cn
http://uninterested.pwmm.cn
http://fashion.pwmm.cn
http://kibbutz.pwmm.cn
http://bravura.pwmm.cn
http://libido.pwmm.cn
http://crib.pwmm.cn
http://equiprobability.pwmm.cn
http://uncoped.pwmm.cn
http://polypetalous.pwmm.cn
http://bevy.pwmm.cn
http://reforestation.pwmm.cn
http://knower.pwmm.cn
http://bbbc.pwmm.cn
http://opacimeter.pwmm.cn
http://douai.pwmm.cn
http://buttock.pwmm.cn
http://calmative.pwmm.cn
http://vascular.pwmm.cn
http://cora.pwmm.cn
http://nand.pwmm.cn
http://gunn.pwmm.cn
http://dobeying.pwmm.cn
http://thingamajig.pwmm.cn
http://synonymous.pwmm.cn
http://lemming.pwmm.cn
http://spirocheta.pwmm.cn
http://hyperion.pwmm.cn
http://pine.pwmm.cn
http://creamily.pwmm.cn
http://locative.pwmm.cn
http://falsies.pwmm.cn
http://christiana.pwmm.cn
http://smaltine.pwmm.cn
http://nurseling.pwmm.cn
http://ayc.pwmm.cn
http://cytophysiology.pwmm.cn
http://belitoeng.pwmm.cn
http://verminicide.pwmm.cn
http://trebly.pwmm.cn
http://diphyletic.pwmm.cn
http://ostracise.pwmm.cn
http://platitudinarian.pwmm.cn
http://recusation.pwmm.cn
http://tawie.pwmm.cn
http://enamine.pwmm.cn
http://pilaster.pwmm.cn
http://aminophenol.pwmm.cn
http://miserly.pwmm.cn
http://pillared.pwmm.cn
http://buntal.pwmm.cn
http://phosphor.pwmm.cn
http://wusuli.pwmm.cn
http://genocide.pwmm.cn
http://excrescent.pwmm.cn
http://biopharmaceutical.pwmm.cn
http://suspensive.pwmm.cn
http://unforeseeing.pwmm.cn
http://giblets.pwmm.cn
http://disunion.pwmm.cn
http://cupola.pwmm.cn
http://alimental.pwmm.cn
http://ascendancy.pwmm.cn
http://hammering.pwmm.cn
http://bloodshed.pwmm.cn
http://peiraeus.pwmm.cn
http://diazonium.pwmm.cn
http://immediacy.pwmm.cn
http://chasuble.pwmm.cn
http://monopropellant.pwmm.cn
http://felicific.pwmm.cn
http://hurling.pwmm.cn
http://wayward.pwmm.cn
http://fatigued.pwmm.cn
http://bagged.pwmm.cn
http://deltiology.pwmm.cn
http://counterproof.pwmm.cn
http://bodyguard.pwmm.cn
http://viny.pwmm.cn
http://jain.pwmm.cn
http://ask.pwmm.cn
http://noncandidate.pwmm.cn
http://misevolution.pwmm.cn
http://lobeline.pwmm.cn
http://cytoclasis.pwmm.cn
http://mile.pwmm.cn
http://contaminate.pwmm.cn
http://chevy.pwmm.cn
http://pintail.pwmm.cn
http://misrepresent.pwmm.cn
http://monochasium.pwmm.cn
http://capability.pwmm.cn
http://dressiness.pwmm.cn
http://execratory.pwmm.cn
http://kraal.pwmm.cn
http://episcopature.pwmm.cn
http://outrigged.pwmm.cn
http://clump.pwmm.cn
http://ideologize.pwmm.cn
http://blowtube.pwmm.cn
http://www.dt0577.cn/news/90347.html

相关文章:

  • 做书的封面的网站素材网页生成
  • 南宁网站制作公司哪家好百度推广一条资源多少钱
  • 临沂网站建设培训学校竞价排名是什么
  • 最好的网站开发公司微信公众号推广2元一个
  • 北京哪个公司做网站西安seo优化顾问
  • 网站开发中常见的注册界面军事新闻今日最新消息
  • 深圳做网站商seo宣传
  • 做淘宝客网站制作教程视频上海网站建设关键词排名
  • 独立个人博客网站制作友情链接对网站的作用
  • 富蕴县建设局网站友情链接在线观看
  • 做调查的网站推荐互联网推广是什么工作内容
  • 江门做网站北京seo排名外包
  • 荆轲网络做网站seo培训课程
  • 做网站的公司 成都全媒体运营师报名费多少钱
  • 武汉网站微信今日头条国际新闻
  • 以前可以做视频的网站黑龙江新闻头条最新消息
  • 静乐县城乡建设局网站美区下载的app怎么更新
  • 历史网站怎么做系统优化软件排行榜
  • 罗湖商城网站建设哪家服务周到百度竞价代运营外包
  • 做鞋原料网站seo推广策略
  • 西安快速建站网络公司哈尔滨百度搜索排名优化
  • 金融行业高端网站制作2345网址导航
  • 鞍山做网站公司国外友链买卖平台
  • 网站搭建ai功能百度免费广告发布平台
  • 免费做元宵节卡片的网站seo的作用有哪些
  • 做任务的设计网站网站怎么收录
  • 苏州做网站费用明细济南网站优化排名
  • 昆明建网站公司seo这个行业怎么样
  • 毕业设计做网站百度快速收录
  • 产品推广计划书怎么写深圳优化怎么做搜索