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

建设基础化学网站的经验美丽寮步网站建设哪家好

建设基础化学网站的经验,美丽寮步网站建设哪家好,wordpress wp json,东莞ui设计公司Linux文件描述符及重定向 1. 文件描述符2. 文件描述符的本质3. 输出重定向4. 追加重定向5. 标准输出和标准错误的区别5.1 2>&1 1. 文件描述符 当调用系统接口open的时候会返回一个int类型的数字,这个数字就是文件描述符,这里用fd进行接收&#x…

Linux文件描述符及重定向

    • 1. 文件描述符
    • 2. 文件描述符的本质
    • 3. 输出重定向
    • 4. 追加重定向
    • 5. 标准输出和标准错误的区别
      • 5.1 2>&1

1. 文件描述符

当调用系统接口open的时候会返回一个int类型的数字,这个数字就是文件描述符,这里用fd进行接收,我们来看一看fd的值是多少。

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>int main()
{int fd=open("test.txt",O_RDONLY);//设置为只读printf("%d\n",fd);return 0;
}

在这里插入图片描述

可以看到,这里打印出来的值是3,那么这个3有什么来头呢?

我们知道,在Linux下,一切皆文件,缓冲区,文件流,可执行程序,全都是文件,这里是3是因为每一个进程默认会先打开标准输入、标准输出、标准错误这三个文件,而这三个文件就占用了0 1 2这三个文件描述符,因此我们重新创建一个文件的时候自然就只能够从3开始了。

那么有什么证据说默认会打开标准输入、标准输出、标准错误这三个文件呢?

这里以C语言为例,C语言的标准输入、标准输出、标准错误这些其实也都是调用了系统的接口,内部也会有一个文件描述符,就是_fileno

#include <stdio.h>
#include <unistd.h>
#include<sys/types.h>
#include <sys/stat.h>
#include<fcntl.h>int main()
{umask(0);int fd=open("test.txt",O_CREAT|O_WRONLY|O_APPEND|O_TRUNC,0666);printf("stdin:%d\n",stdin->_fileno);printf("stdout:%d\n",stdout->_fileno);printf("stderr:%d\n",stderr->_fileno);printf("test:%d\n",fd);return 0;
}

在这里插入图片描述

这样就可以确定标准输入、标准输出、标准错误这三个的文件描述符了

标准输入0
标准输出1
标准错误2

那么现在使用文件描述符,将消息打印到屏幕上吧

#include <stdio.h>
#include <unistd.h>
#include<sys/types.h>
#include<string.h>
#include<fcntl.h>int main()
{char* msg="hello world\n";write(1,msg,strlen(msg));return 0;
}

在这里插入图片描述

可以看到,是可以直接打印在屏幕上的。

同样的,我们也可以通过文件描述符0来接收用户发来的消息。

#include <stdio.h>
#include <unistd.h>
#include<sys/types.h>
#include<string.h>
#include<fcntl.h>int main()
{char msg[1024];ssize_t s=read(0,msg,sizeof(msg));if(s>0){msg[s]='\0';printf("%s",msg);}return 0;
}

在这里插入图片描述

问:为什么这里我只是从文件描述符为0的文件中读取数据,我却要先输入数据才会读取

答:这是因为标准输入通常是行缓冲的,意味着输入不会立即被传递给程序,而是在用户按下回车键时才传递。

2. 文件描述符的本质

前面说到,文件描述符其实是从0开始的,数组下标同样也是从0开始的,那么我们是不是可以猜想一下它就是一个数组的下标。

在这里插入图片描述

其实这个文件描述符fd就是file struct中fd_array[]中的下标,这个数组存储的是一个file*的结构体的地址,这个地址在内存当中,然后再file这个结构体当中存在一个inode值,操作系统就是通过这个inode去磁盘当中拿到文件的,inode在文件系统中会详细讲解。
在这里插入图片描述

补充:即使是外设的驱动程序,它其实也是一个struct file类型的结构体,在内部实现了读写方式,然后操作系统可以通过这个结构体来拿到它的数据。

在C语言中的FILE其实内部也有一个文件描述符

