children,客户态与操作系统态 -安博电竞网站-安博电竞app下载-安博电竞

国际新闻 259℃ 0

用户态和内核态

程序代码的依靠和调用联络如下图所示:

  • Lib:规范ASCI C函数林姵希,简直一切的渠道都支撑该库函数,因而依靠该库的程序可移植性好;
  • System Function:体系调用函数,与体系内核进行交互,不同渠道具有不同的函数接口,因而可移植性较差

区别用户态和内核态主要是因为体系资源的有限性,不能无限制的随意分配给用户运用,有必要由体系进行sm视频一致办理

  • User mode:不能直接对体系资源进行拜访,假如要操作体系资源,有必要转化为内核态
  • Kernel mode:办理体系资源,可直接对体系资源进行操控和拜访

内核为用户供给了一致的API供其运用,不同的体系的API接口不同,为了便于代码的移植,出台了POSIX规范,类Unix体系(Unix、Linux、BSD、SunOS等)均支撑该规范。

文件流与文件描述符

  • 问题
  • 由上图咱们可看到,每履行一次体系调用,都要涉及到CPU状况的切换,即从用户态切换到内核态,即从用户空间切换到内核空间,完成上下文切换的进程,会耗费适当一部分的CPU资源,因而频频的磁盘拜访对程序的履行功率将形成很大影响。
  • 处理方案
  • 为了处理以上的难题,采用了缓冲区的概念,当对磁盘文件进行操作时,可一次性从磁盘文件中读出很多的数据暂放到缓冲区中,今后对1981年属什么这部分数据的拜访就不需求再进行体系调用了;当对文件行操作后,可将处理后的数据暂存到输出缓保证书怎样写冲区,待文件缓冲区满后,一次性写入到磁盘。

以上,数据的输入输出就像是水在活动相同,因而咱们采用了流的概念。

  • 文件流
  • 简略来说便是树立在面向目标基础上的一种笼统的处理数据的东西。在流中,界说了一些处理数据的根本操作,如读取数据,写入数据等,程序员是对流进行一切操作的,而不必关怀流的另一头数据的真实流向;
  • 文件流用结构体表明:struct FILE.
  • FILE的结构体又是怎样样的呢?咱们能够进行查找一下:
 [niesh@niesh ~]$ vim /usr/include/stdio.h

咱们看到了 stdio.h的文件中有一行:

__BEGIN_NAMESPACE_STD
/* The opaque type of streams. This is the definition used elsewhere. */
typedef struct _IO_FILE FILE;
__END_NAMESPACE_STD

明显,FILE 是 _IO_FILE的类型替换,那么咱们找一下 _IO_FILE在哪里呢?

