Linux内核解析之标准I/O库
当Linux创建一个进程时,会自动创建3个文件描述符0,1,2,分别对应标准输入,标准输出,错误输出。C库中与文件描述符对应的是文件指针。查看C库头文件stdio.h中的源码
typedef struct _IO_FILE FILE; //文件流类型- extern struct _IO_FILE *stdin; /* 标准输入流 */
extern struct _IO_FILE *stdout; /* 标准输出流 */extern struct _IO_FILE *stderr; /* 错误流 */#ifdef __STDC__/* C89/C99 say they're macros. Make them happy. */#define stdin stdin#define stdout stdout#define stderr stderr#endif
_IO_FILE *stdin = (FILE *) &_IO_2_1_stdin_;_IO_FILE *stdout = (FILE *) &_IO_2_1_stdout_;_IO_FILE *stderr = (FILE *) &_IO_2_1_stderr_;
DEF_STDFILE(_IO_2_1_stdin_, 0, 0, _IO_NO_WRITES);DEF_STDFILE(_IO_2_1_stdout_, 1, &_IO_2_1_stdin_, _IO_NO_READS);DEF_STDFILE(_IO_2_1_stderr_, 2, &_IO_2_1_stdout_, _IO_NO_READS+_IO_UNBUFFERED);
下面进入glibc的源码,查看函数_IO_new_file_fopen来验证上面的结论
_IO_FILE *_IO_new_file_fopen (fp, filename, mode, is32not64)_IO_FILE *fp;const char *filename;const char *mode;int is32not64;{int oflags = 0, omode;int read_write;int oprot = 0666;int i;_IO_FILE *result;#ifdef _LIBCconst char *cs;const char *last_recognized;#endifif (_IO_file_is_open (fp))return 0;switch (*mode){case 'r':omode = O_RDONLY;read_write = _IO_NO_WRITES;break;case 'w':omode = O_WRONLY;oflags = O_CREAT|O_TRUNC;read_write = _IO_NO_READS;break;case 'a':omode = O_WRONLY;oflags = O_CREAT|O_APPEND;read_write = _IO_NO_READS|_IO_IS_APPENDING;break;default:__set_errno (EINVAL);return NULL;}#ifdef _LIBClast_recognized = mode;#endiffor (i = 1; i < 7; ++i){switch (*++mode){case '\0':break;case '+':omode = O_RDWR;read_write &= _IO_IS_APPENDING;#ifdef _LIBClast_recognized = mode;#endifcontinue;case 'x':oflags |= O_EXCL;#ifdef _LIBClast_recognized = mode;#endifcontinue;case 'b':#ifdef _LIBClast_recognized = mode;#endifcontinue;case 'm':fp->_flags2 |= _IO_FLAGS2_MMAP;continue;case 'c':fp->_flags2 |= _IO_FLAGS2_NOTCANCEL;continue;case 'e':#ifdef O_CLOEXECoflags |= O_CLOEXEC;#endiffp->_flags2 |= _IO_FLAGS2_CLOEXEC;continue;default:/* Ignore. */continue;}break;}result = _IO_file_open (fp, filename, omode|oflags, oprot, read_write,is32not64);
#include <stdio.h>FILE *fdopen(int fd, const char *mode);int fileno(FILE *stream);
int fileno (_IO_FILE* fp){CHECK_FILE (fp, EOF);if (!(fp->_flags & _IO_IS_FILEBUF) || _IO_fileno (fp) < 0){__set_errno (EBADF);return -1;}return _IO_fileno (fp);}#define _IO_fileno(FP) ((FP)->_fileno)
Linux内核解析之标准I/O库的更多相关文章
- Linux内核解析
一.Linux内核 一个完整可用的操作系统主要由 4 部分组成:硬件.操作系统内核.操作系统服务和用户应用程序,如下图所示: 用户应用程序:是指那些自处理程序. Inter ...
- Linux内核解析:进程间通信:管道
管道的定义管道的用途管道的操作管道非法read与write内核实现解析管道通信原理及其亲戚通信解析父子进程通信解析亲缘关系的进程管道通信解析管道的注意事项及其性质管道有以下三条性质shell管道的实现 ...
- 决Ubuntu使用`make menuconfig`配置Linux 内核时,出现缺少'ncurses-devel'库支持。
*** Unable to find the ncurses libraries or the *** required header files. *** 'make menuconfig' req ...
- linux内核系统调用和标准C库函数的关系分析
http://blog.csdn.net/skyflying2012/article/details/10044343
- linux内核编程笔记【原创】
以下为本人学习笔记,如有转载请注明出处,谢谢 DEFINE_MUTEX(buzzer_mutex); mutex_lock(&buzzer_mutex); mutex_unlock(& ...
- 《LINUX内核设计与实现》第三周读书笔记——第一二章
<Linux内核设计与实现>读书笔记--第一二章 20135301张忻 估算学习时间:共2小时 读书:1.5 代码:0 作业:0 博客:0.5 实际学习时间:共2.5小时 读书:2.0 代 ...
- 《Linux内核设计与实现》 第三周 读书笔记
第一章 Linux内核简介 1. Unix的历史 Unⅸ虽然已经使用了40年,但计算机科学家仍然认为它是现存操作系统中最强大和最优秀的系统. Unix强大的根本原因: 简洁 在Unix中所有的东西都被 ...
- 《Linux内核设计与实现》读书笔记——第一二章
<Linux内核设计与实现>读书笔记——第一二章 第一章 Linux内核简介 1.1 Unix的历史 简洁:仅提供系统调用并有一个非常明确的设计目的. 抽象:Unix中绝大部分东西都被当做 ...
- Linux内核剖析 之 进程简单介绍
1.概念 1.1 什么是进程? 进程是程序运行的一个实例.能够看作充分描写叙述程序已经运行到何种程度的数据结构的汇集. 从内核观点看.进程的目的就是担当分配系统资源(CPU时间,内存 ...
随机推荐
- label自适应文本大小
UILabel *label = [[UILabelalloc] initWithFrame:CGRectZero]; NSString *string = @"aa2fkoksdajfis ...
- Python学习笔记5(函数)
[摘要]本文详细介绍python中的函数,以及与之相关的参数和作用域的概念,并介绍递归的概念以及在程序中的应用. 函数定义 定义函数要用函数定义语句def.如下: def hello(name): r ...
- 【dp】P1077 摆花
基础DP题 题目描述 小明的花店新开张,为了吸引顾客,他想在花店的门口摆上一排花,共m盆.通过调查顾客的喜好,小明列出了顾客最喜欢的n种花,从1到n标号.为了在门口展出更多种花,规定第i种花不能超过a ...
- Linux 命令、配置文件及操作
Linux 命令.配置文件及操作 命令 命令 参数 说明 A alias.unalias 命令别名 B C cat 查看文件内容 cd 切换目录 chown 修改拥有着 chgrp 修改所属组 chm ...
- linux 下 docker-compose安装
docker和dockers-compose的版本兼容对照 以下是我的服务器的相关信息 linux版本 [root@izbp16fm097gaw3tdaog2wz bin]# cat /proc/ve ...
- 【linux】【指令集】查看是否打开selinux
> getenforce selinux相关原理资料参考 <鸟哥的linux私房菜> http://cn.linux.vbird.org/linux_server/0210netw ...
- HDU 3639 SCC Hawk-and-Chicken
求SCC缩点,统计出每个SCC中的点的个数. 然后统计能到达u的最多的点的个数,可以反向建图,再dfs一遍统计出来. 最后说一下,有必要开一个标记数组,因为测试数据中有重边,结果无限WA. #incl ...
- Java EE - Servlet 3.0 和 Spring MVC
Table of Contents 前言 基于 Java 的配置 ServletContainerInitializer 动态配置 DispatcherServlet 和 ContextLoaderL ...
- Python学习-day9 线程
这节内容主要是关于线程的学习 首先要了解的什么是进程,什么是线程 进程与线程 什么是进程(process)? 程序并不能单独运行,只有将程序装载到内存中,系统为它分配资源才能运行,而这种执行的程序就称 ...
- django demo --blog
详情,请看虫师博客http://www.cnblogs.com/fnng/p/3737964.html 和https://my.oschina.net/matrixchan/blog/184445 ...