创建 UNIX 外壳 [英] Creating a UNIX shell

查看:24
本文介绍了创建 UNIX 外壳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为 UNIX 创建一个迷你 shell,只是为了了解一切的来龙去脉.我在理解过去认为理所当然的事情时有些困惑.这有点哲学问题.当我创建一个shell"时,我假设我有一个没有 shell 的 UNIX,那么在这种情况下,std in 和 stdout 是什么?不像 system() 和 exec() 这样的函数使用 shell 来执行程序,所以如果我首先创建一个 shell.这些功能是如何工作的?

I want to create a mini shell for UNIX just to know the ins and outs of everything. I am having some confusions understanding things that I used to get for granted. This is kinda philosophical question. When I creating a "shell", I assume I have a UNIX with no shell, so what would be the std in and std out in this case? doesnt functions like system() and exec() use the shell to execute programs, so if I am creating a shell in the first place. How do these functions work?

推荐答案

exec 家族中有几个函数:execve(2), execl(3)execle(3)execlp(3)execv(3)execvp(3).第一个,execve(2) 由操作系统内核作为系统调用提供.(好吧,程序调用的函数是由系统 C 库提供的,但是系统调用的一个简单包装器.)其他函数提供的语义略有不同,并根据execve(2) 函数.

There are several functions in the exec family: execve(2), execl(3), execle(3), execlp(3), execv(3), execvp(3). The first one, execve(2) is provided by the operating system kernel as a system call. (Well, okay, the function that programs call is provided by the system C library, but it is a simple wrapper around the system call.) The other functions provide slightly different semantics and are implemented in terms of the execve(2) function.

shell 可以使用 execvp(3)execlp(3) 来提供对可执行文件的 PATH 搜索,但至少 bash(1) 散列 可执行文件的完整路径名以提供性能优势.(有关详细信息,请参阅 bash(1) 内置 hash.)

The shells could use execvp(3) or execlp(3) to provide the PATH search for executables, but at least bash(1) hashes the full pathname of executables to provide a performance benefit. (See bash(1) built-in hash for details.)

system(3) 是通过 /bin/sh -c 实现的,正如您所猜测的.

system(3) is implemented via /bin/sh -c, as you've surmised.

标准输入和输出由产生 shell 的任何程序设置.如果用户直接登录控制台,它将由 agetty(8)mgetty(8)getty 处理 -类似的程序处理直接登录.如果用户通过 sshd(8) 登录,则 sshd(8) 负责创建 pty 并将终端从机委托给贝壳.如果用户通过 xterm(1) 或其他终端模拟器创建他们的 shell,那么这些进程将负责为 shell 连接标准输入、输出和错误.

The standard input and output is set up by whichever program spawned the shell. If a user logs in on the console directly, it'll be handled by agetty(8) or mgetty(8) or whichever getty-alike program handles direct logins. If a user logs in via sshd(8), then sshd(8) is in charge of creating the pty and delegating the terminal slave to the shell. If a user creates their shells via xterm(1) or other terminal emulators, then those processes will be responsible for hooking up the standard input, output, and error for the shell.

这篇关于创建 UNIX 外壳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