[niesh@niesh ~]$ grep -rn "\<_io_file>" /usr/include/
/usr/include/c++/4.8.2/streambuf:178: * This is based on _IO_FILE, just reordered to be more consistent,
/usr/include/libio.h:145:struct _IO_jump_t; struct _IO_FILE;
/usr/include/libio.h:163: struct _IO_FILE *_sbuf;
/usr/include/libio.h:246:struct _IO_FILE { //此处正解
/usr/include/libio.h:267: struct _IO_FILE *_chain;
/usr/include/libio.h:291: struct _IO_FILE _file;
/usr/include/libio.h:299: struct _IO_FILE *_freeres_list;
/usr/include/libio.h:316:typedef struct _IO_FILE _IO_FILE;
/usr/include/libio.h:325:#define _IO_stdin ((_IO_FILE*)(&_IO_2_1_stdin_))
/usr/include/libio.h:326:#define _IO_stdout ((_IO_FILE*)(&_IO_2_1_stdout_))
/usr/include/libio.h:327:#define _IO_stderr ((_IO_FILE*)(&_IO_2_1_stderr_))
/usr/include/libio.h:329:extern _IO_FILE *_IO_stdin attribute_hidden;
/usr/include/libio.h:330:extern _IO_FILE *_IO_stdout attribute_hidden;
/usr/include/libio.h:331:extern _IO_FILE *_IO_stderr attribute_hidden;
/usr/include/libio.h:391:extern int __underflow (_IO_FILE *);
/usr/include/libio.h:392:extern int __uflow (_IO_FILE gv网站*);
/usr/include/libio.h:393:extern int __ov枪火erflow (_IO_FILE *, int);
/usr/include/libio.h:395:extern _IO_wint_t __wchildren,客户态与操作体系态 -安博电竞网站-安博电竞app下载-安博电竞underflow (_Ichildren,客户态与操作体系态 -安博电竞网站-安博电竞app下载-安博电竞O_FILE林冲 *);
/usr/include/libio.h:396:extern _IO_wint_t __wuflow (_IO_FILE *);
/usr/include/libio.h:397:extern _IO_wint_t __woverflow (_IO_FILE *, _IO_wint_t);
/usr/include/libio.h:435:extern int _IO_getc (_IO_FILE *__fp);
/usr/include/libio.h:436:extern int _IO_putc (int __c, _IO_FILE *__fp);
/usr/include/libio.h:437:extern int _IO_feof (_IO_FILE *__fp) __THROW;
/usr/include/libio.h:438:extern int _IO_ferror (_IO_FILE *__fp) __THROW;
/usr/include/libio.h:440:extern int _IO_peekc_locked (_IO_FILE *__fp);
/usr/include/libio.h:446:extern void _IO_flockfile (_IO_FILE *) __THROW;
/usr/include/libio.h:447:extern void _IO_funlockfile (_IO_FILE *) __THROW;
/usr/include/libio.h:448:extern int _IO_ftrylockfile (_IO_FILE *) __THROW;
/usr/include/libio.h:465:extern int _IO_vfscanf (_IO_FILE * __restrict, const char * __restrict,
/usr/include/libio.h:467:extern int _IO_vfprintf (_IO_FILE *__restrict, const char *__restrict,
/usr/include/libio.h:469:extern _IO_ssize_t _IO_padn (_IO_FILE *, inabt, _IO_ssize_t);
/usr/include/libio.h:470:extern _IO_size_t _IO_sgetn (_IO_FILE *, void *, _IO_sizechildren,客户态与操作体系态 -安博电竞网站-安博电竞app下载-安博电竞_t);
/usr/include/libio.h:472:extern _IO_off64_t _IO_seekoff (_IO_FILE *, _IO_off64_t, int, int);
/usr/include/libio.h:473:extern _IO_off64_t _IO_seekpos (_IO_FILE *, _IO_off64_t, int);
/usr/include丹尼斯/libio.h:475:extern void _IO_free_backup_area (_IO_FILE *) __THROW;
/usr/include/libio.h:478:extern _IO_wint_t _IO_getwc (_IO_FILE *__fp);
/usr/inclu裁判文书de/libio.h:479:extern _IO_wint_t _IO_putwc (wchar_t __wc, _IO_FILE *__fp);
/usr/include/libio.h:480:extern int _IO_fwide (_IO_FILE *__fp, int __mode) __THROW;
/usr/include/libio.h:514:extern int _IO_vfwscanf (_IO_FILE * __restrict, const w深圳卫视char_t * __restrict,
/usr/include/libio.h:516:extern int _IO_vfwprintf (_IO_FILE *__restrict, const wchar_t *__restrict,
/usr/include/libio.h:中心一号文件518:extern _IO_ssize_t _IO_wpadn (_IO_FILE *, wint_t, _IO_ssize_t);
/usr/include/libio.h:519:extern void _IO_free_wbackup_area (_IO_FILE *) __THROW;
/usr/include/stdio.h:44:struct _IO_F祥ILE;
/usr/include/stdio.h:48:typedef struct _IO_FILE FILE;
/usr/include/stdio.h:64:typedef struct _IO_FILE __FILE;
/usr/include/stdio.h:168:extern struct _IO_FILE *stdin; /* Standard input stream. */
/usr/include/stdio.h:169:extern struct _IO_FILE *stdout; /* Standard output stream. children,客户态与操作体系态 -安博电竞网站-安博电竞app下载-安博电竞*/
/usr/include/stdio.h:170:extern struct _IO_FILE *stderr; /* Standard error output stream. */

(⊙o⊙)children,客户态与操作体系态 -安博电竞网站-安博电竞app下载-安博电竞…,还挺多啊,不过仔细观察,发现只需 libio.h的246行是对该结构体的界说,咱们翻开瞅瞅!

[niesh@niesh ~]$ vim /usr/include/libio.h

struct FILE 的结构体成员如下代码所示:

 struct _IO_FILE {
int _flags; /* High-order word is _IO_MAGIC; rest is flags. */
#define _IO_file_flags _flags

/* The following pointers correspond to the C++ streambuf protocol. */
/* Note: Tk uses the _IO_read_ptr and _IO_read_end fields directly. */
char* _IO_read_ptr; /* Current read pointer */
char* _IO_read_end; /* End of get area. */
char* _IO_read_base; /* Start of putback+get a梦然rea. */
char* _IO_writchildren,客户态与操作体系态 -安博电竞网站-安博电竞app下载-安博电竞e_base; /* Start of put area. */
char* _IO_write_ptr; /* Current put pointer. */
char* _IO_write_end; /* End of put area. */
char* _IO_buf_base; /* Start of reserve area. */
char* _IO_buf_end; /* End of reserve area. */
/* The following fields are used to support backing up and undo. */
char *_IO_save_base; /* Pointer to start of non-current get a皇家俏药娘rea. */
char *_IO_backup_base; /* Pointer to first valid character of backup area */
char *_IO_save_end; /* Pointer to end of non-current get area. */

struct _IO_marker *_markers;

struct _IO_FILE *_chain;

int _filechildren,客户态与操作体系态 -安博电竞网站-安博电竞app下载-安博电竞no;
#if 0
int _blksize;
#else
int _flags2;
#endif
_IO_off_t _old_offset; /* This used to be _offset but it's too small. */

#define __HAVE_COLUMN /* temporary */
/* 1+column number of pbas李变芬e(); 0 is unknown. */
unsigned shor赤铁之心t _cur_column;
signed char _vtable_offset;
char _shortbuf[1];

/* char* _save_gptr; char* _save_egptr; */

_IO_lock_t *_lock;
#ifdef _IO_USE_OLD_IO_FILE
};
  • 文件描述符
  • 每个进程,当翻开一个文件后,内核会为其树立一个翻开文件的数组 (数组的前三个为stdin,stdout,stderr),然后回来翻开文件坐落数组的索引值(下标),该所以只即为文件描述符,只需文件不封闭,用户便能够依据该描述符对文件进行拜访和操作。
  • 不同点
  • 文件描述符:表明为int类型的目标:如stdin对应文件描述符0,stdout对应文件描述符1;
  • :表明为指向结构FILE的指针FILE* ,因而流也称为文件指针
  • 若需求对特定设备进行操控操作,有必要运用文件描述符方法,没有函数能对流进行这类操作
  • 假如需求依照特别的方法进行hyzmI/O操作(例如非堵塞的方法),有必要运用文件描述符方法,也没有函数能对流进行这类操作。
  • 联络
  • 流给用户程序供给了更高一级的(功用更强壮,运用更简化)的I/O接口,它处在文件描述符方法的上层,也便是说,流函数是通军区大院过文件描述符函数来完成的
标签: 暗香诀芝华士