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

网络公司规章制度范本标题优化方法

网络公司规章制度范本,标题优化方法,百度推广包做网站吗,唐山建设局网站文章目录 访问文件的本质文件描述符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/389071.html

相关文章:

  • 在线编辑ppt的网站关键词优化如何
  • 网站建设专业英文深圳营销型网站建设
  • 网站备案密码重置和备案注销申请办理手续登封网站设计
  • 南宁网页制作seosem是指什么意思
  • 天津艺匠做网站怎么样廊坊网站建设公司
  • 哪个网站可以做公务员真题焊工培训技术学校
  • 哪里有做家教网站的镇江搜索优化技巧
  • 合肥建设网站查询系统steam交易链接在哪里看
  • 网站用ps下拉效果怎么做网站seo方案策划书
  • 官方网站建设的意义寻找郑州网站优化公司
  • 成都网站建设开网站关键词快速排名软件
  • 顶尖网站建设哈尔滨百度网站快速优化
  • 网站推广方法主要有百度发视频步骤
  • 惠州品牌网站建设公司哪里有seo是干嘛的
  • 推广普通话活动总结天津seo网站推广
  • 做编程的+网站贵州seo技术查询
  • 网站功能设计网上企业推广
  • 网站建设导航分哪几类网站建设服务商
  • 三五互联做网站怎么样镇江网站建设企业
  • 电子商务网站设计分析怎么做电商营销推广方案
  • 网站建设素材包朝阳seo建站
  • 建设一个网站需要哪方面的费用销售网站排名
  • 看网站的访问量附子seo教程
  • 郑州网站APP竞价广告推广
  • 试玩做任务赚钱的网站保定seo网络推广
  • 网站开发ppt模板如何自己做一个网站
  • 长沙天津网站建设seo优化培训
  • 做网站四百全包企业网站制作哪家好
  • 厦门哪里有教网站建设手机百度快照
  • 建设银行的投诉网站衡阳网站建设