Tut 02
Tut 02
Shaofeng Wu
[email protected]
Sep 12, 2024
Overview of Assignment One (1/1)
What is shell?
A computer program that exposes an operating system's services to a
human user or other programs
Each shell is a process (quick demo)
Use “echo $$” to check the process id
Use “ps –ax | grep PID” to check the process information
The shell that most people talk about on Linux is bash shell
Other shells, e.g. powershell, zsh, …
3
Shell Behavior (2/8)
4
Shell Behavior (3/8)
5
Shell Behavior (4/8)
6
Shell Behavior (5/8)
7
Shell Behavior (6/8)
Pipe
Shell has the following meta characters
\/<>!$%^&*|{}[]"'`~;
Pipe(|) is a feature that Linux shell provides to redirect command output to another command
Use “man 7 pipe” in your shell to check details
Some special notes (quick demo)
Space before and after pipe
In bash shells, space before and after pipe does not affect semantics (we do not require this in
asg1, you only need to make sure space before&after pipe is supported)
Several pipes
Builtin commands with pipes: builtin commands will be executed in child process in this case
(this behavior is required in asg1)
How does bash shell implement this feature? (next tutorial)
9
Shell Behavior (8/8)
1
Compile & Run (1/1)
1
Code Walk (1/3)
Directory structure
.
1
Code Walk (2/3)
/* shell_execute - Function to execute the command with
specified command content and command arguments.
simple-execute.c *
*
@args
@argc
String array with command and its arguments.
Total number of strings including the command, its
arguments and the NULL in the end
int shell_execute(char ** args, int argc); */
int shell_execute(char ** args, int argc){
parsed command }
return -1;
/* TODO: cd */
Argc: number of parsed items
/* Non-built-in commands. These commands should be executed
For example, input “ls –l” and enter in a child process so the parent process can continue to invoke
other commands */
/* One command without pipe */
Args = {“ls”, “-l”} if((child_pid = fork()) < 0){
printf("fork() error \n");
}
Argc = 3 else if(child_pid == 0 ){
/* TODO: execute the command and check if the command is
correctly executed */
Return value }
Multiple commands with pipes /* Non-built-in commands. These commands should be executed
in a child process so the parent process can continue to invoke
other commands */
Each one in one child process /* One command without pipe */
if((child_pid = fork()) < 0){
printf("fork() error \n");
}
else if(child_pid == 0 ){
/* TODO: execute the command and check if the command is
Hint: use necessary system calls to correctly executed */
implement these functionalities }
Leveraging auto-grading
Five test cases are provided for your reference
Refer to autograding report to check test cases
We will use another set of test cases for final grading
1
Verification (2/2)
1
Q&A