开发者

What C headers for directory traversal are process safe in Linux?

I'm currently using 开发者_StackOverflowdirent.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 ?


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).


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