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

wordpress 自定义页面模版优化公司排行榜

wordpress 自定义页面模版,优化公司排行榜,北京的电商平台网站,wordpress短链文章目录 访问文件的本质文件描述符fd0 & 1 & 2文件描述符的分配规则 访问文件的本质 文件描述符fd fd文件描述符就是一个小整数int&#xff0c;本质是数组的下标 FILE c库自己封装的结构体。这里面必定封装文件描述符fd。 验证&#xff1a; #include <stdio.h&…

文章目录

    • 访问文件的本质
      • 文件描述符fd
        • 0 & 1 & 2
        • 文件描述符的分配规则

访问文件的本质

image-20250323225144090

文件描述符fd

fd文件描述符就是一个小整数int,本质是数组的下标

FILE c库自己封装的结构体。这里面必定封装文件描述符fd

验证:

#include <stdio.h>  
#include <unistd.h>  
#include <stdlib.h>  
#include <string.h>  
#include <sys/types.h>  
#include <sys/stat.h>  
#include <fcntl.h>  int main()  
{  int fd1=open("log1.txt",O_WRONLY|O_CREAT|O_APPEND,0666);  int fd2=open("log2.txt",O_WRONLY|O_CREAT|O_APPEND,0666);  int fd3=open("log3.txt",O_WRONLY|O_CREAT|O_APPEND,0666);  int fd4=open("log4.txt",O_WRONLY|O_CREAT|O_APPEND,0666);  if(fd1<0)  {  printf("open fail!\n");  return 1;  }  printf("fd1:%d\n",fd1);  printf("fd2:%d\n",fd2);  printf("fd3:%d\n",fd3);                                                               printf("fd4:%d\n",fd4);            return 0;                
}

image-20250323225755882

0 & 1 & 2

Linux进程默认情况下会有3个缺省打开的文件描述符,分别是标准输入0, 标准输出1, 标准错误2.

0,1,2对应的物理设备一般是:键盘,显示器,显示器

image-20250323204621204

证明:

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

image-20250323230541588

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>int main()
{char buffer[1024];//现在读出来的是字节,不是字符串                                                        ssize_t s=read(0,buffer,sizeof(buffer));if(s<0)return 1; buffer[s]='\0';                              //c语言要把读出来的信息当成字符串,所以要加\0printf("echo : %s\n",buffer);                             return 0;                       
} 

image-20250323231238824

所以,进程默认就给我们打开了0(标准输入),1(标准输出),2(标准错误).

这个特性是操作系统的特性。

进程默认会打开键盘、显示器、显示器。

OS为什么默认要打开?

因为编程要用键盘输入,和显示器查看结果。


所以输入输出还可以采用如下方式:

而现在知道,文件描述符就是从0开始的小整数。

当我们打开文件时,操作系统在内存中要创建相应的数据结构来描述目标文件。

于是就有了file结构体,表示一个已经打开的文件对象。

而进程执行open系统调用,所以必须让进程和文件关联起来。

每个进程都有一个指针*files, 指向一张表files_struct,该表最重要的部分就是包涵一个指针数组,

每个元素都是一个指向打开文件的指针!所以,本质上,文件描述符就是该数组的下标。

所以,只要拿着文件描述符,就可以找到对应的文件.

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>int main()
{printf("stdin->fd:%d\n",stdin->_fileno);printf("stdout->fd:%d\n",stdout->_fileno);printf("stderr->fd:%d\n",stderr->_fileno);return 0;                                                                         
} 

image-20250323232449898


#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>int main()
{close(1);int n=printf("stdin->fd:%d\n",stdin->_fileno);printf("stdout->fd:%d\n",stdout->_fileno);printf("stderr->fd:%d\n",stderr->_fileno);fprintf(stderr,"ret : %d\n",n);return 0;                                                                          
} 

image-20250323233221409

在stdin中,向显示器 printf 打印了12个字符,但是因为关掉了1(显示器),

所以,我们看不到打印的 stdin->fd:0

那为什么 fprintf 可以打印出来呢?

因为stderr文件描述符是2号,关闭的是1号

一个文件可以被多个进程同时打开

count引用计数

一个指针指向一个文件,count++

关闭文件:count–,将文件描述符的指针置空。

count==0 释放 struct file 对象

文件描述符的分配规则

open成功就return fd,否则就return -1,

因为fd本质是数组下标,所以没有负数,负数就是失败。

image-20250325131506103

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>#define filename "log.txt"int main()
{int fd=open(filename,O_CREAT|O_WRONLY|O_TRUNC,0666);if(fd<0){perror("open fail!\n");return 1;}printf("fd:%d\n",fd);const char*msg="Hello Linux!\n";int cnt=3;while(cnt--){write(fd,msg,strlen(msg));}close(fd);return 0;
}

image-20250325132425591

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>#define filename "log.txt"int main()
{close(0);int fd=open(filename,O_CREAT|O_WRONLY|O_TRUNC,0666);if(fd<0){perror("open fail!\n");return 1;}printf("fd:%d\n",fd);const char*msg="Hello Linux!\n";int cnt=3;while(cnt--){write(fd,msg,strlen(msg));}close(fd);return 0;
}

image-20250325132627680

close(0); -> close(1);

image-20250325132729316

为什么把1号文件描述符关了就什么都不输出了?

printf 是要向显示器打印的,但是显示器文件描述符stdoutfd=1)关闭了,所以显示不出来。

进程所匹配的文件描述符已经被关掉了。

close(1); -> close(2);

image-20250325133257903

又可以显示了,因为 printf 是向1号文件描述符打的,现在关闭2号,所以可以显示。

文件描述符的分配规则:

files_struct 数组当中,从0下标开始,找到当前没有被使用的最小的一个下标,作为新的文件描述符。

http://www.dtcms.com/wzjs/155756.html

相关文章:

  • wordpress 如何修改主题宽度网站的seo优化报告
  • 省建设安全监督站的网站上海网络优化seo
  • 网站怎么做外链seocui cn
  • 找衣服款式的网站澳门seo关键词排名
  • 给我免费播放在线seo网站优化外包
  • 快云助手网站建设视频应用商店aso优化
  • 网站建设框架注意事项拉新奖励的app排行
  • 手机网站刷排名调研报告万能模板
  • 网站外链内链怎么做企业软文范例
  • 企业网站建设原则是( )河南今日头条新闻
  • 专业的深圳网站建设公司快速网站排名提升工具
  • 做视频网站的上市公司网络营销的六大特征
  • 做公司网站比较好的如何提升网站seo排名
  • 深圳p2p网站开发深圳企业网站制作
  • 网站建设招标公告公关公司一般收费标准
  • 济南产品网站建设外包seo优化专员工作内容
  • 云南省建设造价协会网站营销广告语
  • 福州网站制作专业网站首页快速收录
  • 网站建设合同规范网站推广的基本方法有
  • 沈阳专业网站制作深圳网站关键词优化推广
  • 网站开发 报价去了外包简历就毁了吗
  • 网站微信认证费用吉林seo基础知识
  • 江苏常州青之峰做网站湖北百度推广公司
  • 企业建立网站需要提供什么外包公司什么意思
  • 群晖做网站服务器速度快吗肇庆网站快速排名优化
  • 网站怎么做才美观广告推广策划
  • 个人网站网站建设网络运营具体做什么
  • 做网站建设怎么赚钱百度快照手机入口
  • 徐州东站网址大全名称
  • 个人怎样做旅游网站成都排名推广