C 语言中的 Shell

发布于 2024-12-13 18:41:33 字数 72 浏览 0 评论 0原文

我想用C语言在Linux操作系统下写一个shell。

该项目中使用的库和函数的名称是什么?

谢谢。

I want to write a shell in the Linux operating system with the C language.

What is the name of the libraries and functions to use in this project?

thank you.

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

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

发布评论

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

评论(4

守不住的情 2024-12-20 18:41:33

您可能至少需要:

  • forkexecv - 用于调用子进程。
  • waitpid - 用于收获由 fork+exec 生成的死亡子进程。
  • sigaction - 用于安装信号处理程序:
    • 捕捉中断 (CTRL+C)。
    • 收集死亡的子进程。
  • open - 用于在重定向到文件/从文件重定向时打开文件。
  • dup2 - 用于在实现重定向时用打开的文件替换 STDIN/STDOUT。
  • pipe - 用于创建管道。

单一 UNIX 规范手册页 是所需任何附加功能的良好资源。

You will probably want, at the very least:

  • fork, execv - for invoking a child process.
  • waitpid - for reaping dead child processes spawned with fork+exec.
  • sigaction - for installing signal handlers to:
    • Catch interruption (CTRL+C).
    • Reap dead child processes.
  • open - for opening files when redirecting to/from files.
  • dup2 - for replacing STDIN/STDOUT with an opened file when implementing redirection.
  • pipe - for creating pipes.

The Single UNIX Specification Man Pages are a good resource for any additional functionality needed.

蓝颜夕 2024-12-20 18:41:33

我不明白你的问题。您正在开始一项新事物,您可以使用任何您想要的名称。项目的概念通常特定于某些 IDE。

要编写 shell,首先必须熟悉 C 编程语言,并了解几个重要的 Linux 系统调用(例如 forkexecvepipe、chdirdup 等)。因此,首先阅读一本关于这些的好教科书。系统调用可通过标准 C 库使用,您无需链接额外的库。

也许,研究小型 shell(例如 sash)的源代码会对您有很大帮助。

I don't understand your question. You are starting a new thing, you can use whatever name you want. And the notion of Project is usually specific to some IDEs.

To code a shell, you first must know well the C programming language, and understand well several important Linux system calls (like fork, execve, pipe, chdir, dup etc.). So read a good textbook on these first. The system calls are available thru the standard C library, you don't need to link an extra one.

And probably, studying the source code of small shells (like sash) would help you a lot.

为你鎻心 2024-12-20 18:41:33

实际上,这是一个有趣的问题,对于了解 Unix 的 IO 机制来说,这可能是一个很好的练习。您可能知道,Shell 负责解释输入、调用系统调用并显示输出。该程序本身运行在内核之上。因此,该练习可能会提供有关进程执行系列的大量信息,例如 exec*

这是 一个教程,用 C 语言为您介绍它,这个 one in Python 将帮助您设计一个快速原型以供理解。

Actually, that's an interesting question and it could be a good exercise for something to know the IO mechanism of Unix. As you might know, Shell is responsible for interpreting Input , invoking the system calls and displaying the output. The program as such runs on top of the kernel. So, the exercise might give a lot of info on process execution family such as exec*.

Here is one tutorial covering it for you in C and this one in Python would help you design a quick prototype for understanding.

可爱咩 2024-12-20 18:41:33

不幸的是,您无法为此进行一些神奇的 emulateShell() API 调用,您将需要(深入深入)研究诸如 execforkpopensignal/sigaction 以及许多其他流程控制内容。

此外,您可能需要参与终端处理等,例如 fcntlioctl

这么大的一个答案框,确实不是一个可以全面涵盖的主题。我建议尝试将其分解为较小的工作,以便使您(和我们)的工作变得更轻松。

也许从一个简单的程序开始,该程序接受用户的命令并将其分解为令牌以供执行。这将是良好的第一步。

Unfortunately, there isn't some magical emulateShell() API call you can make for this, you are going to need to look into (in great depth) things like exec, fork, popen, signal/sigaction and a host of other process control things.

In addition, you'll probably need to get involved with terminal handling and so forth, things like fcntl and ioctl.

It's not really a subject that can be covered comprehensively is an answer box this size. I would suggest trying to break it down in smaller jobs so as to make your (and our) job easier.

Perhaps start with a simple program that accepts commands from the user and breaks them down into tokens for execution. That would be a good first step.

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