在 Linux 中,哪些用于目录遍历的 C 头文件是进程安全的?

发布于 2024-08-31 05:48:03 字数 114 浏览 1 评论 0原文

我目前在我的 CGI 网站上使用 dirent.h 和 ftw.h 进行目录遍历,100% 用 C 编程。我不确定它们是否是进程安全的;不同的用户在我的网站上会互相干扰吗?

为此,您会推荐哪些功能?

I'm currently using dirent.h and ftw.h for directory traversal at my CGI website 100% programmed in C. I am not sure if they are process safe; would various users interfere with each other while on my site ?

Which functions would you recommend for this purpose ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心奴独伤 2024-09-07 05:48:03

例如,多个进程使用 ftw() 同时遍历同一目录树是安全的。

但是一个进程遍历目录树而另一个进程正在更新相同的目录树结构(即添加、删除或重命名目录)并不一定安全。如果你遇到这种情况,那么你需要让你的 CGI 进程使用一个 flock() 咨询锁(你可以在共​​享目录树的根部有一个空的锁文件;想要的进程要遍历树必须在该锁文件上获取共享锁,并且想要更改树的进程必须在锁文件上获取独占锁)。

It is safe for multiple processes to, for example, use ftw() to walk the same directory tree at the same time.

However it is not necessarily safe for one process to be walking the directory tree while another process is updating the same directory tree structure (ie adding, removing or renaming directories). If you have this situation, then you will need to make your CGI processes use an flock() advisory lock (you could just have a single empty lockfile in the root of the shared directory tree; processes that want to walk the tree have to take a shared lock on that lockfile, and processes that want to alter the tree have to take an exclusive lock on the lockfile).

夏の忆 2024-09-07 05:48:03

您可能指的是“线程安全”而不是进程安全。所有 libc 调用在 Linux 上都是进程安全的,因为进程(通常)位于单独的内存空间中。
另一方面,readdir 不是线程安全的,因为它为上下文保留了内部静态存储。在这种情况下请使用 readdir_r(_r 表示可重入)。 dirent.h 中的其他函数默认是可重入的。

You probably mean "thread-safe" instead of process-safe. All libc calls are process-safe on Linux as processes (normally) live in separate memory spaces.
On the other hand, readdir is not thread-safe, as it keeps an internal static storage for the context. Use readdir_r in that case (the _r means reentrant). The other functions in dirent.h are reentrant by default.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文