高等UNIX程式設計:作業三
0x00 前言
0x01 作業描述
本次作業主要為練習 process relationships, descriptors, signals 和 simple IPC(Inter-Process Communication) with pipes
需要實作一個 Simple Shell,包含以下要求
- [basic] Execute a single command.
- [basic] Properly block or unblock signals.
- [basic] Replace standard input/output of a process using the redirection operators (< and >).
- [basic] Setup foreground process group and background process groups.
- [basic] Create pipeline for commands separated by the pipe operator (|), and put the commands into the same process group.
- [optional] Manipulate environment variables: provide commands like
export
andunset
to add and remove environment variables, respectively. - [optional] Expand of the
*
and?
operators. - [optional] Job control: support process suspension using
Ctrl-Z
, andjobs
,fg
andbg
command.
not allowed to invoke commands using other shells directly in your program via functions like system() or other equavalent implementations in your program
需要提供 Makefile 和 README
0x02 範例程式
Execute a single command:
shell-prompt$ ls -la
Properly block or unblock signals: Sending SIGINT or SIGQUIT should not terminate the current running shell.
shell-prompt$ sleep 10
Run the above command and send signals like SIGINT or SIGQUIT to the correct process (before the process terminates). Note, this test may only work if you have already properly set the foreground process group.
Replace standard output of a process:
shell-prompt$ ls -la > /tmp/x
You will be not able to see the output, but the output is written to the file /tmp/x.
Replace standard input of a process:
shell-prompt$ cat < /etc/passwd
Setup foreground process group and background process groups:
shell-prompt$ less /etc/passwd
Create pipelines:
shell-prompt$ cat /etc/passwd | cat | less
Put processes into the same process group:
shell-prompt$ ps -o pid,sid,pgid,ppid,cmd | cat | cat | tr A-Z a-z
Processes generated by a single command should be in the same process group.
Manipulate environment variables:
You can use the relevant commands to add and remove environment variables and then use the printenv
command to list all the available environment variables.
Expand * and ? characters:
shell-prompt$ ls *
If you see an error message like ls: cannot access *: No such file or directory
, you did not properly implement the filename expansion feature.
Job control:
You should be able to put a process (or a group of processes) into background if an & operator is appended to a command. You should also implement jobs
, fg
and bg
command to change the current foreground process group and continue the execution of a stopped process group.