是readdir,还是readdir_r
readdir的原型如下:
struct dirent *readdir(DIR *dirp);
因为内部使用了静态数据,所以readdir被认为不是线程安全的函数,POSIX[i]标准这样描述:
The application shall not modify the structure to which the return value of readdir() points, nor any storage areas pointed to by pointers within the structure. The returned
pointer, and pointers within the structure, might be invalidated or the structure or the storage areas might be overwritten by a subsequent call to readdir() on the same directory stream. They shall not be affected by a call to readdir()
on a different directory stream.
If a file is removed from or added to the directory after the most recent call to opendir() or rewinddir(),whether a subsequent call to readdir() returns
an entry for that file is unspecified.
The readdir() function need not be thread-safe.
因此才有了readdir_r函数的出现,它的原型如下:
int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result);
readdir_r将返回结果填充到调用者提供的entry缓冲区中,保证了它的线程安全性。
然而,在GNU的官方文档[ii]中,有下面的描述:
In POSIX.1-2008, readdir is not thread-safe. In the GNU C Library implementation, it is safe to call readdir concurrently on different dirstreams, but multiple threads accessing the
same dirstream result in undefined behavior. readdir_r is a fully thread-safe alternative, but suffers from poor portability (see below).
It is recommended that you use readdir, with externallocking if multiple threads access the same dirstream.
Portability Note:
It is recommended to use readdir instead of readdir_r for the following reasons:
On systems which do not define NAME_MAX, it may not be possible to use readdir_r safely because the caller does not specify the length of the buffer for the directory entry.
On some systems,readdir_r cannot read directory entries with very long names. If such a name is encountered, the GNU C Library implementation of readdir_r returns with an error code
of ENAMETOOLONG after the final directory entry has been read. On other systems, readdir_r may return successfully, but the d_name member may not be NUL-terminated or may be truncated.
POSIX-1.2008 does not guarantee that readdir is thread-safe, even when access to the same dirstream is serialized.
But in current implementations(including the GNU C Library), it is safe to call readdir concurrently on different dirstreams, so there is no need to use readdir_r in most multi-threaded programs.
In the rare case that multiple threads need to read from the same dirstream, it is still better to use readdir and external synchronization.
It is expected that future versions of POSIX will obsolete readdir_r and mandate the level of thread safety for readdir which is provided by the GNU C Library and other implementations
today.
尽管POSIX中不保证readdir是线程安全的,但是在目前的实现中(包括GUN C库),在不同的dirstream上(dirp)同时调用readdir能够保证是安全的。因此,多线程程序中其实没必要使用readdir_r,即使在极少场景下,多个线程中需要使用相同的dirstream时,使用readdir以及外部同步手段(加锁),也会是更好的选择。预计在未来版本的POSIX标准中,将会废弃readdir_r。
除了线程安全方面的考虑,没必要使用readdir_r之外,readdir_r还有其他可移植上的缺点,比如某些系统上readdir_r无法处理有很长名字的目录项。
而且,结构体dirent中只有d_name是在POSIX中有明确规定的,它长度还是未指定的(在Linux中,结构体dirent中的d_name,具有明确的数组长度,为256),因此,某些系统中,为了使用readdir_r,必须像下面这样分配entry的内存:
name_max = pathconf(dirpath, _PC_NAME_MAX);
if (name_max == -1) /* Limit not defined, or error */
name_max = 255; /* Take a guess */
len = offsetof(struct dirent, d_name) + name_max + 1;
entryp = malloc(len);
但是这种方式也有问题[iii]。
因此,结论就是:
只要不是多个线程使用相同的dirstream,就尽可能的使用readdir,它其实更简单且更安全。
参考:
http://pubs.opengroup.org/onlinepubs/9699919799/
https://www.gnu.org/software/libc/manual/html_mono/libc.html#Reading_002fClosing-Directory
http://elliotth.blogspot.co.uk/2012/10/how-not-to-use-readdirr3.html
[i] http://pubs.opengroup.org/onlinepubs/9699919799/
[ii] https://www.gnu.org/software/libc/manual/html_mono/libc.html#Reading_002fClosing-Directory
[iii] elliotth's blog: How (not) to usereaddir_r(3):http://elliotth.blogspot.co.uk/2012/10/how-not-to-use-readdirr3.html
是readdir,还是readdir_r的更多相关文章
- C++ readdir、readdir_r函数
readdir, readdir_r - 读一个目录 readdir函数: struct dirent *readdir(DIR *dirp); The data returned by read ...
- Thinkphp拖拽上传文件-使用webuploader插件(自己改动了一些地方)——分片上传
html页面: <!DOCTYPE html> <html class="js cssanimations"> <head> <meta ...
- Linux c readdir是非线程安全,需用readdir_r,要注意用静态变量当做返回值的函数的非线程安全性
readdir函数: struct dirent *readdir(DIR *dirp); The data returned by readdir() may be overwritten by ...
- readdir_r()读取目录内容
readdir()在多线程操作中不安全,Linux提供了readdir_r()实现多线程读取目录内容操作. #include <stdio.h> #include <stdlib.h ...
- PHP之readdir()函数
最近在学习php文件操作的相关知识,记录一下readdir()函数其中的一个要注意的点 1. 在$temp=readdir($handle)函数中 readdir获取的是文件名和$handle中的文件 ...
- 关于readdir返回值中struct dirent.d_type的取值有关问题(转)
关于readdir返回值中struct dirent.d_type的取值问题 原网页链接 http://www.gnu.org/software/libc/manual/html_node/Direc ...
- 目录操作函数opendir、readdir和closedir
首先,明确一个类型DIR的含义: #include <dirent.h> DIR A type representing a directory stream. DIR是在目录项格式 ...
- (转)PHP自定义遍历目录下所有文件dir(),readdir()函数
方法一:使用dir()遍历目录 dir()函数,成功时返回Directory类实例 PHP dir() 语法格式为: dir(directory);//directory为需要显示文件名的目录名称,可 ...
- oendir(),readdir(),closedir() 打开/读取/关闭目录
目录操作 当目标是目录而不是文件的时候,ls -l的结果会显示目录下所有子条目的信息,怎么去遍历整个目录呢?答案马上揭晓! 1. 打开目录 功能:opendir()用来打开参数name指定的目录,并返 ...
随机推荐
- linux操作系统清除报错Disk /dev/mapper/ddf1_4c53492....
现象描述 Disk /dev/mapper/ddf1_4c5349202020202010000055000000004711471100000a28p1: 666.0 GB, 66600088934 ...
- Django 使用模板页面,块标签,模型
1.Django 使用模板页面 Django对于成体系的页面提出了模板继承和模板加载的方式. 1.导入静态页面 2.导入静态文件(css,js,images) 3.修改页面当中的静态地址 1.sett ...
- Leetcode572.Subtree of Another Tree另一个树的子树
给定两个非空二叉树 s 和 t,检验 s 中是否包含和 t 具有相同结构和节点值的子树.s 的一个子树包括 s 的一个节点和这个节点的所有子孙.s 也可以看做它自身的一棵子树. 示例 1: 给定的树 ...
- Fitnesse批量读取变量信息,并保存到用例执行上下文中
Fitnesse变量可以分成两种,一种是自定义变量,另一种是用例执行过程中的临时变量. 在Finesse使用过程中,如果需要定义一些公共的变量,可以统一在一个文件中使用自定义变量的方法,将公共变量全部 ...
- 2019阿里云开年Hi购季新用户分会场全攻略!
2019阿里云云上Hi购季活动已经于2月25日正式开启,从已开放的活动页面来看,活动分为三个阶段: 2月25日-3月04日的活动报名阶段.3月04日-3月16日的新购满返+5折抢购阶段.3月16日-3 ...
- 运行Jmeter时,响应数据中文乱码问题解决办法
需要修改jmeter中的配置,在Jmeter安装目录/bin/jmeter.properties文件中进行修改: sampleresult.default.encoding默认为ISO-8859-1, ...
- 从0开始学习 GitHub 系列之「06.团队合作利器 Branch」
Git 相比于 SVN 最强大的一个地方就在于「分支」,Git 的分支操作简直不要太方便,而实际项目开发中团队合作最依赖的莫过于分支了,关于分支前面的系列也提到过,但是本篇会详细讲述什么是分支.分支的 ...
- RestFul 与 RPC
原文地址:https://blog.csdn.net/u014590757/article/details/80233901 RPC.REST API深入理解 一:RPC RPC 即远程过程调用(Re ...
- linux系统命令配置文件
系统命令要独占地控制系统,并让一切正常工作.所有如 login(完成控制台用户身份验证阶段)或 bash(提供用户和计算机之间交互)之类的程序都是系统命令.因此,和它们有关的文件也特别重要.这一类别中 ...
- python中函数和方法区别,以及如何给python类动态绑定方法和属性(涉及types.MethodType()和__slots__)
网上有很多同义但不同方式的说法,下面的这个说法比较让你容易理解和接受 与类和实例无绑定关系的function都属于函数(function): 与类和实例有绑定关系的function都属于方法(meth ...