0% found this document useful (0 votes)
24 views17 pages

Tut 02

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views17 pages

Tut 02

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Assignment One: Background Knowledge

and Code Walk


CSCI3150 Introduction to Operating Systems, Fall
2024

Shaofeng Wu
[email protected]
Sep 12, 2024
Overview of Assignment One (1/1)

 Assignment One: Implementation of A Simple Shell


 Implement a basic shell that supports
Github classroom for assignment one:
 Built-in commands https://classroom.github.com/a/rrprBsu4
 External commands
 Pipe
 Aim
 Understanding how to interact with an operating system, e.g. Linux
 Understanding the design choices and rationales of a shell

 Before you start, what you need to know


 The behavior of a real (bash) shell
 How does the starter code we provide work?
 How to compile and run the program?
 The code logic of starter code?
 How to verify that your program really works? 2
Shell Behavior (1/8)

 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)

 Important feature of bash shell


 Builtin commands: cd, echo, …
 External commands: ls, find, sleep …
 Key difference
 Builtin commands are executed in main process of shell
 External commands are executed in a child process of the shell
 Try to think why?
 Disclaimer: for bash shell, for single command

4
Shell Behavior (3/8)

 Quick demo on builtin/external commands


 help
 This shows all builtin commands

5
Shell Behavior (4/8)

 Quick demo on builtin/external commands


 (In terminal 1) read -p "Press enter to continue..."
 This executes a builtin command that waits for user input
 (In terminal 2) pgrep -P [pid of your terminal 1]
 This shows all the child process of the bash shell under test

6
Shell Behavior (5/8)

 Quick demo on builtin/external commands


 (In terminal 1) sleep infinity
 This executes a builtin command that waits for user input
 (In terminal 2) pgrep -P [pid of your terminal 1]
 This shows all the child process of the bash shell under test

7
Shell Behavior (6/8)

 Binaries of external commands


 External commands are executed in a child process with their own pre-
built binaries
 Where are these binaries? (quick demo)
 In “/bin”, “/usr/bin”, etc., they have binaries for different purposes
 One take-away here: your own binary can also be a command

 How does bash shell invoke these binaries? (next tutorial) 8


Shell Behavior (7/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)

 Putting things together: builtin/external commands


+ pipe
 Single command
 Builtin commands in the main process
 External commands in child processes
 Multiple commands linked by pipes
 All commands linked by pipes are executed in a child process

1
Compile & Run (1/1)

 How to compile and run


 Use “make” command to compile
 You can search by yourself how to write and use a Makefile
 Use “./SimpleShell” to run your compiled program

Note: The source code we provided is


incomplete
• You can successfully compile with the
sources we provide, but the program
only supports one command “exit”
• Your task is to add other commands to
the incomplete framework, or
implement in your own framework

1
Code Walk (1/3)

 Directory structure
.

DO NOT modify any file other than


• src/simple-execute.c
• ai_declaration.txt
• declaration_en.doc.

DO NOT implement your shell by adding new


files.

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){

 Arguments int child_pid, wait_return, status;

/* Execute built-in commands */


 Args: pointer to the pointer array of /* exit */
if(strcmp(args[0], "exit") == 0 ){

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 }

/* TODOs: one pipe and two pipes */


 0 for next loop(normal execution)
/* wait for child process to terminate */
 -1 for break loop(error or exit) while((wait_return = wait(&status)) > 0);
return 0;
} 1
Code Walk (3/3)
/* shell_execute - Function to execute the command with
specified command content and command arguments.
 shell_execute *
*
@args
@argc
String array with command and its arguments.
Total number of strings including the command, its
arguments and the NULL in the end
 Single builtin command */
int shell_execute(char ** args, int argc){

 In main process int child_pid, wait_return, status;

/* Execute built-in commands */


/* exit */
 Single external command if(strcmp(args[0], "exit") == 0 ){
return -1;
}
 In child process /* TODO: cd */

 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 }

/* TODOs: one pipe and two pipes */

/* wait for child process to terminate */


while((wait_return = wait(&status)) > 0);
return 0;
} 1
Verification (1/2)

 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)

 Is autograding always correct?


 How do we obtain the groundtruth?
 Using a real bash shell to execute the command
 So autograding are theoretically 100% correct
 You can also compare the output of a real bash shell with your program to validate
 Your program
 Our autograding system
 If you find any mistake without modifying any protected file/path (anything except for
src/simple-execute.c), please open a discussion in piazza

1
Q&A

You might also like