“ls”是如何实现的? 命令在 Linux/Unix 中工作吗?
我想确切地知道“Is”命令在 Linux 和 Unix 中是如何工作的。
据我所知,ls forks & exec 到 Linux/Unix shell,然后获取输出(当前文件树的输出。例如/home/ankit/)。 我需要更详细的解释,因为我不确定调用 fork 后会发生什么。
谁能详细解释“ls”命令的功能吗?
I would like to know exactly how the "Is" command works in Linux and Unix.
As far as I know, ls forks & exec to the Linux/Unix shell and then gets the output (of the current file tree. eg./home/ankit/). I need a more detailed explanation, as I am not sure about what happens after calling fork.
Could anyone please explain the functionality of the 'ls' command in detail?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
ls 不分叉。 shell 会分叉和执行以运行任何非内置命令,它可以运行的命令之一是 ls。
ls 使用 opendir() 和 readdir() 逐步浏览目录中的所有文件。 如果它需要有关其中之一的更多信息,它会调用 stat()。
ls doesn't fork. The shell forks and execs in order to run any command that isn't built in, and one of the commands it can run is ls.
ls uses opendir() and readdir() to step through all the files in the directory. If it needs more information about one of them it calls stat().
为了补充答案,在《C 编程语言》一书(K&RC)中,他们给出了一个关于如何实现 ls 的小示例。 他们很好地解释了所使用的数据结构和函数。
To add to the answer, in The C Programming Language book (K&RC) they have given a small example on how to go about implementing ls. They have explained the datastructures and functions used very well.
要了解 ls 的作用,您可以查看 OpenSolaris 源代码: https://hg.java.net/hg/solaris~on-src/file/tip/usr/src/cmd/ls/ls.c。
如果这令人难以承受,在 Solaris 上,您可以首先使用 truss 查看 ls 进行的系统调用,以了解它的作用。 使用 truss,尝试:
truss -afl -o ls.out /bin/ls
然后查看 ls.out 中的输出
我相信trace相当于Linux中的truss。
To understand what ls does, you could take a gander at the OpenSolaris source: https://hg.java.net/hg/solaris~on-src/file/tip/usr/src/cmd/ls/ls.c.
If that´s overwhelming, on Solaris you start by using truss to look at the system calls that ls makes to understand what it does. Using truss, try:
truss -afl -o ls.out /bin/ls
then look at the output in ls.out
I believe that trace is the equivalent to truss in Linux.
如果你真的想了解 ls 的详细内部结构,请查看源代码。 您可以通过 tpgould 的链接找到 Solaris 源代码,或者很容易从任何 Linux 或 BSD 发行版在线找到源代码。
我会特别推荐 4.4BSD 源代码。
我记得, ls 首先解析它的许多选项,然后从命令行上列出的文件或目录开始(默认为“.”)。 子目录通过递归到目录列表例程来处理。 我记得没有 fork() 或 exec() 。
If you really want to understand the detailed innards of ls, look at the source code. You can follow tpgould's link to the Solaris source, or it's easy to find the source online from any Linux or BSD distribution.
I'll particularly recommend the 4.4BSD source.
As I recall, ls starts by parsing its many options, then starts with the files or directories listed on the command line (default is "."). Subdirectories are handled by recursion into the directory list routine. There's no fork() or exec() that I recall.
这是一个旧线程,但我仍然发表评论,因为我相信被投票和接受的答案部分不正确。 @Mark 说 ls 内置于 shell 中,因此 shell 不会执行和 fork。 当我研究bash上的tldp文档时(我已附上链接)
“ls”未列为内置命令。
http://tldp.org/LDP/Bash-Beginners-Guide/html /sect_01_03.html
Bash 内置命令:
alias、bind、builtin、command、declare、echo、enable、help、let、local、logout、printf、read、shopt、type、typeset、ulimit 和 unalias。
This is a old thread , but still I am commenting because I believe the answer which was upvoted and accepted is partially incorrect. @Mark says that ls is built into shell so shell doesn't exec and fork. When I studied the tldp document on bash(I have attached the link)
"ls" is not listed as a build in command.
http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_01_03.html
Bash built-in commands:
alias, bind, builtin, command, declare, echo, enable, help, let, local, logout, printf, read, shopt, type, typeset, ulimit and unalias.