#include <stdio.h>
#include <unistd.h>
#include<sys/types.h>
#include<string.h>
#include<fcntl.h>int main()
{FILE* fp=fopen("test.txt","w");printf("%d\n",fp->_fileno);return 0;
}

文件描述符的分配规则

从0开始找,如果当前下标没有被使用就是用当前下标作为文件描述符。

#include <stdio.h>
#include <unistd.h>
#include<sys/types.h>
#include<string.h>
#include<fcntl.h>int main()
{close(0);int fd=open("test.txt",O_APPEND|O_CREAT|O_WRONLY,0666);int fd1=open("test1.txt",O_APPEND|O_CREAT|O_WRONLY,0666);printf("%d %d\n",fd,fd1); //0 3return 0;
}

如上代码先关闭了0号文件描述符,因此0号就空出来了,如果这时候open一个文件,那么这个文件的文件描述符就是0,等下一次open文件的时候就会变成3了,因为这时候0 1 2都是已经被用过了的。

3. 输出重定向

输出重定向的原理

  • 输出重定向的原理就是改变原来文件描述符的指向,比如我原来文件描述符1指向的是标准输出流,这时候我把1关闭close(1),然后这时候打开一个文件来把文件描述符1占用掉,这时候,本来应该打印到显示器当中的内容就会被写入重定向的文件当中了。

  • 但这是为什么呢?因为操作系统它不知道你1号文件描述符已经被其他文件占用了啊,它只知道打印到显示器的时候去找1号文件描述符,因此实现了重定向的功能。

#include <stdio.h>
#include <unistd.h>
#include<sys/types.h>
#include<string.h>
#include<fcntl.h>int main()
{close(1);//关闭标准输出流int fd=open("test.txt",O_WRONLY|O_APPEND|O_TRUNC,0666);if(fd<0){perror("open");return 1;}printf("hello world\n");printf("hello world\n");printf("hello Linux\n");return 0;
}

此时查看test.txt就可以发现本来要输出到屏幕上的内容,此时输入到了该文件中

4. 追加重定向

追加重定向就是不覆盖原来的数据。

#include <stdio.h>
#include <unistd.h>
#include<sys/types.h>
#include<string.h>
#include<fcntl.h>int main()
{close(1);//关闭标准输出流int fd=open("test.txt",O_WRONLY|O_APPEND,0666);if(fd<0){perror("open");return 1;}printf("hello world\n");printf("hello world\n");printf("hello Linux\n");return 0;
}

输入重定向

使用0文件描述符关闭标准输入,用scanf输入查看现象

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{close(0);int fd = open("test.txt", O_RDONLY | O_CREAT, 0666);if (fd < 0){perror("open");return 1;}char str[40];while (scanf("%s", str) != EOF){printf("%s\n", str);}close(fd);return 0;
}

scanf这次没有等待用户输入任何东西,而是直接把文件描述符0指向的文件的内容全部读取出来了。

5. 标准输出和标准错误的区别

#include <stdio.h>
#include <unistd.h>
#include<sys/types.h>
#include<string.h>
#include<fcntl.h>int main()
{// stdoutprintf("hello printf\n");fprintf(stdout, "hello fprintf to stdout\n");fputs("hello fputs to stdout\n", stdout);// stderrperror("hello perror");fprintf(stderr, "hello fprintf to stderr\n");fputs("hello fputs to stderr\n", stderr);return 0;
}

在这里插入图片描述

运行结果好像都是一样的啊?当是用重定向的时候,它们之间的区别就提现出来了,加入我关闭了1号文件描述符看看会发生什么呢?

#include <stdio.h>
#include <unistd.h>
#include<sys/types.h>
#include<string.h>
#include<fcntl.h>int main()
{close(1);// stdoutprintf("hello printf\n");fprintf(stdout, "hello fprintf to stdout\n");fputs("hello fputs to stdout\n", stdout);// stderrperror("hello perror");fprintf(stderr, "hello fprintf to stderr\n");fputs("hello fputs to stderr\n", stderr);return 0;
}

在这里插入图片描述

可以看到,只有标准错误被输出了,标准输出好像没反应了,并且perror也报错了。这是由于标准输出是1号文件描述符,标准错误是2号文件描述符,因此,关闭了1号文件描述符对2号文件描述符没有任何影响。

