Linux文件和目录管理全解析
1. 工作目录的管理
在Linux系统中,工作目录的管理十分重要。以下代码展示了如何保存当前工作目录,切换到其他目录,完成工作后再返回原目录:
int swd_fd;
swd_fd = open (".", O_RDONLY);
if (swd_fd == -1) {
perror ("open");
exit (EXIT_FAILURE);
}
/* change to a different directory */
ret = chdir (some_other_dir);
if (ret) {
perror ("chdir");
exit (EXIT_FAILURE);
}
/* do some other work in the new directory... */
/* return to the saved directory */
ret = fchdir (swd_fd);
if (ret) {
perror ("fchdir");
exit (EXIT_FAILURE);
}
/* close the directory's fd */
ret = close (swd_fd);
if (ret) {
perror ("close");
exit (EXIT_FAILURE);
}
这个过程其实就是shell实现缓存上一个目录的方式,例如在bash中使用 cd - 命令。不同类型的进程对工作目录有不同的设置:
- 像守护进程这类不关心当前工作目录的进程,通
超级会员免费看
订阅专栏 解锁全文

被折叠的 条评论
为什么被折叠?



