Systems Programming: - Chapter 10: Shell Building
Systems Programming: - Chapter 10: Shell Building
Chapter Overview
<subtitle>Shell Building</subtitle> building a simple shell for a Unix system Shell Overview Parsing Launching Processes Inter-Process Communication Releasing Processes Signal Handling
2 / 29
3 / 29
Shells in Unix
even in GUI-oriented systems, shells are very important for user interaction a shell is a process that interacts with the user the shell process creates new processes for many commands issued by the user some commands are built-in, i.e. directly handled by the shell, and not handed over to separate processes the shell is responsible for setting up communication structures for child processes that require it, e.g. via pipes or intermediary files the shell is also responsible for some of the signal handling since it is the process that the user primarily interacts with
Shell Overview
4 / 29
Shell Overview
6 / 29
Parsing Parsing
identifying commands, options, and arguments commands may interact with each other, often via pipes redirection operators may be used for input and output
7 / 29
Parsing Hints
strictly separating all commands and arguments by spaces uses up more space in the input line, but makes parsing much easier the input line is separated into tokens the tokens are examined for their purpose: command option for a command, usually indicated by a dash (-) argument for a command pipe operator (|) redirection operators (< and >) the parsing component produces intermediate structures that are used by the shell to set up the processes, and if necessary the communication infraParsing Copyright 2004 Franz J. Kurfess 8 / 29
Parsing
9 / 29
10 / 29
Process Creation
new processes are created via fork() the children inherit most of the parent process' properties, such as open file descriptors, or the signal mask
Launching Processes
11 / 29
Process Management
the shell must keep a list of the processes it launched since the shell is the main point of contact with the user, it may have to assist the children with the handling of signals
Launching Processes
12 / 29
Cleaning Up
before a process exits, it should clean up after itself in particular, open file descriptors need to be closed
Launching Processes
13 / 29
Launching Processes
14 / 29
15 / 29
Inter-Process Communication
17 / 29
Inter-Process Communication
19 / 29
20 / 29
Releasing Processes
21 / 29
22 / 29
Signal Handling
23 / 29
24 / 29
Overall Design
before you start coding, develop an overall design for your shell this can be a sketch of building blocks, some pseudocode, or a more formal design document such as a UML diagram or specification the components identified above are probably a good starting point
25 / 29
Implementation
once you have the overall design, start with the implementation of the main components concentrate on one component, and try to make it work before proceeding to the next one for components that need to interact with each other, use function prototypes or similar techniques to make sure that they work together
26 / 29
Testing
test often, and as thoroughly as feasible fixing an error in a component you thought is working correctly when you're working on a different component is most likely to be more tedious and timeconsuming than fixing it while you're working on that particular component after a component is completed, try it out in combination with the ones it is supposed to interact with when you're stuck, take a break
27 / 29
28 / 29
Shells in Unix
a shell is an important interaction component between the user and the system since a user typically types one or more commands in a line, and then sends it to the shell by pressing the RETURN key, shells are also known as command lines for most commands issued by the user, the shell launches separate child processes shells frequently use the redirection operators (< and >) for more flexible ways of dealing with input and output for commands pipes are often used to direct the output of one command to be used as input by another command redirection and pipes can be handled by rearranging file descriptors for the processes involved
Chapter Summary Copyright 2004 Franz J. Kurfess 29 / 29
Chapter Summary
30 / 30