6.S081 Lab2 Shell

      最后更新:2022-06-29 10:07:40 手机定位技术交流文章

      6.S081 Lab2 Shell

      Your job is to write a simple shell for xv6. It should be able to run commands with arguments, handle input and output redirection, and set up two-element pipelines. Your shell should act like the xv6 shellshfor these examples as well as similar commands:

      You should put your shell source inuser/nsh.c, and modify theMakefileto compile it. We’ll test youruser/nsh.cwith our copies of other xv6 files; so while you can modify other files, your shell should not depend on those modifications.Your shell should use@(at sign) as a prompt rather than$,to avoid confusion with the real shell. An xv6 session with your shell might look like this:

      Please donotuse a memory allocator such asmalloc(). Instead you should use only local (stack-allocated) variables and global variables. It’s OK to impose reasonable fixed limits on things like the maximum length of a command name, the maximum number of arguments, or the maximum length of any single argument.

      We supply you with an xv6 test programtestsh, source inuser/testsh.c. You’re done with the lab if your shell passes all the tests, like this:

      概念:6.S081-1系统调用(第一课学习笔记)是如何实现一个壳

      知道如何运行壳和如何重新引导程序是十分重要的,事实上,完成后,其余的任务是处理输入。

      第一个叉子,然后执行exec(类似以前实验中的Lab1实用程序的 xargs),需要重新引导(6.S081-1 系统调用 -6. IO重新引导)

      1.基本功能: @ 开始执行命令

      使用exec执行命令

      运行结果

      2. 重定向功能

      该节是第6节所述的。系统电话S081-1

      该程序是,在叉后,首先关闭标准输出,然后打开一个新的文件,因为fd必须从小到大,所以新开的文件的fd必须为1,这意味着标准输出成功地被重新分配到指定的文件。

      除此以外,关系是1。 最基本的功能shell,(没有变化,exec甚至不知道fd已经改变了)

      代码:

      运行结果:

      3.加工|管道

      主要创建一个允许进程间通信的管道,如参考6号系统调用S081-1

      代码:

      结果(因为 | 将被实壳识别为管子,因此在这里我使用管子而不是 | 作为输入)

      而且, since | 被真正的壳牌识别为管子,我使用管子而不是 |,3. 此外,我只能处理一个管子,需要再处理更多的字),但这也是巧合

      其中用到了dup()函数

      功能: 复制文件描述符并重新将输入向输出.

      复制文件描述符后,新和旧文件描述符的特征:

      使用 dup 或 dup2 复制文件描述符后,新的文件描述符和旧文件描述符将指向相同的文件、管道或网络连接、共享文件锁定、读写位置和各种权限。
      当关闭一个新的文件描述符时, 该文件仍可通过旧文件描述符操作.
      当旧文件描述符关闭时,该文件仍可通过新的文件描述符操作。
      注: 这是共享文件极化和文件状态符号.
      例如,其中一个使用lseek来修改文件极化,而另一个更改操作位置(读写等)。

      当然,使用dup在这里并不那么复杂,只是使用dup(fd[1]),创建一个新的文件描述树,占用刚刚关闭的标准输出的位置,因此创建一个新的fd = 1,指向空间fd[1]指向。

      4. 备注

      • 为了方便,我不会处理多行数据,这些函数都没有内部集成(shell_redirect和shell_pipe),它们实际上是非常昂贵的。但这是为了了解操作系统,而不是做作业,所以就没有实现)。

      • 我个人认为, 这次实验比实验室一简单一些 ( 可能是因为课 )

      本文由 在线网速测试 整理编辑,转载请注明出处,原文链接:https://www.wangsu123.cn/news/28797.html

          热门文章

          文章分类