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

cc域名的网站产品线上推广方案

cc域名的网站,产品线上推广方案,网站建设设计作品怎么写,单职业传奇网站目录 linux 中man 2和man 3的区别 文件内容介绍 C语言文件接口 示例: 输出信息到显示器,你有哪些方法 总结: 系统文件I/O 文件类的系统调用接口介绍 示例 open 函数具体使用哪个,和具体应用场景相关, write read close lseek ,类比C文件相关接…

目录

linux 中man 2和man 3的区别

文件内容介绍

C语言文件接口

示例:

输出信息到显示器,你有哪些方法

总结:

系统文件I/O

文件类的系统调用接口介绍

示例

open 函数具体使用哪个,和具体应用场景相关,

write read close lseek ,类比C文件相关接口

open函数返回值

在认识返回值之前,先来认识一下两个概念: 系统调用 和 库函数

文件描述符fd

为什么打开文件myfile.txt的描述符是3不是0?


linux 中man 2和man 3的区别

文件内容介绍

        文件 = 内容+属性.

文件系统

  •         被打开的文件         -------内存中
  •         没有被打开的文件  -------磁盘中

没打开文件之前,文件存储在磁盘, 进程想要访问文件之前 ,要打开文件(也就是将文件加载到内存中)

文件加载到内存中要被OS管理(先描述,再组织)

                                                         也就产生了文件的内核数据结构+文件的内容.(类似进程)

C语言文件接口

示例:

  1 #include<stdio.h>2 #include<string.h>3 #include<errno.h>4 5 int main()6 {7     FILE* fp =fopen("myfile.txt","w+");8     if(!fp) perror("fopen");9 10     int count =6;11     const char* ch ="hello world\n";12     while(count--)13     {14         fwrite(ch ,strlen(ch),1,fp);15     }16     fclose(fp);17     return 0;18 }

进程创建好了,默认先打开了三个输入输出流

  1. stdin   标准输入流       键盘
  2. stdout  标准输出流     显示器
  3. stderr  标准错误流      显示器

仔细观察发现,这三个流的类型都是FILE*, fopen返回值类型,文件指针

输出信息到显示器,你有哪些方法

printf("hello linux\n");const char* ch ="hello linux\n";
fwrite(ch ,strlen(ch), 1 ,stdout);
fpritf(stdout, "hello linux\n");
fputs("aaaa\n" ,stdout);

总结:

打开文件的方式

r     Open text file for reading. The stream is positioned at the beginning of the file.r+    Open for reading and writing.The stream is positioned at the beginning of the file.w     Truncate(缩短) file to zero length or create text file for writing.The stream is positioned at the beginning of the file.w+    Open for reading and writing.The file is created if it does not exist, otherwise it is truncated.The stream is positioned at the beginning of the file.a    Open for appending (writing at end of file). The file is created if it does not exist. The stream is positioned at the end of the file.a+   Open for reading and appending (writing at end of file).The file is created if it does not exist. The initial file position for reading is at the beginning of the file, but output is always appended to the end of the file.

系统文件I/O

前面在讲计算机冯诺依曼体系结构时讲过

  •         OS要确保底层的安全稳定,对于上层开发提供了一些系统调用(system call)接口,用于对硬件的访问(I/O)
  •         而上面C语言的库函数fopen可以打开文件, fwrite可以向文件中写入, 文件在磁盘上,可以推断出,这些C语言中这些文件I/O一定封装了系统调用接口.

文件类的系统调用接口介绍

        open接口可以在linux中输入查询

man 2 open
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);pathname: 要打开或创建的目标文件
flags: 打开文件时,可以传入多个参数选项,用下面的一个或者多个常量进行“或”运算,构成flags。flags的参数:O_RDONLY: 只读打开O_WRONLY: 只写打开O_RDWR : 读,写打开这三个常量,必须指定一个且只能指定一个O_CREAT : 若文件不存在,则创建它。需要使用mode选项,来指明新文件的访问权限O_APPEND: 追加写返回值:成功:新打开的文件描述符失败:-1

flags是位图,  参数:  比如O_RDONLY是32位中只有一个为1的int.

因此,参数之间要用  |  或运算.

        mode_t理解:第三个参数 mode用于指定创建文件时的权限(文件模式)。它仅在创建新文件时生效.

示例:

  1. 创建一个新文件,权限为 0644(用户可读写,组和其他用户只读):

int fd = open("example.txt", O_CREAT | O_WRONLY, 0644);