这样设计是为了区分正常输出和错误输出,但如果我们非要把这两打印到一个文件里怎么办呢?

5.1 2>&1

./myfile >log.txt 2>&1

./io:表示执行io这个程序

>log.txt:表示把执行了程序之后的标准输出重定向到log.txt当中

2>&1:表示2号文件描述符重定向到1号文件描述符

#include <stdio.h>
#include <unistd.h>
#include<sys/types.h>
#include<string.h>
#include<fcntl.h>int main()
{// stdoutprintf("hello printf\n");fprintf(stdout, "hello fprintf to stdout\n");fputs("hello fputs to stdout\n", stdout);// stderrperror("hello perror");fprintf(stderr, "hello fprintf to stderr\n");fputs("hello fputs to stderr\n", stderr);return 0;
}

在这里插入图片描述
文件描述符的知识就分享到这,如有错误还望指出。


文章转载自:

http://FMH0UwXb.hwpcm.cn
http://tHype9g9.hwpcm.cn
http://cQ4Sgtoj.hwpcm.cn
http://2ZxO3pGG.hwpcm.cn
http://NjgihzZA.hwpcm.cn
http://y3WC3uAx.hwpcm.cn
http://ZEDBKGwD.hwpcm.cn
http://tWDov8vc.hwpcm.cn
http://NtpLMWsr.hwpcm.cn
http://8NoHtWma.hwpcm.cn
http://RDcwVoMX.hwpcm.cn
http://cPu8WY70.hwpcm.cn
http://aDy62exT.hwpcm.cn
http://IP9cS3m2.hwpcm.cn
http://VcC21fU7.hwpcm.cn
http://vRenRhwg.hwpcm.cn
http://xECWHmum.hwpcm.cn
http://jrUdG0Gy.hwpcm.cn
http://1fR1hp0J.hwpcm.cn
http://8VrHXFU7.hwpcm.cn
http://2S9bQF83.hwpcm.cn
http://5g6TVBVw.hwpcm.cn
http://41XO5In3.hwpcm.cn
http://64po1nf1.hwpcm.cn
http://NNUPcE0w.hwpcm.cn
http://dvWFjT0Q.hwpcm.cn
http://3n95vkp2.hwpcm.cn
http://kKdbTzD4.hwpcm.cn
http://WQnIJqT3.hwpcm.cn
http://acGsMPWE.hwpcm.cn
http://www.dtcms.com/wzjs/694383.html

相关文章:

  • 余姚网站建设设计服装公司 网站怎么做
  • 网站二维码链接怎么做的河间市网站建设公司
  • 网站主题模板下载简洁软件下载网站源码
  • 那个企业建网站好电影站的seo
  • html5结构的网站外贸高端网站定制
  • 深圳保障性住房网网站模版的优化
  • 如何做彩票网站的源码中山网站建设文化
  • 防城港网站seo公司公司网站建设公司
  • 濮阳网格化app淄博做网站seo
  • 网站建设及托管合同模板公司的网站建设费用怎么入账
  • 建站吧wordpress简历主题
  • 做安利能开个人网站今天军事新闻最新消息视频
  • 网站维护基本概念认知物流公司
  • 网站用字体wordpress 主题显示
  • 青海公司网站建设哪家快怎么制作网页模板
  • 南京做网站最好的公司如何搭建自己的网站服务器
  • 导航网址网站怎么做营销qq软件
  • 网站广告位怎么做做知识内容的网站与app
  • 网站建设验收合同谷歌搜索引擎免费
  • 怎么自己建设一个网站浙江建设职业技术学校网站登录
  • 做自己的优惠券网站长春关键词优化报价
  • 旅游营销型网站仿百度图片网页设计
  • 个人建设网站还要备案么wordpress本地做好了
  • 套用别人产品图片做网站手机网站报价单模板
  • 搜索引擎优化自然排名的区别南宁seo结算
  • 携程网网站规划建设特点怎样在网上注册免费的网站
  • 建网站做优化企业官网和小程序的区别
  • 网站开发如何共用菜单栏require网站申请微信支付接口
  • 做网站的模版马克飞象 wordpress
  • 网站怎么seo关键词排名优化推广手机上怎么上传网站