我正在尝试用 C 和/或 C++ 为 Linux 编写一个 shell。我应该使用哪些函数与内核交互?有人说使用system()
,但我认为它会调用shell,这会导致无限递归。其他人说了一些关于fork()
和exec()
的事情。
显然,一个好的 shell 需要某种方式来访问文件系统。我假设内核为此提供了某种接口,不是吗?我如何读取内核的输出?是文本还是整数?
有没有地方可以找到有关必要功能的良好文档?是否有我需要阅读的特定手册页或源代码文件?
I am trying to write a shell for Linux in C and/or C++. What functions should I use to interact with the kernel? Someone said to use system()
, but I think it calls the shell, which would result in infinite recursion. Someone else said something about fork()
and exec()
.
Obviously a good shell needs some way to access the file system. I assume the kernel provides an interface of some sort for this, does it not? How would I read the output from the kernel? Is it text or integers?
Is there a place where I can find good documentation on the necessary functions? Is there a particular man page or source code file that I sould read?
发布评论
评论(4)
内核提供了一组用于低级进程执行和文件系统访问的“系统调用”。流程执行通常使用
fork()
和exec()
家庭通话。 I/O 可以通过诸如open()
,stat()
,opendir()
等。大多数更可移植的调用(C 库和系统调用)的列表可以在 开放组基本规范(单击“系统接口”)。还有一些特定于 Linux 的 - 那些用于普通用途的将记录在手册页的第 2 部分中(
ls /usr/share/doc/man/man2
或man 2 某些功能
)。The kernel provides a set of 'system calls' for low-level process execution and file system access. Process execution is generally done using
fork()
and one of theexec()
family calls. I/O can be done with calls such asopen()
,stat()
,opendir()
, etc.A list of most of the more-portable calls (both C library and system calls) can be found at the Open Group Base Specification (click 'system interfaces'). There are some linux-specific ones as well - the ones intended for ordinary use will be documented in section 2 of the manual pages (
ls /usr/share/doc/man/man2
orman 2 somefunction
).操作系统教科书是一个很好的起点。我会推荐 Silberschatz 等人的“恐龙书”。另一个很好的资源是 Dave Hollinger 教授在 RPI 上的操作系统课程页面。讲座幻灯片位于网上,其中包括一篇有关编写 shell 的讲座。那里还有一个关于编写基本 shell 的作业,您可以查看一下。
http://cgi2.cs.rpi.edu/~hollingd/opsys/ opsys.php
shell 是一个非常复杂的程序,无法在 StackOverflow 答案中真正描述。然而,这些资源应该为您指明正确的方向。
A good place to start is an operating systems textbook. I would recommend the "dinosaur book" by Silberschatz et al. Another good resource is Prof. Dave Hollinger's class page at RPI for his operating systems course. The lecture slides are online, including one lecture on writing a shell. There's also a homework assignment there on writing a basic shell that you can check out.
http://cgi2.cs.rpi.edu/~hollingd/opsys/opsys.php
A shell is a very complicated program that can't really be described in a StackOverflow answer. However, these resources should point you in the right direction.
系统调用由Linux 内核 记录在 系统调用(2) 手册页。
shell 通常不会与所有这些接口连接。 shell 通常最关心的是影响它可以运行的进程的系统调用。例如,shell 通常不需要(自行)调用 mmap 或mprotect 系统调用(即使
malloc
的实现可能会调用 <代码> mmap )。The list of system calls understood by the linux kernel is documented in the syscalls(2) man page.
A shell usually don't interface to all of them. A shell is usually mostly concerned about system calls impacting the processes it can run. For example, a shell usually don't need to call (by itself) the mmap or mprotect syscalls (even if the implementation of
malloc
probably callsmmap
).首先您需要了解现有 shell 提供的所有功能:
1. 分叉可执行文件并执行它们。
2. 一个解释器,用于 shell 脚本。
3. 很少有内置命令,如 cd 等。
4. 还有更多的事情..我建议阅读一些 UNIX 操作系统书籍
如果您只想在新 shell 中运行命令..那么您必须分叉并执行(或者您可以使其线程化)“/bin”、“/usr/bin”等处已存在的可执行文件。
如果它只是一个业余爱好项目,我建议你使用 Python subprocess 模块
First of all you need to know what all functionalities the existing shell provides :
1. Forking executables and executing them.
2. An interpreter, for shell scripts.
3. Few built-in commands like cd etc..
4. And many more things..I would suggest to read some UNIX OS book
If you just want to run commands in your new shell .. then you have to just fork and execute(or you can make it threaded) the already existing executables at "/bin" , "/usr/bin" etc.
If its just a hobby project I would recommend you to use Python subprocess module