linux 中 open和fopen的区别
open 是系统调用 返回的是文件句柄,文件的句柄是文件在文件描述副表里的索引,fopen是C的库函数,返回的是一个指向文件结构的指针。
fopen是ANSIC标准中的C语言库函数,在不同的系统中应该调用不同的内核api
linux中的系统函数是open,fopen是其封装函数
open和fopen的区别:
1、open属于低级IO,fopen是高级IO。
2、open返回一个文件描述符,fopen返回一个文件指针。
3、open无缓冲,fopen有缓冲。
4、open与 read, write 等配合使用, fopen与 fread, fwrite等配合使用。
5、fopen是在open的基础上扩充而来的,在大多数情况下,用fopen。
linux系统默认open files数目为1024, 有时应用程序会报Too many open files的错误,是因为open files 数目不够。这就需要修改ulimit和file-max。特别是提供大量静态文件访问的web服务器,缓存服务器(如squid), 更要注意这个问题。
说明:
1. file-max的含义。man proc,可得到file-max的描述:
/proc/sys/fs/file-max
This file defines a system-wide limit on the number of open files for all processes. (See
also setrlimit(2), which can be used by a process to set the per-process limit,
RLIMIT_NOFILE, on the number of files it may open.) If you get lots of error messages
about running out of file handles, try increasing this value:
即file-max是设置 系统所有进程一共可以打开的文件数量 。同时一些程序可以通过setrlimit调用,设置每个进程的限制。如果得到大量使用完文件句柄的错误信息,是应该增加这个值。
也就是说,这项参数是系统级别的。
2. ulimit
Provides control over the resources available to the shell and to processes started by it, on systems that allow such control.
即设置当前shell以及由它启动的进程的资源限制。
显然,对服务器来说,file-max, ulimit都需要设置,否则就可能出现文件描述符用尽的问题
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!