I'm a newbie to process and thread management. My Shell should understand PATH
environment variable. It can be set and modified. It runs in two ways -interactive & batch mode. Shell is capable of taking more than one job like ls;ps;wc file;cal. I want to get my hands dirty on signals too. So I should handle ^K , ^c as well.
我是处理和线程管理的新手。我的Shell应该理解路径环境变量。它可以被设置和修改。它以两种方式运行——交互和批处理模式。Shell可以执行多个任务,如ls;ps;wc文件;cal。我也想把我的手弄脏信号。所以我应该处理^ K、c ^。
I know I will have to use execs, forks and pipes but just can't get started.
我知道我将不得不使用高级管理人员、餐叉和管道,但就是不能开始。
3 个解决方案
#1
14
All the unix shells are open-source - so a good place to start may be to read the code.
所有的unix shell都是开源的——所以最好的开始是阅读代码。
If you're looking for a good starter article on the subject try Writing Your Own Shell from the Linux Gazette.
如果你想找一篇关于这个主题的好文章,试着在Linux公报上写你自己的Shell。
Another good starting point is to take a look at the source code of mini-shell just because its one of the smallest to get your head round.
另一个好的起点是看一下mini-shell的源代码,因为它是最小的代码之一。
#2
8
Your main loop is:
你的主循环:
- read a line (use
fgets(3)
for a simple shell,readline(3)
for a fancy one) - 读一行(使用fgets(3)为一个简单的shell, readline(3)为一个花哨的)
- parse the command
- 解析命令
- fork and execute the pipelines
- 分叉并执行管道
To parse the command, there are two common choices. Write a recursive descent parser or use yacc(1)
to generate one. It's a lot easier to bang out an initial parser using yacc
, but you can totally get stuck debugging it and it really wants to be context-free. I prefer recursive descent but just about everyone else in the world prefers using yacc. (Technically, bison
.) If the shell is really really simple, like a homework shell, yacc may be overkill.
要解析该命令,有两个常见的选项。编写递归下降解析器或使用yacc(1)生成一个递归下降解析器。使用yacc进行初始解析器会更容易,但是您完全可以调试它,而且它真的想要上下文无关。我更喜欢递归的血统,但是世界上其他的人都喜欢使用yacc。(从技术上讲,野牛。)如果shell真的很简单,比如一个家庭作业shell, yacc可能有点过头了。
To do the lexical analysis you can also roll your own or use flex.
要进行词法分析,您还可以卷自己的或使用flex。
You won't need to use any threads.
您不需要使用任何线程。
#3
4
Many of the Unix books that describe the main system calls also implement a shell to illustrate how and why you might use the various calls. Stevens and Rochkind are two such books:
描述主系统调用的许多Unix书籍还实现了一个shell,以说明如何以及为什么使用各种调用。Stevens和Rochkind是两本这样的书:
-
W Richard Stevens, Stephen A Rago Advanced Programming in the Unix Environment, 3rd Edn
W Richard Stevens, Stephen A Rago in the Unix环境高级编程,第三Edn
-
Marc J Rochkind Advanced Unix Programming, 2nd Edn
Marc J Rochkind高级Unix编程,第二Edn
#1
14
All the unix shells are open-source - so a good place to start may be to read the code.
所有的unix shell都是开源的——所以最好的开始是阅读代码。
If you're looking for a good starter article on the subject try Writing Your Own Shell from the Linux Gazette.
如果你想找一篇关于这个主题的好文章,试着在Linux公报上写你自己的Shell。
Another good starting point is to take a look at the source code of mini-shell just because its one of the smallest to get your head round.
另一个好的起点是看一下mini-shell的源代码,因为它是最小的代码之一。
#2
8
Your main loop is:
你的主循环:
- read a line (use
fgets(3)
for a simple shell,readline(3)
for a fancy one) - 读一行(使用fgets(3)为一个简单的shell, readline(3)为一个花哨的)
- parse the command
- 解析命令
- fork and execute the pipelines
- 分叉并执行管道
To parse the command, there are two common choices. Write a recursive descent parser or use yacc(1)
to generate one. It's a lot easier to bang out an initial parser using yacc
, but you can totally get stuck debugging it and it really wants to be context-free. I prefer recursive descent but just about everyone else in the world prefers using yacc. (Technically, bison
.) If the shell is really really simple, like a homework shell, yacc may be overkill.
要解析该命令,有两个常见的选项。编写递归下降解析器或使用yacc(1)生成一个递归下降解析器。使用yacc进行初始解析器会更容易,但是您完全可以调试它,而且它真的想要上下文无关。我更喜欢递归的血统,但是世界上其他的人都喜欢使用yacc。(从技术上讲,野牛。)如果shell真的很简单,比如一个家庭作业shell, yacc可能有点过头了。
To do the lexical analysis you can also roll your own or use flex.
要进行词法分析,您还可以卷自己的或使用flex。
You won't need to use any threads.
您不需要使用任何线程。
#3
4
Many of the Unix books that describe the main system calls also implement a shell to illustrate how and why you might use the various calls. Stevens and Rochkind are two such books:
描述主系统调用的许多Unix书籍还实现了一个shell,以说明如何以及为什么使用各种调用。Stevens和Rochkind是两本这样的书:
-
W Richard Stevens, Stephen A Rago Advanced Programming in the Unix Environment, 3rd Edn
W Richard Stevens, Stephen A Rago in the Unix环境高级编程,第三Edn
-
Marc J Rochkind Advanced Unix Programming, 2nd Edn
Marc J Rochkind高级Unix编程,第二Edn