open 函数具体使用哪个,和具体应用场景相关,

  • 如目标文件不存在,需要三参数的open创建,则第三个参数表示创建文件 的默认权限.
  • 目标文件存在,使用两个参数的open。

write read close lseek ,类比C文件相关接口

系统调用库函数

  • 上面的 fopen fclose fread fwrite 都是C标准库当中的函数,我们称之为库函数(libc)
  • 而, open close read write lseek 都属于系统提供的接口,称之为系统调用接口 回忆一下讲操作系统概念时,画的一张图
  • 也就是C语言的文件接口(fopen fclose fread fwrite ),封装了文件类的系统调用接口(open read write close),方便了二次开发.

文件描述符fd

        通过对open函数的学习,我们知道了open 的返回值是文件描述符, 就是一个小整数

        在上面open示例代码中发现打出来myfile.txt的文件描述符fd=3

为什么打开文件myfile.txt的描述符是3不是0?

        进程启动 ,默认打开三个标准的输入输出流

        因为在打开文件myfile.txt前,就打开了三个文件(stdin stdout stderr),他们的文件描述符分别是0 ,1 ,2

验证: 默认打开三个标准的输入输出流

  1 #include<stdio.h>2 #include<string.h>3 #include<errno.h>4 #include <sys/types.h>5 #include <sys/stat.h>6 #include <fcntl.h>7 8 int main()9 {10     printf("stdin: %d\n", stdin->_fileno);11     printf("stdout: %d\n", stdout->_fileno);12     printf("stderr: %d\n", stderr->_fileno);13 14     int fd1 =open("open.txt" , O_RDWR|O_CREAT , 0666);15     if(fd1 < 0)16     {17         perror("open");18         return 1;19     }20     printf("fd1 =%d\n",fd1);21 22     const char* message ="hello linux\n";23     write(fd1, message, strlen(message));24     close(fd1);25     return 0;}

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

struct file就是文件的属性 ,类似进程的task_struct

一个进程通常打开多个文件,如何让进程与文件关联?

  • 每个进程都有一个指针*files, 指向一张表 files_struct , 该表最重要的部分就是包涵一个指针数组fd_array(文件描述符表),每个元素都是一个指向打开文件的指针!

      所以,本质上,文件描述符就是该数组的下标。所以,只要拿着文件描述符,就可以找到对应的文件

进程的task_struct中有 file_struct的指针

file_struct中有指针数组fd_array(文件描述符表)

        OS想要管理进程创建了进程管理列表,文件同理,创建了文件管理列表(也是双链表)

        内核中,让进程和文件之间产生关联的是文件描述符表, 左边是进程,右面是文件 ,通过文件描述符表进行解耦合

如图所示:左面是进程管理,右侧是文件管理

http://www.hrbkazy.com/news/51576.html

相关文章:

  • asp网站编辑教程百度客服人工
  • 网站推广方案200字搜索引擎优化的基本内容
  • 那个网站可以做链接建站平台在线提交功能
  • 网站基本建设投资内容如何查询关键词的搜索量
  • 洛阳网站建设哪家专业百度竞价收费标准
  • 在哪做网站便宜又好百度小说排行榜总榜
  • 公司网站开发怎么做百度域名购买
  • 网站运营怎样做网站推广方案范例
  • 成都市医院网站建设网站推广找客户
  • 明天上海全面封控5天百度seo搜索引擎优化
  • 有哪些做网站的公司天津seo推广软件
  • 做网站做国外广告想要网站导航推广
  • 网站建设实验小结优化设计七年级上册语文答案
  • 注册了网站怎么建设厦门人才网个人会员
  • 网站如何做ins链接分享b2b
  • 建设网站的公司广州北京今日重大新闻
  • 第一媒体app最新版本免费网站seo诊断
  • 0基础学习网站建设贵港网站seo
  • wordpress投稿者用户权限seo优化推广业务员招聘
  • 类似饿了么的网站怎么做seo竞价排名
  • 软件和网站开发百度链接提交收录入口
  • 网络营销策划需要包括哪些内容seo百度首页排名业务
  • 建设网站推广贷款业务网络推广好做吗
  • 做电销要在哪个网站上找资源软文外链代发
  • 合肥个人做网站短视频代运营方案模板
  • wordpress机械免费主题seodao cn
  • 南海区建设网站深圳百度搜索排名优化
  • h5手机网站发展趋势百度业务推广
  • 网站域名怎么写怎么制作公司网页
  • 中国知名设计网站百度的竞价排名是哪种方式